Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / gengtype.c
blob2a158e9f0e45681e411ff2b78f19c824a0b45b14
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 == BARRIER && aindex >= 3)
1223 t = scalar_tp, subname = "rt_int";
1224 else if (i == ENTRY_VALUE && aindex == 0)
1225 t = rtx_tp, subname = "rt_rtx";
1226 else
1228 error_at_line
1229 (&lexer_line,
1230 "rtx type `%s' has `0' in position %lu, can't handle",
1231 rtx_name[i], (unsigned long) aindex);
1232 t = &string_type;
1233 subname = "rt_int";
1235 break;
1237 case 's':
1238 case 'S':
1239 case 'T':
1240 t = &string_type;
1241 subname = "rt_str";
1242 break;
1244 case 'e':
1245 case 'u':
1246 t = rtx_tp;
1247 subname = "rt_rtx";
1248 break;
1250 case 'E':
1251 case 'V':
1252 t = rtvec_tp;
1253 subname = "rt_rtvec";
1254 break;
1256 case 't':
1257 t = tree_tp;
1258 subname = "rt_tree";
1259 break;
1261 case 'B':
1262 t = basic_block_tp;
1263 subname = "rt_bb";
1264 break;
1266 default:
1267 error_at_line
1268 (&lexer_line,
1269 "rtx type `%s' has `%c' in position %lu, can't handle",
1270 rtx_name[i], rtx_format[i][aindex],
1271 (unsigned long) aindex);
1272 t = &string_type;
1273 subname = "rt_int";
1274 break;
1277 subfields = create_field (subfields, t,
1278 xasprintf (".fld[%lu].%s",
1279 (unsigned long) aindex,
1280 subname));
1281 subfields->opt = nodot;
1282 if (t == note_union_tp)
1283 subfields->opt =
1284 create_string_option (subfields->opt, "desc",
1285 "NOTE_KIND (&%0)");
1286 if (t == symbol_union_tp)
1287 subfields->opt =
1288 create_string_option (subfields->opt, "desc",
1289 "CONSTANT_POOL_ADDRESS_P (&%0)");
1292 if (i == SYMBOL_REF)
1294 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1295 holds. */
1296 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1297 subfields
1298 = create_optional_field (subfields, field_tp, "block_sym",
1299 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1302 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1303 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1304 NULL);
1306 ftag = xstrdup (rtx_name[i]);
1307 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1308 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1309 flds = create_field (flds, substruct, "");
1310 flds->opt = create_string_option (nodot, "tag", ftag);
1312 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1313 nodot);
1316 /* Handle `special("tree_exp")'. This is a special case for
1317 field `operands' of struct tree_exp, which although it claims to contain
1318 pointers to trees, actually sometimes contains pointers to RTL too.
1319 Passed T, the old type of the field, and OPT its options. Returns
1320 a new type for the field. */
1322 static type_p
1323 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1325 pair_p flds;
1326 options_p nodot;
1328 if (t->kind != TYPE_ARRAY)
1330 error_at_line (&lexer_line,
1331 "special `tree_exp' must be applied to an array");
1332 return &string_type;
1335 nodot = create_string_option (NULL, "dot", "");
1337 flds = create_field (NULL, t, "");
1338 flds->opt = create_string_option (nodot, "length",
1339 "TREE_OPERAND_LENGTH ((tree) &%0)");
1340 flds->opt = create_string_option (flds->opt, "default", "");
1342 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1343 nodot);
1346 /* Perform any special processing on a type T, about to become the type
1347 of a field. Return the appropriate type for the field.
1348 At present:
1349 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1350 - Similarly for arrays of pointer-to-char;
1351 - Converts structures for which a parameter is provided to
1352 TYPE_PARAM_STRUCT;
1353 - Handles "special" options.
1356 type_p
1357 adjust_field_type (type_p t, options_p opt)
1359 int length_p = 0;
1360 const int pointer_p = t->kind == TYPE_POINTER;
1361 type_p params[NUM_PARAM];
1362 int params_p = 0;
1363 int i;
1365 for (i = 0; i < NUM_PARAM; i++)
1366 params[i] = NULL;
1368 for (; opt; opt = opt->next)
1369 if (strcmp (opt->name, "length") == 0)
1371 if (length_p)
1372 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1373 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1375 error_at_line (&lexer_line,
1376 "option `%s' may not be applied to "
1377 "arrays of atomic types", opt->name);
1379 length_p = 1;
1381 else if ((strcmp (opt->name, "param_is") == 0
1382 || (strncmp (opt->name, "param", 5) == 0
1383 && ISDIGIT (opt->name[5])
1384 && strcmp (opt->name + 6, "_is") == 0))
1385 && opt->kind == OPTION_TYPE)
1387 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1389 if (!union_or_struct_p (t)
1390 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1392 error_at_line (&lexer_line,
1393 "option `%s' may only be applied to structures or structure pointers",
1394 opt->name);
1395 return t;
1398 params_p = 1;
1399 if (params[num] != NULL)
1400 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1401 if (!ISDIGIT (opt->name[5]))
1402 params[num] = create_pointer (opt->info.type);
1403 else
1404 params[num] = opt->info.type;
1406 else if (strcmp (opt->name, "special") == 0
1407 && opt->kind == OPTION_STRING)
1409 const char *special_name = opt->info.string;
1410 if (strcmp (special_name, "tree_exp") == 0)
1411 t = adjust_field_tree_exp (t, opt);
1412 else if (strcmp (special_name, "rtx_def") == 0)
1413 t = adjust_field_rtx_def (t, opt);
1414 else
1415 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1418 if (params_p)
1420 type_p realt;
1422 if (pointer_p)
1423 t = t->u.p;
1424 realt = find_param_structure (t, params);
1425 t = pointer_p ? create_pointer (realt) : realt;
1428 if (!length_p
1429 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1430 return &string_type;
1431 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1432 && t->u.a.p->u.p->kind == TYPE_SCALAR
1433 && t->u.a.p->u.p->u.scalar_is_char)
1434 return create_array (&string_type, t->u.a.len);
1436 return t;
1440 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1441 bool = false);
1442 static void set_gc_used (pair_p);
1444 /* Handle OPT for set_gc_used_type. */
1446 static void
1447 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1448 int *pass_param, int *length, int *skip,
1449 type_p *nested_ptr)
1451 options_p o;
1452 for (o = opt; o; o = o->next)
1453 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1454 && o->kind == OPTION_TYPE)
1455 set_gc_used_type (o->info.type,
1456 GC_POINTED_TO, NULL);
1457 else if (strcmp (o->name, "maybe_undef") == 0)
1458 *maybe_undef = 1;
1459 else if (strcmp (o->name, "use_params") == 0)
1460 *pass_param = 1;
1461 else if (strcmp (o->name, "length") == 0)
1462 *length = 1;
1463 else if (strcmp (o->name, "skip") == 0)
1464 *skip = 1;
1465 else if (strcmp (o->name, "nested_ptr") == 0
1466 && o->kind == OPTION_NESTED)
1467 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1471 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1473 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1474 are set to GC_UNUSED. Otherwise, an error is emitted for
1475 TYPE_UNDEFINED types. This is used to support user-defined
1476 template types with non-type arguments.
1478 For instance, when we parse a template type with enum arguments
1479 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1480 artificial fields for 'MyType', one for 'AnotherType', the other
1481 one for 'EnumValue'.
1483 At the time that we parse this type we don't know that 'EnumValue'
1484 is really an enum value, so the parser creates a TYPE_UNDEFINED
1485 type for it. Since 'EnumValue' is never resolved to a known
1486 structure, it will stay with TYPE_UNDEFINED.
1488 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1489 'EnumValue'. Generating marking code for it would cause
1490 compilation failures since the marking routines assumes that
1491 'EnumValue' is a type. */
1493 static void
1494 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1495 bool allow_undefined_types)
1497 if (t->gc_used >= level)
1498 return;
1500 t->gc_used = level;
1502 switch (t->kind)
1504 case TYPE_STRUCT:
1505 case TYPE_UNION:
1506 case TYPE_USER_STRUCT:
1508 pair_p f;
1509 int dummy;
1510 type_p dummy2;
1511 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1513 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1514 &dummy2);
1516 for (f = t->u.s.fields; f; f = f->next)
1518 int maybe_undef = 0;
1519 int pass_param = 0;
1520 int length = 0;
1521 int skip = 0;
1522 type_p nested_ptr = NULL;
1523 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1524 &length, &skip, &nested_ptr);
1526 if (nested_ptr && f->type->kind == TYPE_POINTER)
1527 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1528 pass_param ? param : NULL);
1529 else if (length && f->type->kind == TYPE_POINTER)
1530 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1531 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1532 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1533 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1534 set_gc_used_type (find_param_structure (f->type->u.p, param),
1535 GC_POINTED_TO, NULL);
1536 else if (skip)
1537 ; /* target type is not used through this field */
1538 else
1539 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1540 allow_undefined_field_types);
1542 break;
1545 case TYPE_UNDEFINED:
1546 if (level > GC_UNUSED)
1548 if (!allow_undefined_types)
1549 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1550 t->gc_used = GC_UNUSED;
1552 break;
1554 case TYPE_POINTER:
1555 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1556 break;
1558 case TYPE_ARRAY:
1559 set_gc_used_type (t->u.a.p, GC_USED, param);
1560 break;
1562 case TYPE_LANG_STRUCT:
1563 for (t = t->u.s.lang_struct; t; t = t->next)
1564 set_gc_used_type (t, level, param);
1565 break;
1567 case TYPE_PARAM_STRUCT:
1569 int i;
1570 for (i = 0; i < NUM_PARAM; i++)
1571 if (t->u.param_struct.param[i] != 0)
1572 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1574 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1575 level = GC_POINTED_TO;
1576 else
1577 level = GC_USED;
1578 t->u.param_struct.stru->gc_used = GC_UNUSED;
1579 set_gc_used_type (t->u.param_struct.stru, level,
1580 t->u.param_struct.param);
1581 break;
1583 default:
1584 break;
1588 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1590 static void
1591 set_gc_used (pair_p variables)
1593 int nbvars = 0;
1594 pair_p p;
1595 for (p = variables; p; p = p->next)
1597 set_gc_used_type (p->type, GC_USED, NULL);
1598 nbvars++;
1600 if (verbosity_level >= 2)
1601 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1604 /* File mapping routines. For each input file, there is one output .c file
1605 (but some output files have many input files), and there is one .h file
1606 for the whole build. */
1608 /* Output file handling. */
1610 /* Create and return an outf_p for a new file for NAME, to be called
1611 ONAME. */
1613 static outf_p
1614 create_file (const char *name, const char *oname)
1616 static const char *const hdr[] = {
1617 " Copyright (C) 2004-2013 Free Software Foundation, Inc.\n",
1618 "\n",
1619 "This file is part of GCC.\n",
1620 "\n",
1621 "GCC is free software; you can redistribute it and/or modify it under\n",
1622 "the terms of the GNU General Public License as published by the Free\n",
1623 "Software Foundation; either version 3, or (at your option) any later\n",
1624 "version.\n",
1625 "\n",
1626 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1627 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1628 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1629 "for more details.\n",
1630 "\n",
1631 "You should have received a copy of the GNU General Public License\n",
1632 "along with GCC; see the file COPYING3. If not see\n",
1633 "<http://www.gnu.org/licenses/>. */\n",
1634 "\n",
1635 "/* This file is machine generated. Do not edit. */\n"
1637 outf_p f;
1638 size_t i;
1640 gcc_assert (name != NULL);
1641 gcc_assert (oname != NULL);
1642 f = XCNEW (struct outf);
1643 f->next = output_files;
1644 f->name = oname;
1645 output_files = f;
1647 oprintf (f, "/* Type information for %s.\n", name);
1648 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1649 oprintf (f, "%s", hdr[i]);
1650 return f;
1653 /* Print, like fprintf, to O.
1654 N.B. You might think this could be implemented more efficiently
1655 with vsnprintf(). Unfortunately, there are C libraries that
1656 provide that function but without the C99 semantics for its return
1657 value, making it impossible to know how much space is required. */
1658 void
1659 oprintf (outf_p o, const char *format, ...)
1661 char *s;
1662 size_t slength;
1663 va_list ap;
1665 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1666 in that case. */
1667 if (!o)
1668 return;
1670 va_start (ap, format);
1671 slength = vasprintf (&s, format, ap);
1672 if (s == NULL || (int) slength < 0)
1673 fatal ("out of memory");
1674 va_end (ap);
1676 if (o->bufused + slength > o->buflength)
1678 size_t new_len = o->buflength;
1679 if (new_len == 0)
1680 new_len = 1024;
1683 new_len *= 2;
1685 while (o->bufused + slength >= new_len);
1686 o->buf = XRESIZEVEC (char, o->buf, new_len);
1687 o->buflength = new_len;
1689 memcpy (o->buf + o->bufused, s, slength);
1690 o->bufused += slength;
1691 free (s);
1694 /* Open the global header file and the language-specific header files. */
1696 static void
1697 open_base_files (void)
1699 size_t i;
1701 if (nb_plugin_files > 0 && plugin_files)
1702 return;
1704 header_file = create_file ("GCC", "gtype-desc.h");
1706 base_files = XNEWVEC (outf_p, num_lang_dirs);
1708 for (i = 0; i < num_lang_dirs; i++)
1709 base_files[i] = create_file (lang_dir_names[i],
1710 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1712 /* gtype-desc.c is a little special, so we create it here. */
1714 /* The order of files here matters very much. */
1715 static const char *const ifiles[] = {
1716 "config.h", "system.h", "coretypes.h", "tm.h",
1717 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1718 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1719 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1720 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1721 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1722 "except.h", "output.h", "gimple.h", "cfgloop.h",
1723 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1724 "ipa-inline.h", "dwarf2out.h", NULL
1726 const char *const *ifp;
1727 outf_p gtype_desc_c;
1729 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1730 for (ifp = ifiles; *ifp; ifp++)
1731 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1733 /* Make sure we handle "cfun" specially. */
1734 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1735 oprintf (gtype_desc_c, "#undef cfun\n");
1739 /* For INPF an input file, return the real basename of INPF, with all
1740 the directory components skipped. */
1742 static const char *
1743 get_file_realbasename (const input_file *inpf)
1745 return lbasename (get_input_file_name (inpf));
1748 /* For INPF a filename, return the relative path to INPF from
1749 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1751 const char *
1752 get_file_srcdir_relative_path (const input_file *inpf)
1754 const char *f = get_input_file_name (inpf);
1755 if (strlen (f) > srcdir_len
1756 && IS_DIR_SEPARATOR (f[srcdir_len])
1757 && strncmp (f, srcdir, srcdir_len) == 0)
1758 return f + srcdir_len + 1;
1759 else
1760 return NULL;
1763 /* For INPF an input_file, return the relative path to INPF from
1764 $(srcdir) if the latter is a prefix in INPF, or the real basename
1765 of INPF otherwise. */
1767 static const char *
1768 get_file_basename (const input_file *inpf)
1770 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1772 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1775 /* For F a filename, return the lang_dir_names relative index of the language
1776 directory that is a prefix in F, if any, -1 otherwise. */
1778 static int
1779 get_prefix_langdir_index (const char *f)
1781 size_t f_len = strlen (f);
1782 size_t lang_index;
1784 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1786 const char *langdir = lang_dir_names[lang_index];
1787 size_t langdir_len = strlen (langdir);
1789 if (f_len > langdir_len
1790 && IS_DIR_SEPARATOR (f[langdir_len])
1791 && memcmp (f, langdir, langdir_len) == 0)
1792 return lang_index;
1795 return -1;
1798 /* For INPF an input file, return the name of language directory where
1799 F is located, if any, NULL otherwise. */
1801 static const char *
1802 get_file_langdir (const input_file *inpf)
1804 /* Get the relative path to INPF from $(srcdir) and find the
1805 language by comparing the prefix with language directory names.
1806 If INPF is not even srcdir relative, no point in looking
1807 further. */
1809 int lang_index;
1810 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1811 const char *r;
1813 if (!srcdir_relative_path)
1814 return NULL;
1816 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1817 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1818 r = "c-family";
1819 else if (lang_index >= 0)
1820 r = lang_dir_names[lang_index];
1821 else
1822 r = NULL;
1824 return r;
1827 /* The gt- output file name for INPF. */
1829 static const char *
1830 get_file_gtfilename (const input_file *inpf)
1832 /* Cook up an initial version of the gt- file name from the file real
1833 basename and the language name, if any. */
1835 const char *basename = get_file_realbasename (inpf);
1836 const char *langdir = get_file_langdir (inpf);
1838 char *result =
1839 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1840 : xasprintf ("gt-%s", basename));
1842 /* Then replace all non alphanumerics characters by '-' and change the
1843 extension to ".h". We expect the input filename extension was at least
1844 one character long. */
1846 char *s = result;
1848 for (; *s != '.'; s++)
1849 if (!ISALNUM (*s) && *s != '-')
1850 *s = '-';
1852 memcpy (s, ".h", sizeof (".h"));
1854 return result;
1857 /* Each input_file has its associated output file outf_p. The
1858 association is computed by the function
1859 get_output_file_with_visibility. The associated file is cached
1860 inside input_file in its inpoutf field, so is really computed only
1861 once. Associated output file paths (i.e. output_name-s) are
1862 computed by a rule based regexp machinery, using the files_rules
1863 array of struct file_rule_st. A for_name is also computed, giving
1864 the source file name for which the output_file is generated; it is
1865 often the last component of the input_file path. */
1869 Regexpr machinery to compute the output_name and for_name-s of each
1870 input_file. We have a sequence of file rules which gives the POSIX
1871 extended regular expression to match an input file path, and two
1872 transformed strings for the corresponding output_name and the
1873 corresponding for_name. The transformed string contain dollars: $0
1874 is replaced by the entire match, $1 is replaced by the substring
1875 matching the first parenthesis in the regexp, etc. And $$ is replaced
1876 by a single verbatim dollar. The rule order is important. The
1877 general case is last, and the particular cases should come before.
1878 An action routine can, when needed, update the out_name & for_name
1879 and/or return the appropriate output file. It is invoked only when a
1880 rule is triggered. When a rule is triggered, the output_name and
1881 for_name are computed using their transform string in while $$, $0,
1882 $1, ... are suitably replaced. If there is an action, it is called.
1883 In some few cases, the action can directly return the outf_p, but
1884 usually it just updates the output_name and for_name so should free
1885 them before replacing them. The get_output_file_with_visibility
1886 function creates an outf_p only once per each output_name, so it
1887 scans the output_files list for previously seen output file names.
1890 /* Signature of actions in file rules. */
1891 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1894 struct file_rule_st {
1895 const char* frul_srcexpr; /* Source string for regexp. */
1896 int frul_rflags; /* Flags passed to regcomp, usually
1897 * REG_EXTENDED. */
1898 regex_t* frul_re; /* Compiled regular expression
1899 obtained by regcomp. */
1900 const char* frul_tr_out; /* Transformation string for making
1901 * the output_name, with $1 ... $9 for
1902 * subpatterns and $0 for the whole
1903 * matched filename. */
1904 const char* frul_tr_for; /* Tranformation string for making the
1905 for_name. */
1906 frul_actionrout_t* frul_action; /* The action, if non null, is
1907 * called once the rule matches, on
1908 * the transformed out_name &
1909 * for_name. It could change them
1910 * and/or give the output file. */
1913 /* File rule action handling *.h files. */
1914 static outf_p header_dot_h_frul (input_file*, char**, char**);
1916 /* File rule action handling *.c files. */
1917 static outf_p source_dot_c_frul (input_file*, char**, char**);
1919 #define NULL_REGEX (regex_t*)0
1921 /* The prefix in our regexp-s matching the directory. */
1922 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1924 #define NULL_FRULACT (frul_actionrout_t*)0
1926 /* The array of our rules governing file name generation. Rules order
1927 matters, so change with extreme care! */
1929 struct file_rule_st files_rules[] = {
1930 /* The general rule assumes that files in subdirectories belong to a
1931 particular front-end, and files not in subdirectories are shared.
1932 The following rules deal with exceptions - files that are in
1933 subdirectories and yet are shared, and files that are top-level,
1934 but are not shared. */
1936 /* the c-family/ source directory is special. */
1937 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1938 REG_EXTENDED, NULL_REGEX,
1939 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1941 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1942 REG_EXTENDED, NULL_REGEX,
1943 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1945 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1946 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1947 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1949 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1950 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1952 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1953 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1954 REG_EXTENDED, NULL_REGEX,
1955 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1957 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1958 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1959 REG_EXTENDED, NULL_REGEX,
1960 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1962 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1963 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1964 REG_EXTENDED, NULL_REGEX,
1965 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1967 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1968 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1969 REG_EXTENDED, NULL_REGEX,
1970 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1972 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1973 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1974 REG_EXTENDED, NULL_REGEX,
1975 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1977 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1978 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1979 REG_EXTENDED, NULL_REGEX,
1980 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
1982 /* General cases. For header *.h and source *.c files, we need
1983 * special actions to handle the language. */
1985 /* Source *.c files are using get_file_gtfilename to compute their
1986 output_name and get_file_basename to compute their for_name
1987 through the source_dot_c_frul action. */
1988 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
1989 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
1990 /* Common header files get "gtype-desc.c" as their output_name,
1991 * while language specific header files are handled specially. So
1992 * we need the header_dot_h_frul action. */
1993 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
1994 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
1996 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
1997 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
1999 /* Mandatory null last entry signaling end of rules. */
2000 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2003 /* Special file rules action for handling *.h header files. It gives
2004 "gtype-desc.c" for common headers and corresponding output
2005 files for language-specific header files. */
2006 static outf_p
2007 header_dot_h_frul (input_file* inpf, char**poutname,
2008 char**pforname ATTRIBUTE_UNUSED)
2010 const char *basename = 0;
2011 int lang_index = 0;
2012 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2013 (void*) inpf, get_input_file_name (inpf),
2014 *poutname, *pforname);
2015 basename = get_file_basename (inpf);
2016 lang_index = get_prefix_langdir_index (basename);
2017 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2019 if (lang_index >= 0)
2021 /* The header is language specific. Given output_name &
2022 for_name remains unchanged. The base_files array gives the
2023 outf_p. */
2024 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2025 (void*) base_files[lang_index],
2026 (base_files[lang_index])->name);
2027 return base_files[lang_index];
2029 else
2031 /* The header is common to all front-end languages. So
2032 output_name is "gtype-desc.c" file. The calling function
2033 get_output_file_with_visibility will find its outf_p. */
2034 free (*poutname);
2035 *poutname = xstrdup ("gtype-desc.c");
2036 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2037 get_input_file_name (inpf));
2038 return NULL;
2043 /* Special file rules action for handling *.c source files using
2044 * get_file_gtfilename to compute their output_name and
2045 * get_file_basename to compute their for_name. The output_name is
2046 * gt-<LANG>-<BASE>.h for language specific source files, and
2047 * gt-<BASE>.h for common source files. */
2048 static outf_p
2049 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2051 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2052 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2053 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2054 (void*) inpf, get_input_file_name (inpf),
2055 *poutname, *pforname);
2056 DBGPRINTF ("newoutname %s", newoutname);
2057 DBGPRINTF ("newbasename %s", newbasename);
2058 free (*poutname);
2059 free (*pforname);
2060 *poutname = newoutname;
2061 *pforname = newbasename;
2062 return NULL;
2065 /* Utility function for get_output_file_with_visibility which returns
2066 * a malloc-ed substituted string using TRS on matching of the FILNAM
2067 * file name, using the PMATCH array. */
2068 static char*
2069 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2070 const char *trs)
2072 struct obstack str_obstack;
2073 char *str = NULL;
2074 char *rawstr = NULL;
2075 const char *pt = NULL;
2076 DBGPRINTF ("filnam %s", filnam);
2077 obstack_init (&str_obstack);
2078 for (pt = trs; *pt; pt++) {
2079 char c = *pt;
2080 if (c == '$')
2082 if (pt[1] == '$')
2084 /* A double dollar $$ is substituted by a single verbatim
2085 dollar, but who really uses dollar signs in file
2086 paths? */
2087 obstack_1grow (&str_obstack, '$');
2089 else if (ISDIGIT (pt[1]))
2091 /* Handle $0 $1 ... $9 by appropriate substitution. */
2092 int dolnum = pt[1] - '0';
2093 int so = pmatch[dolnum].rm_so;
2094 int eo = pmatch[dolnum].rm_eo;
2095 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2096 if (so>=0 && eo>=so)
2097 obstack_grow (&str_obstack, filnam + so, eo - so);
2099 else
2101 /* This can happen only when files_rules is buggy! */
2102 gcc_unreachable();
2104 /* Always skip the character after the dollar. */
2105 pt++;
2107 else
2108 obstack_1grow (&str_obstack, c);
2110 obstack_1grow (&str_obstack, '\0');
2111 rawstr = XOBFINISH (&str_obstack, char *);
2112 str = xstrdup (rawstr);
2113 obstack_free (&str_obstack, NULL);
2114 DBGPRINTF ("matched replacement %s", str);
2115 rawstr = NULL;
2116 return str;
2120 /* An output file, suitable for definitions, that can see declarations
2121 made in INPF and is linked into every language that uses INPF.
2122 Since the result is cached inside INPF, that argument cannot be
2123 declared constant, but is "almost" constant. */
2125 outf_p
2126 get_output_file_with_visibility (input_file *inpf)
2128 outf_p r;
2129 char *for_name = NULL;
2130 char *output_name = NULL;
2131 const char* inpfname;
2133 /* This can happen when we need a file with visibility on a
2134 structure that we've never seen. We have to just hope that it's
2135 globally visible. */
2136 if (inpf == NULL)
2137 inpf = system_h_file;
2139 /* The result is cached in INPF, so return it if already known. */
2140 if (inpf->inpoutf)
2141 return inpf->inpoutf;
2143 /* In plugin mode, return NULL unless the input_file is one of the
2144 plugin_files. */
2145 if (plugin_files)
2147 size_t i;
2148 for (i = 0; i < nb_plugin_files; i++)
2149 if (inpf == plugin_files[i])
2151 inpf->inpoutf = plugin_output;
2152 return plugin_output;
2155 return NULL;
2158 inpfname = get_input_file_name (inpf);
2160 /* Try each rule in sequence in files_rules until one is triggered. */
2162 int rulix = 0;
2163 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2164 (void*) inpf, inpfname);
2166 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2168 DBGPRINTF ("rulix#%d srcexpr %s",
2169 rulix, files_rules[rulix].frul_srcexpr);
2171 if (!files_rules[rulix].frul_re)
2173 /* Compile the regexpr lazily. */
2174 int err = 0;
2175 files_rules[rulix].frul_re = XCNEW (regex_t);
2176 err = regcomp (files_rules[rulix].frul_re,
2177 files_rules[rulix].frul_srcexpr,
2178 files_rules[rulix].frul_rflags);
2179 if (err)
2181 /* The regular expression compilation fails only when
2182 file_rules is buggy. */
2183 gcc_unreachable ();
2187 output_name = NULL;
2188 for_name = NULL;
2190 /* Match the regexpr and trigger the rule if matched. */
2192 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2193 $3, ... $9. */
2194 regmatch_t pmatch[10];
2195 memset (pmatch, 0, sizeof (pmatch));
2196 if (!regexec (files_rules[rulix].frul_re,
2197 inpfname, 10, pmatch, 0))
2199 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2200 (void*) inpf, inpfname, rulix,
2201 files_rules[rulix].frul_srcexpr);
2202 for_name =
2203 matching_file_name_substitute (inpfname, pmatch,
2204 files_rules[rulix].frul_tr_for);
2205 DBGPRINTF ("for_name %s", for_name);
2206 output_name =
2207 matching_file_name_substitute (inpfname, pmatch,
2208 files_rules[rulix].frul_tr_out);
2209 DBGPRINTF ("output_name %s", output_name);
2210 if (files_rules[rulix].frul_action)
2212 /* Invoke our action routine. */
2213 outf_p of = NULL;
2214 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2215 rulix, output_name, for_name);
2216 of =
2217 (files_rules[rulix].frul_action) (inpf,
2218 &output_name, &for_name);
2219 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2220 rulix, (void*)of, output_name, for_name);
2221 /* If the action routine returned something, give it back
2222 immediately and cache it in inpf. */
2223 if (of)
2225 inpf->inpoutf = of;
2226 return of;
2229 /* The rule matched, and had no action, or that action did
2230 not return any output file but could have changed the
2231 output_name or for_name. We break out of the loop on the
2232 files_rules. */
2233 break;
2235 else
2237 /* The regexpr did not match. */
2238 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2239 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2240 continue;
2245 if (!output_name || !for_name)
2247 /* This is impossible, and could only happen if the files_rules is
2248 incomplete or buggy. */
2249 gcc_unreachable ();
2252 /* Look through to see if we've ever seen this output filename
2253 before. If found, cache the result in inpf. */
2254 for (r = output_files; r; r = r->next)
2255 if (filename_cmp (r->name, output_name) == 0)
2257 inpf->inpoutf = r;
2258 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2259 output_name, for_name);
2260 return r;
2263 /* If not found, create it, and cache it in inpf. */
2264 r = create_file (for_name, output_name);
2266 gcc_assert (r && r->name);
2267 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2268 output_name, for_name);
2269 inpf->inpoutf = r;
2270 return r;
2275 /* The name of an output file, suitable for definitions, that can see
2276 declarations made in INPF and is linked into every language that
2277 uses INPF. */
2279 const char *
2280 get_output_file_name (input_file* inpf)
2282 outf_p o = get_output_file_with_visibility (inpf);
2283 if (o)
2284 return o->name;
2285 return NULL;
2288 /* Check if existing file is equal to the in memory buffer. */
2290 static bool
2291 is_file_equal (outf_p of)
2293 FILE *newfile = fopen (of->name, "r");
2294 size_t i;
2295 bool equal;
2296 if (newfile == NULL)
2297 return false;
2299 equal = true;
2300 for (i = 0; i < of->bufused; i++)
2302 int ch;
2303 ch = fgetc (newfile);
2304 if (ch == EOF || ch != (unsigned char) of->buf[i])
2306 equal = false;
2307 break;
2310 fclose (newfile);
2311 return equal;
2314 /* Copy the output to its final destination,
2315 but don't unnecessarily change modification times. */
2317 static void
2318 close_output_files (void)
2320 int nbwrittenfiles = 0;
2321 outf_p of;
2323 for (of = output_files; of; of = of->next)
2325 if (!is_file_equal (of))
2327 FILE *newfile = NULL;
2328 char *backupname = NULL;
2329 /* Back up the old version of the output file gt-FOO.c as
2330 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2331 if (backup_dir)
2333 backupname = concat (backup_dir, "/",
2334 lbasename (of->name), "~", NULL);
2335 if (!access (of->name, F_OK) && rename (of->name, backupname))
2336 fatal ("failed to back up %s as %s: %s",
2337 of->name, backupname, xstrerror (errno));
2340 newfile = fopen (of->name, "w");
2341 if (newfile == NULL)
2342 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2343 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2344 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2345 if (fclose (newfile) != 0)
2346 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2347 nbwrittenfiles++;
2348 if (verbosity_level >= 2 && backupname)
2349 printf ("%s wrote #%-3d %s backed-up in %s\n",
2350 progname, nbwrittenfiles, of->name, backupname);
2351 else if (verbosity_level >= 1)
2352 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2353 free (backupname);
2355 else
2357 /* output file remains unchanged. */
2358 if (verbosity_level >= 2)
2359 printf ("%s keep %s\n", progname, of->name);
2361 free (of->buf);
2362 of->buf = NULL;
2363 of->bufused = of->buflength = 0;
2365 if (verbosity_level >= 1)
2366 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2369 struct flist
2371 struct flist *next;
2372 int started_p;
2373 const input_file* file;
2374 outf_p f;
2377 struct walk_type_data;
2379 /* For scalars and strings, given the item in 'val'.
2380 For structures, given a pointer to the item in 'val'.
2381 For misc. pointers, given the item in 'val'.
2383 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2384 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2386 /* Parameters for write_types. */
2388 struct write_types_data
2390 const char *prefix;
2391 const char *param_prefix;
2392 const char *subfield_marker_routine;
2393 const char *marker_routine;
2394 const char *reorder_note_routine;
2395 const char *comment;
2396 int skip_hooks; /* skip hook generation if non zero */
2399 static void output_escaped_param (struct walk_type_data *d,
2400 const char *, const char *);
2401 static void output_mangled_typename (outf_p, const_type_p);
2402 static void walk_type (type_p t, struct walk_type_data *d);
2403 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2404 const struct write_types_data *wtd);
2405 static void write_types_process_field
2406 (type_p f, const struct walk_type_data *d);
2407 static void write_types (outf_p output_header,
2408 type_p structures,
2409 type_p param_structs,
2410 const struct write_types_data *wtd);
2411 static void write_types_local_process_field
2412 (type_p f, const struct walk_type_data *d);
2413 static void write_local_func_for_structure
2414 (const_type_p orig_s, type_p s, type_p *param);
2415 static void write_local (outf_p output_header,
2416 type_p structures, type_p param_structs);
2417 static void write_enum_defn (type_p structures, type_p param_structs);
2418 static int contains_scalar_p (type_p t);
2419 static void put_mangled_filename (outf_p, const input_file *);
2420 static void finish_root_table (struct flist *flp, const char *pfx,
2421 const char *tname, const char *lastname,
2422 const char *name);
2423 static void write_root (outf_p, pair_p, type_p, const char *, int,
2424 struct fileloc *, const char *, bool);
2425 static void write_array (outf_p f, pair_p v,
2426 const struct write_types_data *wtd);
2427 static void write_roots (pair_p, bool);
2429 /* Parameters for walk_type. */
2431 struct walk_type_data
2433 process_field_fn process_field;
2434 const void *cookie;
2435 outf_p of;
2436 options_p opt;
2437 const char *val;
2438 const char *prev_val[4];
2439 int indent;
2440 int counter;
2441 const struct fileloc *line;
2442 lang_bitmap bitmap;
2443 type_p *param;
2444 int used_length;
2445 type_p orig_s;
2446 const char *reorder_fn;
2447 bool needs_cast_p;
2448 bool fn_wants_lvalue;
2449 bool in_record_p;
2450 int loopcounter;
2451 bool in_ptr_field;
2452 bool have_this_obj;
2456 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2457 pre-processor identifier to use in a #define directive. This replaces
2458 special characters used in C++ identifiers like '>', '<' and ':' with
2459 '_'.
2461 If no C++ special characters are found in TYPE_NAME, return
2462 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2463 characters replaced with '_'. In this case, the caller is
2464 responsible for freeing the allocated string. */
2466 static const char *
2467 filter_type_name (const char *type_name)
2469 if (strchr (type_name, '<') || strchr (type_name, ':'))
2471 size_t i;
2472 char *s = xstrdup (type_name);
2473 for (i = 0; i < strlen (s); i++)
2474 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ',')
2475 s[i] = '_';
2476 return s;
2478 else
2479 return type_name;
2483 /* Print a mangled name representing T to OF. */
2485 static void
2486 output_mangled_typename (outf_p of, const_type_p t)
2488 if (t == NULL)
2489 oprintf (of, "Z");
2490 else
2491 switch (t->kind)
2493 case TYPE_NONE:
2494 case TYPE_UNDEFINED:
2495 gcc_unreachable ();
2496 break;
2497 case TYPE_POINTER:
2498 oprintf (of, "P");
2499 output_mangled_typename (of, t->u.p);
2500 break;
2501 case TYPE_SCALAR:
2502 oprintf (of, "I");
2503 break;
2504 case TYPE_STRING:
2505 oprintf (of, "S");
2506 break;
2507 case TYPE_STRUCT:
2508 case TYPE_UNION:
2509 case TYPE_LANG_STRUCT:
2510 case TYPE_USER_STRUCT:
2512 const char *id_for_tag = filter_type_name (t->u.s.tag);
2513 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2514 id_for_tag);
2515 if (id_for_tag != t->u.s.tag)
2516 free (CONST_CAST(char *, id_for_tag));
2518 break;
2519 case TYPE_PARAM_STRUCT:
2521 int i;
2522 for (i = 0; i < NUM_PARAM; i++)
2523 if (t->u.param_struct.param[i] != NULL)
2524 output_mangled_typename (of, t->u.param_struct.param[i]);
2525 output_mangled_typename (of, t->u.param_struct.stru);
2527 break;
2528 case TYPE_ARRAY:
2529 gcc_unreachable ();
2533 /* Print PARAM to D->OF processing escapes. D->VAL references the
2534 current object, D->PREV_VAL the object containing the current
2535 object, ONAME is the name of the option and D->LINE is used to
2536 print error messages. */
2538 static void
2539 output_escaped_param (struct walk_type_data *d, const char *param,
2540 const char *oname)
2542 const char *p;
2544 for (p = param; *p; p++)
2545 if (*p != '%')
2546 oprintf (d->of, "%c", *p);
2547 else
2548 switch (*++p)
2550 case 'h':
2551 oprintf (d->of, "(%s)", d->prev_val[2]);
2552 break;
2553 case '0':
2554 oprintf (d->of, "(%s)", d->prev_val[0]);
2555 break;
2556 case '1':
2557 oprintf (d->of, "(%s)", d->prev_val[1]);
2558 break;
2559 case 'a':
2561 const char *pp = d->val + strlen (d->val);
2562 while (pp[-1] == ']')
2563 while (*pp != '[')
2564 pp--;
2565 oprintf (d->of, "%s", pp);
2567 break;
2568 default:
2569 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2570 oname, '%', *p);
2575 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2576 which is of type T. Write code to D->OF to constrain execution (at
2577 the point that D->PROCESS_FIELD is called) to the appropriate
2578 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2579 pointers to those objects. D->PREV_VAL lists the objects
2580 containing the current object, D->OPT is a list of options to
2581 apply, D->INDENT is the current indentation level, D->LINE is used
2582 to print error messages, D->BITMAP indicates which languages to
2583 print the structure for, and D->PARAM is the current parameter
2584 (from an enclosing param_is option). */
2586 static void
2587 walk_type (type_p t, struct walk_type_data *d)
2589 const char *length = NULL;
2590 const char *desc = NULL;
2591 int maybe_undef_p = 0;
2592 int use_param_num = -1;
2593 int use_params_p = 0;
2594 int atomic_p = 0;
2595 options_p oo;
2596 const struct nested_ptr_data *nested_ptr_d = NULL;
2598 d->needs_cast_p = false;
2599 for (oo = d->opt; oo; oo = oo->next)
2600 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2601 length = oo->info.string;
2602 else if (strcmp (oo->name, "maybe_undef") == 0)
2603 maybe_undef_p = 1;
2604 else if (strncmp (oo->name, "use_param", 9) == 0
2605 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2606 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2607 else if (strcmp (oo->name, "use_params") == 0)
2608 use_params_p = 1;
2609 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2610 desc = oo->info.string;
2611 else if (strcmp (oo->name, "mark_hook") == 0)
2613 else if (strcmp (oo->name, "nested_ptr") == 0
2614 && oo->kind == OPTION_NESTED)
2615 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2616 else if (strcmp (oo->name, "dot") == 0)
2618 else if (strcmp (oo->name, "tag") == 0)
2620 else if (strcmp (oo->name, "special") == 0)
2622 else if (strcmp (oo->name, "skip") == 0)
2624 else if (strcmp (oo->name, "atomic") == 0)
2625 atomic_p = 1;
2626 else if (strcmp (oo->name, "default") == 0)
2628 else if (strcmp (oo->name, "param_is") == 0)
2630 else if (strncmp (oo->name, "param", 5) == 0
2631 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2633 else if (strcmp (oo->name, "chain_next") == 0)
2635 else if (strcmp (oo->name, "chain_prev") == 0)
2637 else if (strcmp (oo->name, "chain_circular") == 0)
2639 else if (strcmp (oo->name, "reorder") == 0)
2641 else if (strcmp (oo->name, "variable_size") == 0)
2643 else
2644 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2646 if (d->used_length)
2647 length = NULL;
2649 if (use_params_p)
2651 int pointer_p = t->kind == TYPE_POINTER;
2653 if (pointer_p)
2654 t = t->u.p;
2655 if (!union_or_struct_p (t))
2656 error_at_line (d->line, "`use_params' option on unimplemented type");
2657 else
2658 t = find_param_structure (t, d->param);
2659 if (pointer_p)
2660 t = create_pointer (t);
2663 if (use_param_num != -1)
2665 if (d->param != NULL && d->param[use_param_num] != NULL)
2667 type_p nt = d->param[use_param_num];
2669 if (t->kind == TYPE_ARRAY)
2670 nt = create_array (nt, t->u.a.len);
2671 else if (length != NULL && t->kind == TYPE_POINTER)
2672 nt = create_pointer (nt);
2673 d->needs_cast_p = (t->kind != TYPE_POINTER
2674 && (nt->kind == TYPE_POINTER
2675 || nt->kind == TYPE_STRING));
2676 t = nt;
2678 else
2679 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2682 if (maybe_undef_p
2683 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2685 error_at_line (d->line,
2686 "field `%s' has invalid option `maybe_undef_p'\n",
2687 d->val);
2688 return;
2691 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2693 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2694 return;
2697 switch (t->kind)
2699 case TYPE_SCALAR:
2700 case TYPE_STRING:
2701 d->process_field (t, d);
2702 break;
2704 case TYPE_POINTER:
2706 d->in_ptr_field = true;
2707 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2709 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2710 break;
2713 /* If a pointer type is marked as "atomic", we process the
2714 field itself, but we don't walk the data that they point to.
2716 There are two main cases where we walk types: to mark
2717 pointers that are reachable, and to relocate pointers when
2718 writing a PCH file. In both cases, an atomic pointer is
2719 itself marked or relocated, but the memory that it points
2720 to is left untouched. In the case of PCH, that memory will
2721 be read/written unchanged to the PCH file. */
2722 if (atomic_p)
2724 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2725 d->indent += 2;
2726 d->process_field (t, d);
2727 d->indent -= 2;
2728 oprintf (d->of, "%*s}\n", d->indent, "");
2729 break;
2732 if (!length)
2734 if (!union_or_struct_p (t->u.p)
2735 && t->u.p->kind != TYPE_PARAM_STRUCT)
2737 error_at_line (d->line,
2738 "field `%s' is pointer to unimplemented type",
2739 d->val);
2740 break;
2743 if (nested_ptr_d)
2745 const char *oldprevval2 = d->prev_val[2];
2747 if (!union_or_struct_p (nested_ptr_d->type))
2749 error_at_line (d->line,
2750 "field `%s' has invalid "
2751 "option `nested_ptr'\n", d->val);
2752 return;
2755 d->prev_val[2] = d->val;
2756 oprintf (d->of, "%*s{\n", d->indent, "");
2757 d->indent += 2;
2758 d->val = xasprintf ("x%d", d->counter++);
2759 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2760 (nested_ptr_d->type->kind == TYPE_UNION
2761 ? "union" : "struct"),
2762 nested_ptr_d->type->u.s.tag,
2763 d->fn_wants_lvalue ? "" : "const ", d->val);
2764 oprintf (d->of, "%*s", d->indent + 2, "");
2765 output_escaped_param (d, nested_ptr_d->convert_from,
2766 "nested_ptr");
2767 oprintf (d->of, ";\n");
2769 d->process_field (nested_ptr_d->type, d);
2771 if (d->fn_wants_lvalue)
2773 oprintf (d->of, "%*s%s = ", d->indent, "",
2774 d->prev_val[2]);
2775 d->prev_val[2] = d->val;
2776 output_escaped_param (d, nested_ptr_d->convert_to,
2777 "nested_ptr");
2778 oprintf (d->of, ";\n");
2781 d->indent -= 2;
2782 oprintf (d->of, "%*s}\n", d->indent, "");
2783 d->val = d->prev_val[2];
2784 d->prev_val[2] = oldprevval2;
2786 else
2787 d->process_field (t->u.p, d);
2789 else
2791 int loopcounter = d->loopcounter;
2792 const char *oldval = d->val;
2793 const char *oldprevval3 = d->prev_val[3];
2794 char *newval;
2796 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2797 d->indent += 2;
2798 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2799 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2800 "", loopcounter, loopcounter);
2801 if (!d->in_record_p)
2802 output_escaped_param (d, length, "length");
2803 else
2804 oprintf (d->of, "l%d", loopcounter);
2805 if (d->have_this_obj)
2806 /* Try to unswitch loops (see PR53880). */
2807 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2808 oprintf (d->of, "); i%d++) {\n", loopcounter);
2809 d->indent += 2;
2810 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2811 d->used_length = 1;
2812 d->prev_val[3] = oldval;
2813 walk_type (t->u.p, d);
2814 free (newval);
2815 d->val = oldval;
2816 d->prev_val[3] = oldprevval3;
2817 d->used_length = 0;
2818 d->indent -= 2;
2819 oprintf (d->of, "%*s}\n", d->indent, "");
2820 d->process_field (t, d);
2821 d->indent -= 2;
2822 oprintf (d->of, "%*s}\n", d->indent, "");
2824 d->in_ptr_field = false;
2826 break;
2828 case TYPE_ARRAY:
2830 int loopcounter;
2831 const char *oldval = d->val;
2832 char *newval;
2834 /* If it's an array of scalars, we optimize by not generating
2835 any code. */
2836 if (t->u.a.p->kind == TYPE_SCALAR)
2837 break;
2839 if (length)
2840 loopcounter = d->loopcounter;
2841 else
2842 loopcounter = d->counter++;
2844 /* When walking an array, compute the length and store it in a
2845 local variable before walking the array elements, instead of
2846 recomputing the length expression each time through the loop.
2847 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2848 where the length is stored in the first array element,
2849 because otherwise that operand can get overwritten on the
2850 first iteration. */
2851 oprintf (d->of, "%*s{\n", d->indent, "");
2852 d->indent += 2;
2853 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2854 if (!d->in_record_p || !length)
2856 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2857 d->indent, "", loopcounter);
2858 if (length)
2859 output_escaped_param (d, length, "length");
2860 else
2861 oprintf (d->of, "%s", t->u.a.len);
2862 oprintf (d->of, ");\n");
2865 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2866 d->indent, "",
2867 loopcounter, loopcounter, loopcounter, loopcounter);
2868 d->indent += 2;
2869 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2870 d->used_length = 1;
2871 walk_type (t->u.a.p, d);
2872 free (newval);
2873 d->used_length = 0;
2874 d->val = oldval;
2875 d->indent -= 2;
2876 oprintf (d->of, "%*s}\n", d->indent, "");
2877 d->indent -= 2;
2878 oprintf (d->of, "%*s}\n", d->indent, "");
2880 break;
2882 case TYPE_STRUCT:
2883 case TYPE_UNION:
2885 pair_p f;
2886 const char *oldval = d->val;
2887 const char *oldprevval1 = d->prev_val[1];
2888 const char *oldprevval2 = d->prev_val[2];
2889 const char *struct_mark_hook = NULL;
2890 const int union_p = t->kind == TYPE_UNION;
2891 int seen_default_p = 0;
2892 options_p o;
2893 int lengths_seen = 0;
2894 int endcounter;
2895 bool any_length_seen = false;
2897 if (!t->u.s.line.file)
2898 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2900 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2902 error_at_line (d->line,
2903 "structure `%s' defined for mismatching languages",
2904 t->u.s.tag);
2905 error_at_line (&t->u.s.line, "one structure defined here");
2908 /* Some things may also be defined in the structure's options. */
2909 for (o = t->u.s.opt; o; o = o->next)
2910 if (!desc && strcmp (o->name, "desc") == 0
2911 && o->kind == OPTION_STRING)
2912 desc = o->info.string;
2913 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2914 && o->kind == OPTION_STRING)
2915 struct_mark_hook = o->info.string;
2917 if (struct_mark_hook)
2918 oprintf (d->of, "%*s%s (&%s);\n",
2919 d->indent, "", struct_mark_hook, oldval);
2921 d->prev_val[2] = oldval;
2922 d->prev_val[1] = oldprevval2;
2923 if (union_p)
2925 if (desc == NULL)
2927 error_at_line (d->line,
2928 "missing `desc' option for union `%s'",
2929 t->u.s.tag);
2930 desc = "1";
2932 oprintf (d->of, "%*sswitch (", d->indent, "");
2933 output_escaped_param (d, desc, "desc");
2934 oprintf (d->of, ")\n");
2935 d->indent += 2;
2936 oprintf (d->of, "%*s{\n", d->indent, "");
2939 for (f = t->u.s.fields; f; f = f->next)
2941 options_p oo;
2942 int skip_p = 0;
2943 const char *fieldlength = NULL;
2945 d->reorder_fn = NULL;
2946 for (oo = f->opt; oo; oo = oo->next)
2947 if (strcmp (oo->name, "skip") == 0)
2948 skip_p = 1;
2949 else if (strcmp (oo->name, "length") == 0
2950 && oo->kind == OPTION_STRING)
2951 fieldlength = oo->info.string;
2953 if (skip_p)
2954 continue;
2955 if (fieldlength)
2957 lengths_seen++;
2958 d->counter++;
2959 if (!union_p)
2961 if (!any_length_seen)
2963 oprintf (d->of, "%*s{\n", d->indent, "");
2964 d->indent += 2;
2966 any_length_seen = true;
2968 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2969 d->indent, "", d->counter - 1);
2970 output_escaped_param (d, fieldlength, "length");
2971 oprintf (d->of, ");\n");
2975 endcounter = d->counter;
2977 for (f = t->u.s.fields; f; f = f->next)
2979 options_p oo;
2980 const char *dot = ".";
2981 const char *tagid = NULL;
2982 int skip_p = 0;
2983 int default_p = 0;
2984 int use_param_p = 0;
2985 const char *fieldlength = NULL;
2986 char *newval;
2988 d->reorder_fn = NULL;
2989 for (oo = f->opt; oo; oo = oo->next)
2990 if (strcmp (oo->name, "dot") == 0
2991 && oo->kind == OPTION_STRING)
2992 dot = oo->info.string;
2993 else if (strcmp (oo->name, "tag") == 0
2994 && oo->kind == OPTION_STRING)
2995 tagid = oo->info.string;
2996 else if (strcmp (oo->name, "skip") == 0)
2997 skip_p = 1;
2998 else if (strcmp (oo->name, "default") == 0)
2999 default_p = 1;
3000 else if (strcmp (oo->name, "reorder") == 0
3001 && oo->kind == OPTION_STRING)
3002 d->reorder_fn = oo->info.string;
3003 else if (strncmp (oo->name, "use_param", 9) == 0
3004 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3005 use_param_p = 1;
3006 else if (strcmp (oo->name, "length") == 0
3007 && oo->kind == OPTION_STRING)
3008 fieldlength = oo->info.string;
3010 if (skip_p)
3011 continue;
3013 if (union_p && tagid)
3015 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3016 d->indent += 2;
3018 else if (union_p && default_p)
3020 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3021 d->indent += 2;
3022 seen_default_p = 1;
3024 else if (!union_p && (default_p || tagid))
3025 error_at_line (d->line,
3026 "can't use `%s' outside a union on field `%s'",
3027 default_p ? "default" : "tag", f->name);
3028 else if (union_p && !(default_p || tagid)
3029 && f->type->kind == TYPE_SCALAR)
3031 fprintf (stderr,
3032 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3033 get_input_file_name (d->line->file), d->line->line,
3034 f->name);
3035 continue;
3037 else if (union_p && !(default_p || tagid))
3038 error_at_line (d->line,
3039 "field `%s' is missing `tag' or `default' option",
3040 f->name);
3042 if (fieldlength)
3044 d->loopcounter = endcounter - lengths_seen--;
3047 d->line = &f->line;
3048 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3049 d->opt = f->opt;
3050 d->used_length = false;
3051 d->in_record_p = !union_p;
3053 if (union_p && use_param_p && d->param == NULL)
3054 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3055 else
3056 walk_type (f->type, d);
3058 d->in_record_p = false;
3060 free (newval);
3062 if (union_p)
3064 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3065 d->indent -= 2;
3068 d->reorder_fn = NULL;
3070 d->val = oldval;
3071 d->prev_val[1] = oldprevval1;
3072 d->prev_val[2] = oldprevval2;
3074 if (union_p && !seen_default_p)
3076 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3077 oprintf (d->of, "%*s break;\n", d->indent, "");
3079 if (union_p)
3081 oprintf (d->of, "%*s}\n", d->indent, "");
3082 d->indent -= 2;
3084 if (any_length_seen)
3086 d->indent -= 2;
3087 oprintf (d->of, "%*s}\n", d->indent, "");
3090 break;
3092 case TYPE_LANG_STRUCT:
3094 type_p nt;
3095 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3096 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3097 break;
3098 if (nt == NULL)
3099 error_at_line (d->line, "structure `%s' differs between languages",
3100 t->u.s.tag);
3101 else
3102 walk_type (nt, d);
3104 break;
3106 case TYPE_PARAM_STRUCT:
3108 type_p *oldparam = d->param;
3110 d->param = t->u.param_struct.param;
3111 walk_type (t->u.param_struct.stru, d);
3112 d->param = oldparam;
3114 break;
3116 case TYPE_USER_STRUCT:
3117 d->process_field (t, d);
3118 break;
3120 case TYPE_NONE:
3121 case TYPE_UNDEFINED:
3122 gcc_unreachable ();
3126 /* process_field routine for marking routines. */
3128 static void
3129 write_types_process_field (type_p f, const struct walk_type_data *d)
3131 const struct write_types_data *wtd;
3132 const char *cast = d->needs_cast_p ? "(void *)" : "";
3133 wtd = (const struct write_types_data *) d->cookie;
3135 switch (f->kind)
3137 case TYPE_NONE:
3138 case TYPE_UNDEFINED:
3139 gcc_unreachable ();
3140 case TYPE_POINTER:
3141 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3142 wtd->subfield_marker_routine, cast, d->val);
3143 if (wtd->param_prefix)
3145 if (f->u.p->kind == TYPE_SCALAR)
3146 /* The current type is a pointer to a scalar (so not
3147 considered like a pointer to instances of user defined
3148 types) and we are seeing it; it means we must be even
3149 more careful about the second argument of the
3150 SUBFIELD_MARKER_ROUTINE call. That argument must
3151 always be the instance of the type for which
3152 write_func_for_structure was called - this really is
3153 what the function SUBFIELD_MARKER_ROUTINE expects.
3154 That is, it must be an instance of the ORIG_S type
3155 parameter of write_func_for_structure. The convention
3156 is that that argument must be "x" in that case (as set
3157 by write_func_for_structure). The problem is, we can't
3158 count on d->prev_val[3] to be always set to "x" in that
3159 case. Sometimes walk_type can set it to something else
3160 (to e.g cooperate with write_array when called from
3161 write_roots). So let's set it to "x" here then. */
3162 oprintf (d->of, ", x");
3163 else
3164 oprintf (d->of, ", %s", d->prev_val[3]);
3165 if (d->orig_s)
3167 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3168 output_mangled_typename (d->of, d->orig_s);
3170 else
3171 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3173 if (f->u.p->kind == TYPE_PARAM_STRUCT
3174 && f->u.p->u.s.line.file != NULL)
3176 oprintf (d->of, ", gt_e_");
3177 output_mangled_typename (d->of, f);
3179 else if (union_or_struct_p (f) && f->u.p->u.s.line.file != NULL)
3181 oprintf (d->of, ", gt_ggc_e_");
3182 output_mangled_typename (d->of, f);
3184 else
3185 oprintf (d->of, ", gt_types_enum_last");
3187 oprintf (d->of, ");\n");
3188 if (d->reorder_fn && wtd->reorder_note_routine)
3189 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3190 wtd->reorder_note_routine, cast, d->val,
3191 d->prev_val[3], d->reorder_fn);
3192 break;
3194 case TYPE_STRING:
3195 case TYPE_STRUCT:
3196 case TYPE_UNION:
3197 case TYPE_LANG_STRUCT:
3198 case TYPE_PARAM_STRUCT:
3199 case TYPE_USER_STRUCT:
3200 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3202 /* If F is a user-defined type and the field is not a
3203 pointer to the type, then we should not generate the
3204 standard pointer-marking code. All we need to do is call
3205 the user-provided marking function to process the fields
3206 of F. */
3207 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3208 d->val);
3210 else
3212 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3213 output_mangled_typename (d->of, f);
3214 oprintf (d->of, " (%s%s);\n", cast, d->val);
3215 if (d->reorder_fn && wtd->reorder_note_routine)
3216 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3217 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3218 d->reorder_fn);
3220 break;
3222 case TYPE_SCALAR:
3223 break;
3225 case TYPE_ARRAY:
3226 gcc_unreachable ();
3230 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
3232 static void
3233 output_type_enum (outf_p of, type_p s)
3235 if (s->kind == TYPE_PARAM_STRUCT && s->u.param_struct.line.file != NULL)
3237 oprintf (of, ", gt_e_");
3238 output_mangled_typename (of, s);
3240 else if (union_or_struct_p (s) && s->u.s.line.file != NULL)
3242 oprintf (of, ", gt_ggc_e_");
3243 output_mangled_typename (of, s);
3245 else
3246 oprintf (of, ", gt_types_enum_last");
3249 /* Return an output file that is suitable for definitions which can
3250 reference struct S */
3252 static outf_p
3253 get_output_file_for_structure (const_type_p s, type_p *param)
3255 const input_file *fn;
3256 int i;
3258 gcc_assert (union_or_struct_p (s));
3259 fn = s->u.s.line.file;
3261 /* This is a hack, and not the good kind either. */
3262 for (i = NUM_PARAM - 1; i >= 0; i--)
3263 if (param && param[i] && param[i]->kind == TYPE_POINTER
3264 && union_or_struct_p (param[i]->u.p))
3265 fn = param[i]->u.p->u.s.line.file;
3267 /* The call to get_output_file_with_visibility may update fn by
3268 caching its result inside, so we need the CONST_CAST. */
3269 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3273 /* Returns the specifier keyword for a string or union type S, empty string
3274 otherwise. */
3276 static const char *
3277 get_type_specifier (const type_p s)
3279 if (s->kind == TYPE_STRUCT)
3280 return "struct ";
3281 else if (s->kind == TYPE_LANG_STRUCT)
3282 return get_type_specifier (s->u.s.lang_struct);
3283 else if (s->kind == TYPE_UNION)
3284 return "union ";
3285 return "";
3289 /* Emits a declaration for type TY (assumed to be a union or a
3290 structure) on stream OUT. */
3292 static void
3293 write_type_decl (outf_p out, type_p ty)
3295 if (union_or_struct_p (ty))
3296 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3297 else if (ty->kind == TYPE_SCALAR)
3299 if (ty->u.scalar_is_char)
3300 oprintf (out, "const char");
3301 else
3302 oprintf (out, "void");
3304 else if (ty->kind == TYPE_POINTER)
3306 write_type_decl (out, ty->u.p);
3307 oprintf (out, " *");
3309 else if (ty->kind == TYPE_ARRAY)
3311 write_type_decl (out, ty->u.a.p);
3312 oprintf (out, " *");
3314 else if (ty->kind == TYPE_STRING)
3316 oprintf (out, "const char *");
3318 else
3319 gcc_unreachable ();
3323 /* Write on OF the name of the marker function for structure S. PREFIX
3324 is the prefix to use (to distinguish ggc from pch markers). */
3326 static void
3327 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3329 if (union_or_struct_p (s))
3331 const char *id_for_tag = filter_type_name (s->u.s.tag);
3332 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3333 if (id_for_tag != s->u.s.tag)
3334 free (CONST_CAST(char *, id_for_tag));
3336 else if (s->kind == TYPE_PARAM_STRUCT)
3338 oprintf (of, "gt_%s_", prefix);
3339 output_mangled_typename (of, s);
3341 else
3342 gcc_unreachable ();
3345 /* Write on OF a user-callable routine to act as an entry point for
3346 the marking routine for S, generated by write_func_for_structure.
3347 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3349 static void
3350 write_user_func_for_structure_ptr (outf_p of, type_p s, const char *prefix)
3352 /* Parameterized structures are not supported in user markers. There
3353 is no way for the marker function to know which specific type
3354 to use to generate the call to the void * entry point. For
3355 instance, a marker for struct htab may need to call different
3356 routines to mark the fields, depending on the paramN_is attributes.
3358 A user-defined marker that accepts 'struct htab' as its argument
3359 would not know which variant to call. Generating several entry
3360 points accepting 'struct htab' would cause multiply-defined
3361 errors during compilation. */
3362 gcc_assert (union_or_struct_p (s));
3364 type_p alias_of = NULL;
3365 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3366 if (strcmp (opt->name, "ptr_alias") == 0)
3368 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3369 we do not generate marking code for ORIG_S here. Instead, a
3370 forwarder #define in gtype-desc.h will cause every call to its
3371 marker to call the target of this alias.
3373 However, we still want to create a user entry code for the
3374 aliased type. So, if ALIAS_OF is set, we only generate the
3375 user-callable marker function. */
3376 alias_of = opt->info.type;
3377 break;
3380 oprintf (of, "\nvoid\n");
3381 oprintf (of, "gt_%sx (", prefix);
3382 write_type_decl (of, s);
3383 oprintf (of, " *& x)\n");
3384 oprintf (of, "{\n");
3385 oprintf (of, " if (x)\n ");
3386 write_marker_function_name (of, alias_of ? alias_of : s, prefix);
3387 oprintf (of, " ((void *) x);\n");
3388 oprintf (of, "}\n");
3392 /* Write a function to mark all the fields of type S on OF. PREFIX
3393 and D are as in write_user_marking_functions. */
3395 static void
3396 write_user_func_for_structure_body (type_p s, const char *prefix,
3397 struct walk_type_data *d)
3399 oprintf (d->of, "\nvoid\n");
3400 oprintf (d->of, "gt_%sx (", prefix);
3401 write_type_decl (d->of, s);
3402 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3403 oprintf (d->of, "{\n");
3404 oprintf (d->of, " ");
3405 write_type_decl (d->of, s);
3406 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3407 d->val = "(*x)";
3408 d->indent = 2;
3409 walk_type (s, d);
3410 oprintf (d->of, "}\n");
3414 /* Emit the user-callable functions needed to mark all the types used
3415 by the user structure S. PREFIX is the prefix to use to
3416 distinguish ggc and pch markers. D contains data needed to pass to
3417 walk_type when traversing the fields of a type.
3419 For every type T referenced by S, two routines are generated: one
3420 that takes 'T *', marks the pointer and calls the second routine,
3421 which just marks the fields of T. */
3423 static void
3424 write_user_marking_functions (type_p s, const char *prefix,
3425 struct walk_type_data *d)
3427 gcc_assert (s->kind == TYPE_USER_STRUCT);
3429 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3431 type_p fld_type = fld->type;
3432 if (fld_type->kind == TYPE_POINTER)
3434 type_p pointed_to_type = fld_type->u.p;
3435 if (union_or_struct_p (pointed_to_type))
3436 write_user_func_for_structure_ptr (d->of, pointed_to_type, prefix);
3438 else if (union_or_struct_p (fld_type))
3439 write_user_func_for_structure_body (fld_type, prefix, d);
3444 /* For S, a structure that's part of ORIG_S, and using parameters
3445 PARAM, write out a routine that:
3446 - Takes a parameter, a void * but actually of type *S
3447 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3448 field of S or its substructures and (in some cases) things
3449 that are pointed to by S. */
3451 static void
3452 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3453 const struct write_types_data *wtd)
3455 const char *chain_next = NULL;
3456 const char *chain_prev = NULL;
3457 const char *chain_circular = NULL;
3458 const char *mark_hook_name = NULL;
3459 options_p opt;
3460 struct walk_type_data d;
3462 memset (&d, 0, sizeof (d));
3463 d.of = get_output_file_for_structure (s, param);
3464 for (opt = s->u.s.opt; opt; opt = opt->next)
3465 if (strcmp (opt->name, "chain_next") == 0
3466 && opt->kind == OPTION_STRING)
3467 chain_next = opt->info.string;
3468 else if (strcmp (opt->name, "chain_prev") == 0
3469 && opt->kind == OPTION_STRING)
3470 chain_prev = opt->info.string;
3471 else if (strcmp (opt->name, "chain_circular") == 0
3472 && opt->kind == OPTION_STRING)
3473 chain_circular = opt->info.string;
3474 else if (strcmp (opt->name, "mark_hook") == 0
3475 && opt->kind == OPTION_STRING)
3476 mark_hook_name = opt->info.string;
3477 if (chain_prev != NULL && chain_next == NULL)
3478 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3479 if (chain_circular != NULL && chain_next != NULL)
3480 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3481 if (chain_circular != NULL)
3482 chain_next = chain_circular;
3484 d.process_field = write_types_process_field;
3485 d.cookie = wtd;
3486 d.orig_s = orig_s;
3487 d.opt = s->u.s.opt;
3488 d.line = &s->u.s.line;
3489 d.bitmap = s->u.s.bitmap;
3490 d.param = param;
3491 d.prev_val[0] = "*x";
3492 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3493 d.prev_val[3] = "x";
3494 d.val = "(*x)";
3495 d.have_this_obj = false;
3497 oprintf (d.of, "\n");
3498 oprintf (d.of, "void\n");
3499 write_marker_function_name (d.of, orig_s, wtd->prefix);
3500 oprintf (d.of, " (void *x_p)\n");
3501 oprintf (d.of, "{\n ");
3502 write_type_decl (d.of, s);
3503 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3504 write_type_decl (d.of, s);
3505 oprintf (d.of, " *)x_p;\n");
3506 if (chain_next != NULL)
3508 /* TYPE_USER_STRUCTs should not occur here. These structures
3509 are completely handled by user code. */
3510 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3512 oprintf (d.of, " ");
3513 write_type_decl (d.of, s);
3514 oprintf (d.of, " * xlimit = x;\n");
3516 if (chain_next == NULL)
3518 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3519 if (wtd->param_prefix)
3521 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3522 output_mangled_typename (d.of, orig_s);
3523 output_type_enum (d.of, orig_s);
3525 oprintf (d.of, "))\n");
3527 else
3529 if (chain_circular != NULL)
3530 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3531 else
3532 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3533 if (wtd->param_prefix)
3535 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3536 output_mangled_typename (d.of, orig_s);
3537 output_type_enum (d.of, orig_s);
3539 oprintf (d.of, "))\n");
3540 if (chain_circular != NULL)
3541 oprintf (d.of, " return;\n do\n");
3542 if (mark_hook_name && !wtd->skip_hooks)
3544 oprintf (d.of, " {\n");
3545 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3547 oprintf (d.of, " xlimit = (");
3548 d.prev_val[2] = "*xlimit";
3549 output_escaped_param (&d, chain_next, "chain_next");
3550 oprintf (d.of, ");\n");
3551 if (mark_hook_name && !wtd->skip_hooks)
3552 oprintf (d.of, " }\n");
3553 if (chain_prev != NULL)
3555 oprintf (d.of, " if (x != xlimit)\n");
3556 oprintf (d.of, " for (;;)\n");
3557 oprintf (d.of, " {\n");
3558 oprintf (d.of, " %s %s * const xprev = (",
3559 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3561 d.prev_val[2] = "*x";
3562 output_escaped_param (&d, chain_prev, "chain_prev");
3563 oprintf (d.of, ");\n");
3564 oprintf (d.of, " if (xprev == NULL) break;\n");
3565 oprintf (d.of, " x = xprev;\n");
3566 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3567 if (wtd->param_prefix)
3569 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3570 output_mangled_typename (d.of, orig_s);
3571 output_type_enum (d.of, orig_s);
3573 oprintf (d.of, ");\n");
3574 oprintf (d.of, " }\n");
3576 if (chain_circular != NULL)
3578 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3579 if (wtd->param_prefix)
3581 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3582 output_mangled_typename (d.of, orig_s);
3583 output_type_enum (d.of, orig_s);
3585 oprintf (d.of, "));\n");
3586 if (mark_hook_name && !wtd->skip_hooks)
3587 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3588 oprintf (d.of, " do\n");
3590 else
3591 oprintf (d.of, " while (x != xlimit)\n");
3593 oprintf (d.of, " {\n");
3594 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3596 oprintf (d.of, " %s (x);\n", mark_hook_name);
3599 d.prev_val[2] = "*x";
3600 d.indent = 6;
3601 if (orig_s->kind != TYPE_USER_STRUCT)
3602 walk_type (s, &d);
3603 else
3605 /* User structures have no fields to walk. Simply generate a call
3606 to the user-provided structure marker. */
3607 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3610 if (chain_next != NULL)
3612 oprintf (d.of, " x = (");
3613 output_escaped_param (&d, chain_next, "chain_next");
3614 oprintf (d.of, ");\n");
3617 oprintf (d.of, " }\n");
3618 if (chain_circular != NULL)
3619 oprintf (d.of, " while (x != xlimit);\n");
3620 oprintf (d.of, "}\n");
3622 if (orig_s->kind == TYPE_USER_STRUCT)
3623 write_user_marking_functions (orig_s, wtd->prefix, &d);
3627 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3629 static void
3630 write_types (outf_p output_header, type_p structures, type_p param_structs,
3631 const struct write_types_data *wtd)
3633 int nbfun = 0; /* Count the emitted functions. */
3634 type_p s;
3636 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3638 /* We first emit the macros and the declarations. Functions' code is
3639 emitted afterwards. This is needed in plugin mode. */
3640 oprintf (output_header, "/* Macros and declarations. */\n");
3641 for (s = structures; s; s = s->next)
3642 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3644 options_p opt;
3646 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3647 continue;
3649 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3651 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3652 output_mangled_typename (output_header, s);
3653 oprintf (output_header, "(X) do { \\\n");
3654 oprintf (output_header,
3655 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3656 s_id_for_tag);
3657 oprintf (output_header, " } while (0)\n");
3659 for (opt = s->u.s.opt; opt; opt = opt->next)
3660 if (strcmp (opt->name, "ptr_alias") == 0
3661 && opt->kind == OPTION_TYPE)
3663 const_type_p const t = (const_type_p) opt->info.type;
3664 if (t->kind == TYPE_STRUCT
3665 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3667 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3668 oprintf (output_header,
3669 "#define gt_%sx_%s gt_%sx_%s\n",
3670 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3671 if (t_id_for_tag != t->u.s.tag)
3672 free (CONST_CAST(char *, t_id_for_tag));
3674 else
3675 error_at_line (&s->u.s.line,
3676 "structure alias is not a structure");
3677 break;
3679 if (opt)
3680 continue;
3682 /* Declare the marker procedure only once. */
3683 oprintf (output_header,
3684 "extern void gt_%sx_%s (void *);\n",
3685 wtd->prefix, s_id_for_tag);
3687 if (s_id_for_tag != s->u.s.tag)
3688 free (CONST_CAST(char *, s_id_for_tag));
3690 if (s->u.s.line.file == NULL)
3692 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3693 s->u.s.tag);
3694 continue;
3698 for (s = param_structs; s; s = s->next)
3699 if (s->gc_used == GC_POINTED_TO)
3701 type_p stru = s->u.param_struct.stru;
3703 /* Declare the marker procedure. */
3704 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3705 output_mangled_typename (output_header, s);
3706 oprintf (output_header, " (void *);\n");
3708 if (stru->u.s.line.file == NULL)
3710 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3711 stru->u.s.tag);
3712 continue;
3716 /* At last we emit the functions code. */
3717 oprintf (output_header, "\n/* functions code */\n");
3718 for (s = structures; s; s = s->next)
3719 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3721 options_p opt;
3723 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3724 continue;
3725 for (opt = s->u.s.opt; opt; opt = opt->next)
3726 if (strcmp (opt->name, "ptr_alias") == 0)
3727 break;
3728 if (opt)
3729 continue;
3731 if (s->kind == TYPE_LANG_STRUCT)
3733 type_p ss;
3734 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3736 nbfun++;
3737 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3738 nbfun, (void*) ss, ss->u.s.tag);
3739 write_func_for_structure (s, ss, NULL, wtd);
3742 else
3744 nbfun++;
3745 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3746 nbfun, (void*) s, s->u.s.tag);
3747 write_func_for_structure (s, s, NULL, wtd);
3750 else
3752 /* Structure s is not possibly pointed to, so can be ignored. */
3753 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3754 (void*)s, s->u.s.tag,
3755 (int) s->gc_used);
3758 for (s = param_structs; s; s = s->next)
3759 if (s->gc_used == GC_POINTED_TO)
3761 type_p *param = s->u.param_struct.param;
3762 type_p stru = s->u.param_struct.stru;
3763 if (stru->u.s.line.file == NULL)
3764 continue;
3765 if (stru->kind == TYPE_LANG_STRUCT)
3767 type_p ss;
3768 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3770 nbfun++;
3771 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3772 nbfun, (void*) ss, ss->u.s.tag);
3773 write_func_for_structure (s, ss, param, wtd);
3776 else
3778 nbfun++;
3779 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3780 nbfun, (void*) s,
3781 (void*) stru, stru->u.s.tag);
3782 write_func_for_structure (s, stru, param, wtd);
3785 else
3787 /* Param structure s is not pointed to, so should be ignored. */
3788 DBGPRINTF ("ignored s @ %p", (void*)s);
3790 if (verbosity_level >= 2)
3791 printf ("%s emitted %d routines for %s\n",
3792 progname, nbfun, wtd->comment);
3795 static const struct write_types_data ggc_wtd = {
3796 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3797 "GC marker procedures. ",
3798 FALSE
3801 static const struct write_types_data pch_wtd = {
3802 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3803 "gt_pch_note_reorder",
3804 "PCH type-walking procedures. ",
3805 TRUE
3808 /* Write out the local pointer-walking routines. */
3810 /* process_field routine for local pointer-walking for user-callable
3811 routines. The difference between this and
3812 write_types_local_process_field is that, in this case, we do not
3813 need to check whether the given pointer matches the address of the
3814 parent structure. This check was already generated by the call
3815 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3816 this code. */
3818 static void
3819 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3821 switch (f->kind)
3823 case TYPE_POINTER:
3824 case TYPE_STRUCT:
3825 case TYPE_UNION:
3826 case TYPE_LANG_STRUCT:
3827 case TYPE_PARAM_STRUCT:
3828 case TYPE_STRING:
3829 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3830 break;
3832 case TYPE_USER_STRUCT:
3833 if (d->in_ptr_field)
3834 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3835 else
3836 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3837 d->indent, "", d->val);
3838 break;
3840 case TYPE_SCALAR:
3841 break;
3843 case TYPE_ARRAY:
3844 case TYPE_NONE:
3845 case TYPE_UNDEFINED:
3846 gcc_unreachable ();
3851 /* Write a function to PCH walk all the fields of type S on OF.
3852 D contains data needed by walk_type to recurse into the fields of S. */
3854 static void
3855 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3857 oprintf (d->of, "\nvoid\n");
3858 oprintf (d->of, "gt_pch_nx (");
3859 write_type_decl (d->of, s);
3860 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3861 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3862 "\tATTRIBUTE_UNUSED void *cookie)\n");
3863 oprintf (d->of, "{\n");
3864 d->val = "(*x)";
3865 d->indent = 2;
3866 d->process_field = write_types_local_user_process_field;
3867 walk_type (s, d);
3868 oprintf (d->of, "}\n");
3872 /* Emit the user-callable functions needed to mark all the types used
3873 by the user structure S. PREFIX is the prefix to use to
3874 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3875 chain_next option defined. D contains data needed to pass to
3876 walk_type when traversing the fields of a type.
3878 For every type T referenced by S, two routines are generated: one
3879 that takes 'T *', marks the pointer and calls the second routine,
3880 which just marks the fields of T. */
3882 static void
3883 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3885 gcc_assert (s->kind == TYPE_USER_STRUCT);
3887 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3889 type_p fld_type = fld->type;
3890 if (union_or_struct_p (fld_type))
3891 write_pch_user_walking_for_structure_body (fld_type, d);
3896 /* process_field routine for local pointer-walking. */
3898 static void
3899 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3901 gcc_assert (d->have_this_obj);
3902 switch (f->kind)
3904 case TYPE_POINTER:
3905 case TYPE_STRUCT:
3906 case TYPE_UNION:
3907 case TYPE_LANG_STRUCT:
3908 case TYPE_PARAM_STRUCT:
3909 case TYPE_STRING:
3910 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3911 d->prev_val[3]);
3912 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3913 break;
3915 case TYPE_USER_STRUCT:
3916 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3917 d->prev_val[3]);
3918 if (d->in_ptr_field)
3919 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3920 else
3921 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3922 d->indent, "", d->val);
3923 break;
3925 case TYPE_SCALAR:
3926 break;
3928 case TYPE_ARRAY:
3929 case TYPE_NONE:
3930 case TYPE_UNDEFINED:
3931 gcc_unreachable ();
3936 /* For S, a structure that's part of ORIG_S, and using parameters
3937 PARAM, write out a routine that:
3938 - Is of type gt_note_pointers
3939 - Calls PROCESS_FIELD on each field of S or its substructures.
3942 static void
3943 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3945 struct walk_type_data d;
3947 memset (&d, 0, sizeof (d));
3948 d.of = get_output_file_for_structure (s, param);
3949 d.process_field = write_types_local_process_field;
3950 d.opt = s->u.s.opt;
3951 d.line = &s->u.s.line;
3952 d.bitmap = s->u.s.bitmap;
3953 d.param = param;
3954 d.prev_val[0] = d.prev_val[2] = "*x";
3955 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3956 d.prev_val[3] = "x";
3957 d.val = "(*x)";
3958 d.fn_wants_lvalue = true;
3960 oprintf (d.of, "\n");
3961 oprintf (d.of, "void\n");
3962 oprintf (d.of, "gt_pch_p_");
3963 output_mangled_typename (d.of, orig_s);
3964 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3965 "\tvoid *x_p,\n"
3966 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3967 "\tATTRIBUTE_UNUSED void *cookie)\n");
3968 oprintf (d.of, "{\n");
3969 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3970 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3971 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3972 d.indent = 2;
3973 d.have_this_obj = true;
3975 if (s->kind != TYPE_USER_STRUCT)
3976 walk_type (s, &d);
3977 else
3979 /* User structures have no fields to walk. Simply generate a
3980 call to the user-provided PCH walker. */
3981 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3982 d.prev_val[3]);
3983 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3984 d.indent, "", d.val);
3987 oprintf (d.of, "}\n");
3989 /* Write user-callable entry points for the PCH walking routines. */
3990 if (orig_s->kind == TYPE_USER_STRUCT)
3991 write_pch_user_walking_functions (s, &d);
3994 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3996 static void
3997 write_local (outf_p output_header, type_p structures, type_p param_structs)
3999 type_p s;
4001 if (!output_header)
4002 return;
4004 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4005 for (s = structures; s; s = s->next)
4006 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4008 options_p opt;
4010 if (s->u.s.line.file == NULL)
4011 continue;
4012 for (opt = s->u.s.opt; opt; opt = opt->next)
4013 if (strcmp (opt->name, "ptr_alias") == 0
4014 && opt->kind == OPTION_TYPE)
4016 const_type_p const t = (const_type_p) opt->info.type;
4017 if (t->kind == TYPE_STRUCT
4018 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4020 oprintf (output_header, "#define gt_pch_p_");
4021 output_mangled_typename (output_header, s);
4022 oprintf (output_header, " gt_pch_p_");
4023 output_mangled_typename (output_header, t);
4024 oprintf (output_header, "\n");
4026 else
4027 error_at_line (&s->u.s.line,
4028 "structure alias is not a structure");
4029 break;
4031 if (opt)
4032 continue;
4034 /* Declare the marker procedure only once. */
4035 oprintf (output_header, "extern void gt_pch_p_");
4036 output_mangled_typename (output_header, s);
4037 oprintf (output_header,
4038 "\n (void *, void *, gt_pointer_operator, void *);\n");
4040 if (s->kind == TYPE_LANG_STRUCT)
4042 type_p ss;
4043 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4044 write_local_func_for_structure (s, ss, NULL);
4046 else
4047 write_local_func_for_structure (s, s, NULL);
4050 for (s = param_structs; s; s = s->next)
4051 if (s->gc_used == GC_POINTED_TO)
4053 type_p *param = s->u.param_struct.param;
4054 type_p stru = s->u.param_struct.stru;
4056 /* Declare the marker procedure. */
4057 oprintf (output_header, "extern void gt_pch_p_");
4058 output_mangled_typename (output_header, s);
4059 oprintf (output_header,
4060 "\n (void *, void *, gt_pointer_operator, void *);\n");
4062 if (stru->u.s.line.file == NULL)
4064 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4065 stru->u.s.tag);
4066 continue;
4069 if (stru->kind == TYPE_LANG_STRUCT)
4071 type_p ss;
4072 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4073 write_local_func_for_structure (s, ss, param);
4075 else
4076 write_local_func_for_structure (s, stru, param);
4080 /* Nonzero if S is a type for which typed GC allocators should be output. */
4082 #define USED_BY_TYPED_GC_P(s) \
4083 ((s->kind == TYPE_POINTER \
4084 && (s->u.p->gc_used == GC_POINTED_TO \
4085 || s->u.p->gc_used == GC_USED)) \
4086 || (union_or_struct_p (s) \
4087 && ((s)->gc_used == GC_POINTED_TO \
4088 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4089 && s->u.s.line.file != NULL) \
4090 || ((s)->gc_used == GC_USED \
4091 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
4094 /* Write out the 'enum' definition for gt_types_enum. */
4096 static void
4097 write_enum_defn (type_p structures, type_p param_structs)
4099 type_p s;
4100 int nbstruct = 0;
4101 int nbparamstruct = 0;
4103 if (!header_file)
4104 return;
4105 oprintf (header_file, "\n/* Enumeration of types known. */\n");
4106 oprintf (header_file, "enum gt_types_enum {\n");
4107 for (s = structures; s; s = s->next)
4108 if (USED_BY_TYPED_GC_P (s))
4110 nbstruct++;
4111 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
4112 (void*) s, nbstruct);
4113 if (union_or_struct_p (s))
4114 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
4115 (void*) s, nbstruct, s->u.s.tag);
4116 oprintf (header_file, " gt_ggc_e_");
4117 output_mangled_typename (header_file, s);
4118 oprintf (header_file, ",\n");
4120 for (s = param_structs; s; s = s->next)
4121 if (s->gc_used == GC_POINTED_TO)
4123 nbparamstruct++;
4124 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
4125 (void*) s, nbparamstruct);
4126 oprintf (header_file, " gt_e_");
4127 output_mangled_typename (header_file, s);
4128 oprintf (header_file, ",\n");
4130 oprintf (header_file, " gt_types_enum_last\n");
4131 oprintf (header_file, "};\n");
4132 if (verbosity_level >= 2)
4133 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
4134 progname, nbstruct, nbparamstruct);
4138 /* Might T contain any non-pointer elements? */
4140 static int
4141 contains_scalar_p (type_p t)
4143 switch (t->kind)
4145 case TYPE_STRING:
4146 case TYPE_POINTER:
4147 return 0;
4148 case TYPE_ARRAY:
4149 return contains_scalar_p (t->u.a.p);
4150 case TYPE_USER_STRUCT:
4151 /* User-marked structures will typically contain pointers. */
4152 return 0;
4153 default:
4154 /* Could also check for structures that have no non-pointer
4155 fields, but there aren't enough of those to worry about. */
4156 return 1;
4160 /* Mangle INPF and print it to F. */
4162 static void
4163 put_mangled_filename (outf_p f, const input_file *inpf)
4165 /* The call to get_output_file_name may indirectly update fn since
4166 get_output_file_with_visibility caches its result inside, so we
4167 need the CONST_CAST. */
4168 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4169 if (!f || !name)
4170 return;
4171 for (; *name != 0; name++)
4172 if (ISALNUM (*name))
4173 oprintf (f, "%c", *name);
4174 else
4175 oprintf (f, "%c", '_');
4178 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4179 LASTNAME, and NAME are all strings to insert in various places in
4180 the resulting code. */
4182 static void
4183 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4184 const char *tname, const char *name)
4186 struct flist *fli2;
4188 for (fli2 = flp; fli2; fli2 = fli2->next)
4189 if (fli2->started_p)
4191 oprintf (fli2->f, " %s\n", lastname);
4192 oprintf (fli2->f, "};\n\n");
4195 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4196 if (fli2->started_p)
4198 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4199 int fnum;
4201 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4202 if (bitmap & 1)
4204 oprintf (base_files[fnum],
4205 "extern const struct %s gt_%s_", tname, pfx);
4206 put_mangled_filename (base_files[fnum], fli2->file);
4207 oprintf (base_files[fnum], "[];\n");
4212 size_t fnum;
4213 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4214 oprintf (base_files[fnum],
4215 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4219 for (fli2 = flp; fli2; fli2 = fli2->next)
4220 if (fli2->started_p)
4222 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4223 int fnum;
4225 fli2->started_p = 0;
4227 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4228 if (bitmap & 1)
4230 oprintf (base_files[fnum], " gt_%s_", pfx);
4231 put_mangled_filename (base_files[fnum], fli2->file);
4232 oprintf (base_files[fnum], ",\n");
4237 size_t fnum;
4238 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4240 oprintf (base_files[fnum], " NULL\n");
4241 oprintf (base_files[fnum], "};\n");
4246 /* Write the first three fields (pointer, count and stride) for
4247 root NAME to F. V and LINE are as for write_root.
4249 Return true if the entry could be written; return false on error. */
4251 static bool
4252 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4254 type_p ap;
4256 if (!v)
4258 error_at_line (line, "`%s' is too complex to be a root", name);
4259 return false;
4262 oprintf (f, " {\n");
4263 oprintf (f, " &%s,\n", name);
4264 oprintf (f, " 1");
4266 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4267 if (ap->u.a.len[0])
4268 oprintf (f, " * (%s)", ap->u.a.len);
4269 else if (ap == v->type)
4270 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4271 oprintf (f, ",\n");
4272 oprintf (f, " sizeof (%s", v->name);
4273 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4274 oprintf (f, "[0]");
4275 oprintf (f, "),\n");
4276 return true;
4279 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4280 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4281 of the caller. */
4283 static void
4284 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4285 int has_length, struct fileloc *line, const char *if_marked,
4286 bool emit_pch, type_p field_type, const char *field_name)
4288 struct pair newv;
4289 /* If the field reference is relative to V, rather than to some
4290 subcomponent of V, we can mark any subarrays with a single stride.
4291 We're effectively treating the field as a global variable in its
4292 own right. */
4293 if (v && type == v->type)
4295 newv = *v;
4296 newv.type = field_type;
4297 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4298 v = &newv;
4300 /* Otherwise, any arrays nested in the structure are too complex to
4301 handle. */
4302 else if (field_type->kind == TYPE_ARRAY)
4303 v = NULL;
4304 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4305 has_length, line, if_marked, emit_pch);
4308 /* Write out to F the table entry and any marker routines needed to
4309 mark NAME as TYPE. V can be one of three values:
4311 - null, if NAME is too complex to represent using a single
4312 count and stride. In this case, it is an error for NAME to
4313 contain any gc-ed data.
4315 - the outermost array that contains NAME, if NAME is part of an array.
4317 - the C variable that contains NAME, if NAME is not part of an array.
4319 LINE is the line of the C source that declares the root variable.
4320 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4321 is nonzero iff we are building the root table for hash table caches. */
4323 static void
4324 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4325 struct fileloc *line, const char *if_marked, bool emit_pch)
4327 switch (type->kind)
4329 case TYPE_STRUCT:
4331 pair_p fld;
4332 for (fld = type->u.s.fields; fld; fld = fld->next)
4334 int skip_p = 0;
4335 const char *desc = NULL;
4336 options_p o;
4338 for (o = fld->opt; o; o = o->next)
4339 if (strcmp (o->name, "skip") == 0)
4340 skip_p = 1;
4341 else if (strcmp (o->name, "desc") == 0
4342 && o->kind == OPTION_STRING)
4343 desc = o->info.string;
4344 else if (strcmp (o->name, "param_is") == 0)
4346 else
4347 error_at_line (line,
4348 "field `%s' of global `%s' has unknown option `%s'",
4349 fld->name, name, o->name);
4351 if (skip_p)
4352 continue;
4353 else if (desc && fld->type->kind == TYPE_UNION)
4355 pair_p validf = NULL;
4356 pair_p ufld;
4358 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4360 const char *tag = NULL;
4361 options_p oo;
4362 for (oo = ufld->opt; oo; oo = oo->next)
4363 if (strcmp (oo->name, "tag") == 0
4364 && oo->kind == OPTION_STRING)
4365 tag = oo->info.string;
4366 if (tag == NULL || strcmp (tag, desc) != 0)
4367 continue;
4368 if (validf != NULL)
4369 error_at_line (line,
4370 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4371 name, fld->name, validf->name,
4372 name, fld->name, ufld->name, tag);
4373 validf = ufld;
4375 if (validf != NULL)
4376 write_field_root (f, v, type, name, 0, line, if_marked,
4377 emit_pch, validf->type,
4378 ACONCAT ((fld->name, ".",
4379 validf->name, NULL)));
4381 else if (desc)
4382 error_at_line (line,
4383 "global `%s.%s' has `desc' option but is not union",
4384 name, fld->name);
4385 else
4386 write_field_root (f, v, type, name, 0, line, if_marked,
4387 emit_pch, fld->type, fld->name);
4390 break;
4392 case TYPE_ARRAY:
4394 char *newname;
4395 newname = xasprintf ("%s[0]", name);
4396 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4397 emit_pch);
4398 free (newname);
4400 break;
4402 case TYPE_USER_STRUCT:
4403 error_at_line (line, "`%s' must be a pointer type, because it is "
4404 "a GC root and its type is marked with GTY((user))",
4405 v->name);
4406 break;
4408 case TYPE_POINTER:
4410 type_p tp;
4412 if (!start_root_entry (f, v, name, line))
4413 return;
4415 tp = type->u.p;
4417 if (!has_length && union_or_struct_p (tp))
4419 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4420 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4421 if (emit_pch)
4422 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4423 else
4424 oprintf (f, " NULL");
4425 if (id_for_tag != tp->u.s.tag)
4426 free (CONST_CAST(char *, id_for_tag));
4428 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4430 oprintf (f, " &gt_ggc_m_");
4431 output_mangled_typename (f, tp);
4432 if (emit_pch)
4434 oprintf (f, ",\n &gt_pch_n_");
4435 output_mangled_typename (f, tp);
4437 else
4438 oprintf (f, ",\n NULL");
4440 else if (has_length
4441 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4443 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4444 if (emit_pch)
4445 oprintf (f, " &gt_pch_na_%s", name);
4446 else
4447 oprintf (f, " NULL");
4449 else
4451 error_at_line (line,
4452 "global `%s' is pointer to unimplemented type",
4453 name);
4455 if (if_marked)
4456 oprintf (f, ",\n &%s", if_marked);
4457 oprintf (f, "\n },\n");
4459 break;
4461 case TYPE_STRING:
4463 if (!start_root_entry (f, v, name, line))
4464 return;
4466 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4467 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4468 oprintf (f, " },\n");
4470 break;
4472 case TYPE_SCALAR:
4473 break;
4475 case TYPE_NONE:
4476 case TYPE_UNDEFINED:
4477 case TYPE_UNION:
4478 case TYPE_LANG_STRUCT:
4479 case TYPE_PARAM_STRUCT:
4480 error_at_line (line, "global `%s' is unimplemented type", name);
4484 /* This generates a routine to walk an array. */
4486 static void
4487 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4489 struct walk_type_data d;
4490 char *prevval3;
4492 memset (&d, 0, sizeof (d));
4493 d.of = f;
4494 d.cookie = wtd;
4495 d.indent = 2;
4496 d.line = &v->line;
4497 d.opt = v->opt;
4498 d.bitmap = get_lang_bitmap (v->line.file);
4499 d.param = NULL;
4501 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4503 if (wtd->param_prefix)
4505 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4506 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4507 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4508 wtd->param_prefix, v->name);
4509 oprintf (d.of,
4510 " ATTRIBUTE_UNUSED void *x_p,\n"
4511 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4512 " ATTRIBUTE_UNUSED void * cookie)\n");
4513 oprintf (d.of, "{\n");
4514 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4515 d.process_field = write_types_local_process_field;
4516 d.have_this_obj = true;
4517 walk_type (v->type, &d);
4518 oprintf (f, "}\n\n");
4521 d.opt = v->opt;
4522 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4523 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4524 wtd->prefix, v->name);
4525 oprintf (f, "{\n");
4526 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4527 d.process_field = write_types_process_field;
4528 d.have_this_obj = false;
4529 walk_type (v->type, &d);
4530 free (prevval3);
4531 oprintf (f, "}\n\n");
4534 /* Output a table describing the locations and types of VARIABLES. */
4536 static void
4537 write_roots (pair_p variables, bool emit_pch)
4539 pair_p v;
4540 struct flist *flp = NULL;
4542 for (v = variables; v; v = v->next)
4544 outf_p f =
4545 get_output_file_with_visibility (CONST_CAST (input_file*,
4546 v->line.file));
4547 struct flist *fli;
4548 const char *length = NULL;
4549 int deletable_p = 0;
4550 options_p o;
4551 for (o = v->opt; o; o = o->next)
4552 if (strcmp (o->name, "length") == 0
4553 && o->kind == OPTION_STRING)
4554 length = o->info.string;
4555 else if (strcmp (o->name, "deletable") == 0)
4556 deletable_p = 1;
4557 else if (strcmp (o->name, "param_is") == 0)
4559 else if (strncmp (o->name, "param", 5) == 0
4560 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4562 else if (strcmp (o->name, "if_marked") == 0)
4564 else
4565 error_at_line (&v->line,
4566 "global `%s' has unknown option `%s'",
4567 v->name, o->name);
4569 for (fli = flp; fli; fli = fli->next)
4570 if (fli->f == f && f)
4571 break;
4572 if (fli == NULL)
4574 fli = XNEW (struct flist);
4575 fli->f = f;
4576 fli->next = flp;
4577 fli->started_p = 0;
4578 fli->file = v->line.file;
4579 gcc_assert (fli->file);
4580 flp = fli;
4582 oprintf (f, "\n/* GC roots. */\n\n");
4585 if (!deletable_p
4586 && length
4587 && v->type->kind == TYPE_POINTER
4588 && (v->type->u.p->kind == TYPE_POINTER
4589 || v->type->u.p->kind == TYPE_STRUCT))
4591 write_array (f, v, &ggc_wtd);
4592 write_array (f, v, &pch_wtd);
4596 for (v = variables; v; v = v->next)
4598 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4599 v->line.file));
4600 struct flist *fli;
4601 int skip_p = 0;
4602 int length_p = 0;
4603 options_p o;
4605 for (o = v->opt; o; o = o->next)
4606 if (strcmp (o->name, "length") == 0)
4607 length_p = 1;
4608 else if (strcmp (o->name, "deletable") == 0
4609 || strcmp (o->name, "if_marked") == 0)
4610 skip_p = 1;
4612 if (skip_p)
4613 continue;
4615 for (fli = flp; fli; fli = fli->next)
4616 if (fli->f == f)
4617 break;
4618 if (!fli->started_p)
4620 fli->started_p = 1;
4622 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4623 put_mangled_filename (f, v->line.file);
4624 oprintf (f, "[] = {\n");
4627 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4630 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4631 "gt_ggc_rtab");
4633 for (v = variables; v; v = v->next)
4635 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4636 v->line.file));
4637 struct flist *fli;
4638 int skip_p = 1;
4639 options_p o;
4641 for (o = v->opt; o; o = o->next)
4642 if (strcmp (o->name, "deletable") == 0)
4643 skip_p = 0;
4644 else if (strcmp (o->name, "if_marked") == 0)
4645 skip_p = 1;
4647 if (skip_p)
4648 continue;
4650 for (fli = flp; fli; fli = fli->next)
4651 if (fli->f == f)
4652 break;
4653 if (!fli->started_p)
4655 fli->started_p = 1;
4657 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4658 put_mangled_filename (f, v->line.file);
4659 oprintf (f, "[] = {\n");
4662 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4663 v->name, v->name);
4666 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4667 "gt_ggc_deletable_rtab");
4669 for (v = variables; v; v = v->next)
4671 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4672 v->line.file));
4673 struct flist *fli;
4674 const char *if_marked = NULL;
4675 int length_p = 0;
4676 options_p o;
4678 for (o = v->opt; o; o = o->next)
4679 if (strcmp (o->name, "length") == 0)
4680 length_p = 1;
4681 else if (strcmp (o->name, "if_marked") == 0
4682 && o->kind == OPTION_STRING)
4683 if_marked = o->info.string;
4684 if (if_marked == NULL)
4685 continue;
4686 if (v->type->kind != TYPE_POINTER
4687 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4688 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4689 TYPE_STRUCT))
4691 error_at_line (&v->line,
4692 "if_marked option used but not hash table");
4693 continue;
4696 for (fli = flp; fli; fli = fli->next)
4697 if (fli->f == f)
4698 break;
4699 if (!fli->started_p)
4701 fli->started_p = 1;
4703 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4704 put_mangled_filename (f, v->line.file);
4705 oprintf (f, "[] = {\n");
4708 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4709 v->name, length_p, &v->line, if_marked, emit_pch);
4712 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4713 "gt_ggc_cache_rtab");
4715 if (!emit_pch)
4716 return;
4718 for (v = variables; v; v = v->next)
4720 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4721 v->line.file));
4722 struct flist *fli;
4723 int length_p = 0;
4724 int if_marked_p = 0;
4725 options_p o;
4727 for (o = v->opt; o; o = o->next)
4728 if (strcmp (o->name, "length") == 0)
4729 length_p = 1;
4730 else if (strcmp (o->name, "if_marked") == 0)
4731 if_marked_p = 1;
4733 if (!if_marked_p)
4734 continue;
4736 for (fli = flp; fli; fli = fli->next)
4737 if (fli->f == f)
4738 break;
4739 if (!fli->started_p)
4741 fli->started_p = 1;
4743 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4744 put_mangled_filename (f, v->line.file);
4745 oprintf (f, "[] = {\n");
4748 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4751 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4752 "gt_pch_cache_rtab");
4754 for (v = variables; v; v = v->next)
4756 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4757 v->line.file));
4758 struct flist *fli;
4759 int skip_p = 0;
4760 options_p o;
4762 for (o = v->opt; o; o = o->next)
4763 if (strcmp (o->name, "deletable") == 0
4764 || strcmp (o->name, "if_marked") == 0)
4765 skip_p = 1;
4767 if (skip_p)
4768 continue;
4770 if (!contains_scalar_p (v->type))
4771 continue;
4773 for (fli = flp; fli; fli = fli->next)
4774 if (fli->f == f)
4775 break;
4776 if (!fli->started_p)
4778 fli->started_p = 1;
4780 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4781 put_mangled_filename (f, v->line.file);
4782 oprintf (f, "[] = {\n");
4785 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4786 v->name, v->name);
4789 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4790 "gt_pch_scalar_rtab");
4793 /* TRUE if type S has the GTY variable_size annotation. */
4795 static bool
4796 variable_size_p (const type_p s)
4798 options_p o;
4799 for (o = s->u.s.opt; o; o = o->next)
4800 if (strcmp (o->name, "variable_size") == 0)
4801 return true;
4802 return false;
4805 enum alloc_quantity
4806 { single, vector };
4807 enum alloc_zone
4808 { any_zone, specific_zone };
4810 /* Writes one typed allocator definition into output F for type
4811 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4812 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4813 is true, the allocator will have an extra parameter specifying
4814 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4815 vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
4816 the allocator will be zone-specific. */
4818 static void
4819 write_typed_alloc_def (outf_p f,
4820 bool variable_size, const char *type_specifier,
4821 const char *type_name, const char *allocator_type,
4822 enum alloc_quantity quantity, enum alloc_zone zone)
4824 bool two_args = variable_size && (quantity == vector);
4825 bool third_arg = ((zone == specific_zone)
4826 && (variable_size || (quantity == vector)));
4827 gcc_assert (f != NULL);
4828 const char *type_name_as_id = filter_type_name (type_name);
4829 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name_as_id);
4830 oprintf (f, "(%s%s%s%s%s) ",
4831 (variable_size ? "SIZE" : ""),
4832 (two_args ? ", " : ""),
4833 (quantity == vector) ? "n" : "",
4834 (third_arg ? ", " : ""), (zone == specific_zone) ? "z" : "");
4835 oprintf (f, "((%s%s *)", type_specifier, type_name);
4836 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4837 if (zone == specific_zone)
4838 oprintf (f, "z, ");
4839 if (variable_size)
4840 oprintf (f, "SIZE");
4841 else
4842 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4843 if (quantity == vector)
4844 oprintf (f, ", n");
4845 oprintf (f, " MEM_STAT_INFO)))\n");
4846 if (type_name_as_id != type_name)
4847 free (CONST_CAST(char *, type_name_as_id));
4850 /* Writes a typed allocator definition into output F for a struct or
4851 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4853 static void
4854 write_typed_struct_alloc_def (outf_p f,
4855 const type_p s, const char *allocator_type,
4856 enum alloc_quantity quantity,
4857 enum alloc_zone zone)
4859 gcc_assert (union_or_struct_p (s));
4860 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4861 s->u.s.tag, allocator_type, quantity, zone);
4864 /* Writes a typed allocator definition into output F for a typedef P,
4865 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4867 static void
4868 write_typed_typedef_alloc_def (outf_p f,
4869 const pair_p p, const char *allocator_type,
4870 enum alloc_quantity quantity,
4871 enum alloc_zone zone)
4873 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4874 allocator_type, quantity, zone);
4877 /* Writes typed allocator definitions into output F for the types in
4878 STRUCTURES and TYPEDEFS that are used by GC. */
4880 static void
4881 write_typed_alloc_defns (outf_p f,
4882 const type_p structures, const pair_p typedefs)
4884 type_p s;
4885 pair_p p;
4887 gcc_assert (f != NULL);
4888 oprintf (f,
4889 "\n/* Allocators for known structs and unions. */\n\n");
4890 for (s = structures; s; s = s->next)
4892 if (!USED_BY_TYPED_GC_P (s))
4893 continue;
4894 gcc_assert (union_or_struct_p (s));
4895 /* In plugin mode onput output ggc_alloc macro definitions
4896 relevant to plugin input files. */
4897 if (nb_plugin_files > 0
4898 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
4899 continue;
4900 write_typed_struct_alloc_def (f, s, "", single, any_zone);
4901 write_typed_struct_alloc_def (f, s, "cleared_", single, any_zone);
4902 write_typed_struct_alloc_def (f, s, "vec_", vector, any_zone);
4903 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector, any_zone);
4904 write_typed_struct_alloc_def (f, s, "zone_", single, specific_zone);
4905 write_typed_struct_alloc_def (f, s, "zone_cleared_", single,
4906 specific_zone);
4907 write_typed_struct_alloc_def (f, s, "zone_vec_", vector, specific_zone);
4908 write_typed_struct_alloc_def (f, s, "zone_cleared_vec_", vector,
4909 specific_zone);
4912 oprintf (f, "\n/* Allocators for known typedefs. */\n");
4913 for (p = typedefs; p; p = p->next)
4915 s = p->type;
4916 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4917 continue;
4918 /* In plugin mode onput output ggc_alloc macro definitions
4919 relevant to plugin input files. */
4920 if (nb_plugin_files > 0)
4922 struct fileloc* filoc = type_fileloc(s);
4923 if (!filoc || !filoc->file->inpisplugin)
4924 continue;
4926 write_typed_typedef_alloc_def (f, p, "", single, any_zone);
4927 write_typed_typedef_alloc_def (f, p, "cleared_", single, any_zone);
4928 write_typed_typedef_alloc_def (f, p, "vec_", vector, any_zone);
4929 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector, any_zone);
4930 write_typed_typedef_alloc_def (f, p, "zone_", single, specific_zone);
4931 write_typed_typedef_alloc_def (f, p, "zone_cleared_", single,
4932 specific_zone);
4933 write_typed_typedef_alloc_def (f, p, "zone_cleared_vec_", vector,
4934 specific_zone);
4938 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4939 guaranteee for somewhat increased readability. If name conflicts do happen,
4940 this funcion will have to be adjusted to be more like
4941 output_mangled_typename. */
4943 static void
4944 output_typename (outf_p of, const_type_p t)
4946 switch (t->kind)
4948 case TYPE_STRING:
4949 oprintf (of, "str");
4950 break;
4951 case TYPE_SCALAR:
4952 oprintf (of, "scalar");
4953 break;
4954 case TYPE_POINTER:
4955 output_typename (of, t->u.p);
4956 break;
4957 case TYPE_STRUCT:
4958 case TYPE_USER_STRUCT:
4959 case TYPE_UNION:
4960 case TYPE_LANG_STRUCT:
4961 oprintf (of, "%s", t->u.s.tag);
4962 break;
4963 case TYPE_PARAM_STRUCT:
4965 int i;
4966 for (i = 0; i < NUM_PARAM; i++)
4967 if (t->u.param_struct.param[i] != NULL)
4969 output_typename (of, t->u.param_struct.param[i]);
4970 oprintf (of, "_");
4972 output_typename (of, t->u.param_struct.stru);
4973 break;
4975 case TYPE_NONE:
4976 case TYPE_UNDEFINED:
4977 case TYPE_ARRAY:
4978 gcc_unreachable ();
4982 /* Writes a typed GC allocator for type S that is suitable as a callback for
4983 the splay tree implementation in libiberty. */
4985 static void
4986 write_splay_tree_allocator_def (const_type_p s)
4988 outf_p of = get_output_file_with_visibility (NULL);
4989 oprintf (of, "void * ggc_alloc_splay_tree_");
4990 output_typename (of, s);
4991 oprintf (of, " (int sz, void * nl)\n");
4992 oprintf (of, "{\n");
4993 oprintf (of, " return ggc_splay_alloc (");
4994 oprintf (of, "gt_e_");
4995 output_mangled_typename (of, s);
4996 oprintf (of, ", sz, nl);\n");
4997 oprintf (of, "}\n\n");
5000 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5001 for the splay tree implementation in libiberty. */
5003 static void
5004 write_splay_tree_allocators (const_type_p param_structs)
5006 const_type_p s;
5008 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
5009 for (s = param_structs; s; s = s->next)
5010 if (s->gc_used == GC_POINTED_TO)
5012 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
5013 output_typename (header_file, s);
5014 oprintf (header_file, " (int, void *);\n");
5015 write_splay_tree_allocator_def (s);
5019 #define INDENT 2
5021 /* Dumps the value of typekind KIND. */
5023 static void
5024 dump_typekind (int indent, enum typekind kind)
5026 printf ("%*ckind = ", indent, ' ');
5027 switch (kind)
5029 case TYPE_SCALAR:
5030 printf ("TYPE_SCALAR");
5031 break;
5032 case TYPE_STRING:
5033 printf ("TYPE_STRING");
5034 break;
5035 case TYPE_STRUCT:
5036 printf ("TYPE_STRUCT");
5037 break;
5038 case TYPE_UNDEFINED:
5039 printf ("TYPE_UNDEFINED");
5040 break;
5041 case TYPE_USER_STRUCT:
5042 printf ("TYPE_USER_STRUCT");
5043 break;
5044 case TYPE_UNION:
5045 printf ("TYPE_UNION");
5046 break;
5047 case TYPE_POINTER:
5048 printf ("TYPE_POINTER");
5049 break;
5050 case TYPE_ARRAY:
5051 printf ("TYPE_ARRAY");
5052 break;
5053 case TYPE_LANG_STRUCT:
5054 printf ("TYPE_LANG_STRUCT");
5055 break;
5056 case TYPE_PARAM_STRUCT:
5057 printf ("TYPE_PARAM_STRUCT");
5058 break;
5059 default:
5060 gcc_unreachable ();
5062 printf ("\n");
5065 /* Dumps the value of GC_USED flag. */
5067 static void
5068 dump_gc_used (int indent, enum gc_used_enum gc_used)
5070 printf ("%*cgc_used = ", indent, ' ');
5071 switch (gc_used)
5073 case GC_UNUSED:
5074 printf ("GC_UNUSED");
5075 break;
5076 case GC_USED:
5077 printf ("GC_USED");
5078 break;
5079 case GC_MAYBE_POINTED_TO:
5080 printf ("GC_MAYBE_POINTED_TO");
5081 break;
5082 case GC_POINTED_TO:
5083 printf ("GC_POINTED_TO");
5084 break;
5085 default:
5086 gcc_unreachable ();
5088 printf ("\n");
5091 /* Dumps the type options OPT. */
5093 static void
5094 dump_options (int indent, options_p opt)
5096 options_p o;
5097 printf ("%*coptions = ", indent, ' ');
5098 o = opt;
5099 while (o)
5101 switch (o->kind)
5103 case OPTION_STRING:
5104 printf ("%s:string %s ", o->name, o->info.string);
5105 break;
5106 case OPTION_TYPE:
5107 printf ("%s:type ", o->name);
5108 dump_type (indent+1, o->info.type);
5109 break;
5110 case OPTION_NESTED:
5111 printf ("%s:nested ", o->name);
5112 break;
5113 case OPTION_NONE:
5114 gcc_unreachable ();
5116 o = o->next;
5118 printf ("\n");
5121 /* Dumps the source file location in LINE. */
5123 static void
5124 dump_fileloc (int indent, struct fileloc line)
5126 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5127 get_input_file_name (line.file),
5128 line.line);
5131 /* Recursively dumps the struct, union, or a language-specific
5132 struct T. */
5134 static void
5135 dump_type_u_s (int indent, type_p t)
5137 pair_p fields;
5139 gcc_assert (union_or_struct_p (t));
5140 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5141 dump_fileloc (indent, t->u.s.line);
5142 printf ("%*cu.s.fields =\n", indent, ' ');
5143 fields = t->u.s.fields;
5144 while (fields)
5146 dump_pair (indent + INDENT, fields);
5147 fields = fields->next;
5149 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5150 dump_options (indent, t->u.s.opt);
5151 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5152 if (t->kind == TYPE_LANG_STRUCT)
5154 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5155 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5159 /* Recursively dumps the array T. */
5161 static void
5162 dump_type_u_a (int indent, type_p t)
5164 gcc_assert (t->kind == TYPE_ARRAY);
5165 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5166 dump_type_list (indent + INDENT, t->u.a.p);
5169 /* Recursively dumps the parameterized struct T. */
5171 static void
5172 dump_type_u_param_struct (int indent, type_p t)
5174 int i;
5175 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5176 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5177 dump_type_list (indent, t->u.param_struct.stru);
5178 dump_fileloc (indent, t->u.param_struct.line);
5179 for (i = 0; i < NUM_PARAM; i++)
5181 if (t->u.param_struct.param[i] == NULL)
5182 continue;
5183 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5184 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5188 /* Recursively dumps the type list T. */
5190 static void
5191 dump_type_list (int indent, type_p t)
5193 type_p p = t;
5194 while (p)
5196 dump_type (indent, p);
5197 p = p->next;
5201 static htab_t seen_types;
5203 /* Recursively dumps the type T if it was not dumped previously. */
5205 static void
5206 dump_type (int indent, type_p t)
5208 PTR *slot;
5210 if (seen_types == NULL)
5211 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5213 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5214 slot = htab_find_slot (seen_types, t, INSERT);
5215 if (*slot != NULL)
5217 printf ("already seen.\n");
5218 return;
5220 *slot = t;
5221 printf ("\n");
5223 dump_typekind (indent, t->kind);
5224 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5225 (void *) t->pointer_to);
5226 dump_gc_used (indent + INDENT, t->gc_used);
5227 switch (t->kind)
5229 case TYPE_SCALAR:
5230 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5231 t->u.scalar_is_char ? "true" : "false");
5232 break;
5233 case TYPE_STRING:
5234 break;
5235 case TYPE_STRUCT:
5236 case TYPE_UNION:
5237 case TYPE_LANG_STRUCT:
5238 case TYPE_USER_STRUCT:
5239 dump_type_u_s (indent + INDENT, t);
5240 break;
5241 case TYPE_POINTER:
5242 printf ("%*cp:\n", indent + INDENT, ' ');
5243 dump_type (indent + INDENT, t->u.p);
5244 break;
5245 case TYPE_ARRAY:
5246 dump_type_u_a (indent + INDENT, t);
5247 break;
5248 case TYPE_PARAM_STRUCT:
5249 dump_type_u_param_struct (indent + INDENT, t);
5250 break;
5251 default:
5252 gcc_unreachable ();
5254 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5257 /* Dumps the pair P. */
5259 static void
5260 dump_pair (int indent, pair_p p)
5262 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5263 dump_type (indent, p->type);
5264 dump_fileloc (indent, p->line);
5265 dump_options (indent, p->opt);
5266 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5269 /* Dumps the list of pairs PP. */
5271 static void
5272 dump_pair_list (const char *name, pair_p pp)
5274 pair_p p;
5275 printf ("%s:\n", name);
5276 for (p = pp; p != NULL; p = p->next)
5277 dump_pair (0, p);
5278 printf ("End of %s\n\n", name);
5281 /* Dumps the STRUCTURES. */
5283 static void
5284 dump_structures (const char *name, type_p structures)
5286 printf ("%s:\n", name);
5287 dump_type_list (0, structures);
5288 printf ("End of %s\n\n", name);
5291 /* Dumps the internal structures of gengtype. This is useful to debug
5292 gengtype itself, or to understand what it does, e.g. for plugin
5293 developers. */
5295 static void
5296 dump_everything (void)
5298 dump_pair_list ("typedefs", typedefs);
5299 dump_structures ("structures", structures);
5300 dump_structures ("param_structs", param_structs);
5301 dump_pair_list ("variables", variables);
5303 /* Allocated with the first call to dump_type. */
5304 htab_delete (seen_types);
5309 /* Option specification for getopt_long. */
5310 static const struct option gengtype_long_options[] = {
5311 {"help", no_argument, NULL, 'h'},
5312 {"version", no_argument, NULL, 'V'},
5313 {"verbose", no_argument, NULL, 'v'},
5314 {"dump", no_argument, NULL, 'd'},
5315 {"debug", no_argument, NULL, 'D'},
5316 {"plugin", required_argument, NULL, 'P'},
5317 {"srcdir", required_argument, NULL, 'S'},
5318 {"backupdir", required_argument, NULL, 'B'},
5319 {"inputs", required_argument, NULL, 'I'},
5320 {"read-state", required_argument, NULL, 'r'},
5321 {"write-state", required_argument, NULL, 'w'},
5322 /* Terminating NULL placeholder. */
5323 {NULL, no_argument, NULL, 0},
5327 static void
5328 print_usage (void)
5330 printf ("Usage: %s\n", progname);
5331 printf ("\t -h | --help " " \t# Give this help.\n");
5332 printf ("\t -D | --debug "
5333 " \t# Give debug output to debug %s itself.\n", progname);
5334 printf ("\t -V | --version " " \t# Give version information.\n");
5335 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5336 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5337 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5338 " \t# Generate for plugin.\n");
5339 printf ("\t -S | --srcdir <GCC-directory> "
5340 " \t# Specify the GCC source directory.\n");
5341 printf ("\t -B | --backupdir <directory> "
5342 " \t# Specify the backup directory for updated files.\n");
5343 printf ("\t -I | --inputs <input-list> "
5344 " \t# Specify the file with source files list.\n");
5345 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5346 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5349 static void
5350 print_version (void)
5352 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5353 printf ("Report bugs: %s\n", bug_report_url);
5356 /* Parse the program options using getopt_long... */
5357 static void
5358 parse_program_options (int argc, char **argv)
5360 int opt = -1;
5361 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5362 gengtype_long_options, NULL)) >= 0)
5364 switch (opt)
5366 case 'h': /* --help */
5367 print_usage ();
5368 break;
5369 case 'V': /* --version */
5370 print_version ();
5371 break;
5372 case 'd': /* --dump */
5373 do_dump = 1;
5374 break;
5375 case 'D': /* --debug */
5376 do_debug = 1;
5377 break;
5378 case 'v': /* --verbose */
5379 verbosity_level++;
5380 break;
5381 case 'P': /* --plugin */
5382 if (optarg)
5383 plugin_output_filename = optarg;
5384 else
5385 fatal ("missing plugin output file name");
5386 break;
5387 case 'S': /* --srcdir */
5388 if (optarg)
5389 srcdir = optarg;
5390 else
5391 fatal ("missing source directory");
5392 srcdir_len = strlen (srcdir);
5393 break;
5394 case 'B': /* --backupdir */
5395 if (optarg)
5396 backup_dir = optarg;
5397 else
5398 fatal ("missing backup directory");
5399 break;
5400 case 'I': /* --inputs */
5401 if (optarg)
5402 inputlist = optarg;
5403 else
5404 fatal ("missing input list");
5405 break;
5406 case 'r': /* --read-state */
5407 if (optarg)
5408 read_state_filename = optarg;
5409 else
5410 fatal ("missing read state file");
5411 DBGPRINTF ("read state %s\n", optarg);
5412 break;
5413 case 'w': /* --write-state */
5414 DBGPRINTF ("write state %s\n", optarg);
5415 if (optarg)
5416 write_state_filename = optarg;
5417 else
5418 fatal ("missing write state file");
5419 break;
5420 default:
5421 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5422 print_usage ();
5423 fatal ("unexpected flag");
5426 if (plugin_output_filename)
5428 /* In plugin mode we require some input files. */
5429 int i = 0;
5430 if (optind >= argc)
5431 fatal ("no source files given in plugin mode");
5432 nb_plugin_files = argc - optind;
5433 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5434 for (i = 0; i < (int) nb_plugin_files; i++)
5436 char *name = argv[i + optind];
5437 plugin_files[i] = input_file_by_name (name);
5444 /******* Manage input files. ******/
5446 /* Hash table of unique input file names. */
5447 static htab_t input_file_htab;
5449 /* Find or allocate a new input_file by hash-consing it. */
5450 input_file*
5451 input_file_by_name (const char* name)
5453 PTR* slot;
5454 input_file* f = NULL;
5455 int namlen = 0;
5456 if (!name)
5457 return NULL;
5458 namlen = strlen (name);
5459 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5460 f->inpbitmap = 0;
5461 f->inpoutf = NULL;
5462 f->inpisplugin = false;
5463 strcpy (f->inpname, name);
5464 slot = htab_find_slot (input_file_htab, f, INSERT);
5465 gcc_assert (slot != NULL);
5466 if (*slot)
5468 /* Already known input file. */
5469 free (f);
5470 return (input_file*)(*slot);
5472 /* New input file. */
5473 *slot = f;
5474 return f;
5477 /* Hash table support routines for input_file-s. */
5478 static hashval_t
5479 htab_hash_inputfile (const void *p)
5481 const input_file *inpf = (const input_file *) p;
5482 gcc_assert (inpf);
5483 return htab_hash_string (get_input_file_name (inpf));
5486 static int
5487 htab_eq_inputfile (const void *x, const void *y)
5489 const input_file *inpfx = (const input_file *) x;
5490 const input_file *inpfy = (const input_file *) y;
5491 gcc_assert (inpfx != NULL && inpfy != NULL);
5492 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5497 main (int argc, char **argv)
5499 size_t i;
5500 static struct fileloc pos = { NULL, 0 };
5501 outf_p output_header;
5503 /* Mandatory common initializations. */
5504 progname = "gengtype"; /* For fatal and messages. */
5505 /* Create the hash-table used to hash-cons input files. */
5506 input_file_htab =
5507 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5508 /* Initialize our special input files. */
5509 this_file = input_file_by_name (__FILE__);
5510 system_h_file = input_file_by_name ("system.h");
5511 /* Set the scalar_is_char union number for predefined scalar types. */
5512 scalar_nonchar.u.scalar_is_char = FALSE;
5513 scalar_char.u.scalar_is_char = TRUE;
5515 parse_program_options (argc, argv);
5517 #if ENABLE_CHECKING
5518 if (do_debug)
5520 time_t now = (time_t) 0;
5521 time (&now);
5522 DBGPRINTF ("gengtype started pid %d at %s",
5523 (int) getpid (), ctime (&now));
5525 #endif /* ENABLE_CHECKING */
5527 /* Parse the input list and the input files. */
5528 DBGPRINTF ("inputlist %s", inputlist);
5529 if (read_state_filename)
5531 if (inputlist)
5532 fatal ("input list %s cannot be given with a read state file %s",
5533 inputlist, read_state_filename);
5534 read_state (read_state_filename);
5535 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5536 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5538 else if (inputlist)
5540 /* These types are set up with #define or else outside of where
5541 we can see them. We should initialize them before calling
5542 read_input_list. */
5543 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5544 Call;} while(0)
5545 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5546 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5547 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5548 POS_HERE (do_scalar_typedef ("double_int", &pos));
5549 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5550 POS_HERE (do_scalar_typedef ("uint8", &pos));
5551 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5552 POS_HERE (do_scalar_typedef ("jword", &pos));
5553 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5554 POS_HERE (do_scalar_typedef ("void", &pos));
5555 POS_HERE (do_typedef ("PTR",
5556 create_pointer (resolve_typedef ("void", &pos)),
5557 &pos));
5558 #undef POS_HERE
5559 read_input_list (inputlist);
5560 for (i = 0; i < num_gt_files; i++)
5562 parse_file (get_input_file_name (gt_files[i]));
5563 DBGPRINTF ("parsed file #%d %s",
5564 (int) i, get_input_file_name (gt_files[i]));
5566 if (verbosity_level >= 1)
5567 printf ("%s parsed %d files with %d GTY types\n",
5568 progname, (int) num_gt_files, type_count);
5570 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5571 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5574 else
5575 fatal ("either an input list or a read state file should be given");
5576 if (hit_error)
5577 return 1;
5580 if (plugin_output_filename)
5582 size_t ix = 0;
5583 /* In plugin mode, we should have read a state file, and have
5584 given at least one plugin file. */
5585 if (!read_state_filename)
5586 fatal ("No read state given in plugin mode for %s",
5587 plugin_output_filename);
5589 if (nb_plugin_files == 0 || !plugin_files)
5590 fatal ("No plugin files given in plugin mode for %s",
5591 plugin_output_filename);
5593 /* Parse our plugin files and augment the state. */
5594 for (ix = 0; ix < nb_plugin_files; ix++)
5596 input_file* pluginput = plugin_files [ix];
5597 pluginput->inpisplugin = true;
5598 parse_file (get_input_file_name (pluginput));
5600 if (hit_error)
5601 return 1;
5603 plugin_output = create_file ("GCC", plugin_output_filename);
5604 DBGPRINTF ("created plugin_output %p named %s",
5605 (void *) plugin_output, plugin_output->name);
5607 else
5608 { /* No plugin files, we are in normal mode. */
5609 if (!srcdir)
5610 fatal ("gengtype needs a source directory in normal mode");
5612 if (hit_error)
5613 return 1;
5615 gen_rtx_next ();
5617 /* The call to set_gc_used may indirectly call find_param_structure
5618 hence enlarge the param_structs list of types. */
5619 set_gc_used (variables);
5621 /* The state at this point is read from the state input file or by
5622 parsing source files and optionally augmented by parsing plugin
5623 source files. Write it now. */
5624 if (write_state_filename)
5626 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5627 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5629 if (hit_error)
5630 fatal ("didn't write state file %s after errors",
5631 write_state_filename);
5633 DBGPRINTF ("before write_state %s", write_state_filename);
5634 write_state (write_state_filename);
5636 if (do_dump)
5637 dump_everything ();
5639 /* After having written the state file we return immediately to
5640 avoid generating any output file. */
5641 if (hit_error)
5642 return 1;
5643 else
5644 return 0;
5648 open_base_files ();
5650 write_enum_defn (structures, param_structs);
5651 output_header = plugin_output ? plugin_output : header_file;
5652 write_typed_alloc_defns (output_header, structures, typedefs);
5653 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5654 structures);
5655 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5656 param_structs);
5658 write_types (output_header, structures, param_structs, &ggc_wtd);
5659 if (plugin_files == NULL)
5661 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5662 structures);
5663 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5664 param_structs);
5665 write_types (header_file, structures, param_structs, &pch_wtd);
5666 write_local (header_file, structures, param_structs);
5668 write_splay_tree_allocators (param_structs);
5669 write_roots (variables, plugin_files == NULL);
5670 write_rtx_next ();
5671 close_output_files ();
5673 if (do_dump)
5674 dump_everything ();
5676 /* Don't bother about free-ing any input or plugin file, etc. */
5678 if (hit_error)
5679 return 1;
5680 return 0;