* trans-mem.c (execute_tm_mark): Release bb_regions.
[official-gcc.git] / gcc / gengtype.c
blobb3f73fe924b0d3539eea1c4a295a5039aee631e3
1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
3 2012
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #ifdef GENERATOR_FILE
23 #include "bconfig.h"
24 #else
25 #include "config.h"
26 #endif
27 #include "system.h"
28 #include "errors.h" /* for fatal */
29 #include "getopt.h"
30 #include "double-int.h"
31 #include "version.h" /* for version_string & pkgversion_string. */
32 #include "hashtab.h"
33 #include "xregex.h"
34 #include "obstack.h"
35 #include "gengtype.h"
36 #include "filenames.h"
38 /* Data types, macros, etc. used only in this file. */
41 /* The list of output files. */
42 outf_p output_files;
44 /* The output header file that is included into pretty much every
45 source file. */
46 outf_p header_file;
49 /* The name of the file containing the list of input files. */
50 static char *inputlist;
52 /* The plugin input files and their number; in that case only
53 a single file is produced. */
54 static input_file **plugin_files;
55 static size_t nb_plugin_files;
57 /* The generated plugin output file and name. */
58 static outf_p plugin_output;
59 static char *plugin_output_filename;
61 /* Our source directory and its length. */
62 const char *srcdir;
63 size_t srcdir_len;
65 /* Variables used for reading and writing the state. */
66 const char *read_state_filename;
67 const char *write_state_filename;
69 /* Variables to help debugging. */
70 int do_dump;
71 int do_debug;
73 /* Level for verbose messages. */
74 int verbosity_level;
76 /* We have a type count and use it to set the state_number of newly
77 allocated types to some unique negative number. */
78 static int type_count;
80 /* The backup directory should be in the same file system as the
81 generated files, otherwise the rename(2) system call would fail.
82 If NULL, no backup is made when overwriting a generated file. */
83 static const char* backup_dir; /* (-B) program option. */
86 static outf_p create_file (const char *, const char *);
88 static const char *get_file_basename (const input_file *);
89 static const char *get_file_realbasename (const input_file *);
91 static int get_prefix_langdir_index (const char *);
92 static const char *get_file_langdir (const input_file *);
94 static void dump_pair (int indent, pair_p p);
95 static void dump_type (int indent, type_p p);
96 static void dump_type_list (int indent, type_p p);
99 /* Nonzero iff an error has occurred. */
100 bool hit_error = false;
102 static void gen_rtx_next (void);
103 static void write_rtx_next (void);
104 static void open_base_files (void);
105 static void close_output_files (void);
107 /* Report an error at POS, printing MSG. */
109 void
110 error_at_line (const struct fileloc *pos, const char *msg, ...)
112 va_list ap;
114 gcc_assert (pos != NULL && pos->file != NULL);
115 va_start (ap, msg);
117 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
118 vfprintf (stderr, msg, ap);
119 fputc ('\n', stderr);
120 hit_error = true;
122 va_end (ap);
125 /* asprintf, but produces fatal message on out-of-memory. */
126 char *
127 xasprintf (const char *format, ...)
129 int n;
130 char *result;
131 va_list ap;
133 va_start (ap, format);
134 n = vasprintf (&result, format, ap);
135 if (result == NULL || n < 0)
136 fatal ("out of memory");
137 va_end (ap);
139 return result;
142 /* Input file handling. */
144 /* Table of all input files. */
145 const input_file **gt_files;
146 size_t num_gt_files;
148 /* A number of places use the name of this "gengtype.c" file for a
149 location for things that we can't rely on the source to define.
150 Make sure we can still use pointer comparison on filenames. */
151 input_file* this_file;
152 /* The "system.h" file is likewise specially useful. */
153 input_file* system_h_file;
155 /* Vector of per-language directories. */
156 const char **lang_dir_names;
157 size_t num_lang_dirs;
159 /* An array of output files suitable for definitions. There is one
160 BASE_FILES entry for each language. */
161 static outf_p *base_files;
165 #if ENABLE_CHECKING
166 /* Utility debugging function, printing the various type counts within
167 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
168 void
169 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
171 int nb_types = 0, nb_scalar = 0, nb_string = 0;
172 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
173 int nb_lang_struct = 0, nb_param_struct = 0;
174 int nb_user_struct = 0, nb_undefined = 0;
175 type_p p = NULL;
176 for (p = t; p; p = p->next)
178 nb_types++;
179 switch (p->kind)
181 case TYPE_UNDEFINED:
182 nb_undefined++;
183 case TYPE_SCALAR:
184 nb_scalar++;
185 break;
186 case TYPE_STRING:
187 nb_string++;
188 break;
189 case TYPE_STRUCT:
190 nb_struct++;
191 break;
192 case TYPE_USER_STRUCT:
193 nb_user_struct++;
194 break;
195 case TYPE_UNION:
196 nb_union++;
197 break;
198 case TYPE_POINTER:
199 nb_pointer++;
200 break;
201 case TYPE_ARRAY:
202 nb_array++;
203 break;
204 case TYPE_LANG_STRUCT:
205 nb_lang_struct++;
206 break;
207 case TYPE_PARAM_STRUCT:
208 nb_param_struct++;
209 break;
210 case TYPE_NONE:
211 gcc_unreachable ();
214 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
215 lbasename (fil), lin, msg, nb_types);
216 if (nb_scalar > 0 || nb_string > 0)
217 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
218 if (nb_struct > 0 || nb_union > 0)
219 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
220 if (nb_pointer > 0 || nb_array > 0)
221 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
222 if (nb_lang_struct > 0 || nb_param_struct > 0)
223 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
224 nb_lang_struct, nb_param_struct);
225 if (nb_user_struct > 0)
226 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
227 if (nb_undefined > 0)
228 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
229 fprintf (stderr, "\n");
231 #endif /* ENABLE_CHECKING */
233 /* Scan the input file, LIST, and determine how much space we need to
234 store strings in. Also, count the number of language directories
235 and files. The numbers returned are overestimates as they does not
236 consider repeated files. */
237 static size_t
238 measure_input_list (FILE *list)
240 size_t n = 0;
241 int c;
242 bool atbol = true;
243 num_lang_dirs = 0;
244 num_gt_files = plugin_files ? nb_plugin_files : 0;
245 while ((c = getc (list)) != EOF)
247 n++;
248 if (atbol)
250 if (c == '[')
251 num_lang_dirs++;
252 else
254 /* Add space for a lang_bitmap before the input file name. */
255 n += sizeof (lang_bitmap);
256 num_gt_files++;
258 atbol = false;
261 if (c == '\n')
262 atbol = true;
265 rewind (list);
266 return n;
269 /* Read one input line from LIST to HEREP (which is updated). A
270 pointer to the string is returned via LINEP. If it was a language
271 subdirectory in square brackets, strip off the square brackets and
272 return true. Otherwise, leave space before the string for a
273 lang_bitmap, and return false. At EOF, returns false, does not
274 touch *HEREP, and sets *LINEP to NULL. POS is used for
275 diagnostics. */
276 static bool
277 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
279 char *here = *herep;
280 char *line;
281 int c = getc (list);
283 /* Read over whitespace. */
284 while (c == '\n' || c == ' ')
285 c = getc (list);
287 if (c == EOF)
289 *linep = 0;
290 return false;
292 else if (c == '[')
294 /* No space for a lang_bitmap is necessary. Discard the '['. */
295 c = getc (list);
296 line = here;
297 while (c != ']' && c != '\n' && c != EOF)
299 *here++ = c;
300 c = getc (list);
302 *here++ = '\0';
304 if (c == ']')
306 c = getc (list); /* eat what should be a newline */
307 if (c != '\n' && c != EOF)
308 error_at_line (pos, "junk on line after language tag [%s]", line);
310 else
311 error_at_line (pos, "missing close bracket for language tag [%s",
312 line);
314 *herep = here;
315 *linep = line;
316 return true;
318 else
320 /* Leave space for a lang_bitmap. */
321 memset (here, 0, sizeof (lang_bitmap));
322 here += sizeof (lang_bitmap);
323 line = here;
326 *here++ = c;
327 c = getc (list);
329 while (c != EOF && c != '\n');
330 *here++ = '\0';
331 *herep = here;
332 *linep = line;
333 return false;
337 /* Read the list of input files from LIST and compute all of the
338 relevant tables. There is one file per line of the list. At
339 first, all the files on the list are language-generic, but
340 eventually a line will appear which is the name of a language
341 subdirectory in square brackets, like this: [cp]. All subsequent
342 files are specific to that language, until another language
343 subdirectory tag appears. Files can appear more than once, if
344 they apply to more than one language. */
345 static void
346 read_input_list (const char *listname)
348 FILE *list = fopen (listname, "r");
349 if (!list)
350 fatal ("cannot open %s: %s", listname, xstrerror (errno));
351 else
353 struct fileloc epos;
354 size_t bufsz = measure_input_list (list);
355 char *buf = XNEWVEC (char, bufsz);
356 char *here = buf;
357 char *committed = buf;
358 char *limit = buf + bufsz;
359 char *line;
360 bool is_language;
361 size_t langno = 0;
362 size_t nfiles = 0;
363 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
365 epos.file = input_file_by_name (listname);
366 epos.line = 0;
368 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
369 gt_files = XNEWVEC (const input_file *, num_gt_files);
371 for (;;)
373 next_line:
374 epos.line++;
375 committed = here;
376 is_language = read_input_line (list, &here, &line, &epos);
377 gcc_assert (here <= limit);
378 if (line == 0)
379 break;
380 else if (is_language)
382 size_t i;
383 gcc_assert (langno <= num_lang_dirs);
384 for (i = 0; i < langno; i++)
385 if (strcmp (lang_dir_names[i], line) == 0)
387 error_at_line (&epos, "duplicate language tag [%s]",
388 line);
389 curlangs = 1 << i;
390 here = committed;
391 goto next_line;
394 curlangs = 1 << langno;
395 lang_dir_names[langno++] = line;
397 else
399 size_t i;
400 input_file *inpf = input_file_by_name (line);
401 gcc_assert (nfiles <= num_gt_files);
402 for (i = 0; i < nfiles; i++)
403 /* Since the input_file-s are uniquely hash-consed, we
404 can just compare pointers! */
405 if (gt_files[i] == inpf)
407 /* Throw away the string we just read, and add the
408 current language to the existing string's bitmap. */
409 lang_bitmap bmap = get_lang_bitmap (inpf);
410 if (bmap & curlangs)
411 error_at_line (&epos,
412 "file %s specified more than once "
413 "for language %s", line,
414 langno ==
415 0 ? "(all)" : lang_dir_names[langno -
416 1]);
418 bmap |= curlangs;
419 set_lang_bitmap (inpf, bmap);
420 here = committed;
421 goto next_line;
424 set_lang_bitmap (inpf, curlangs);
425 gt_files[nfiles++] = inpf;
428 /* Update the global counts now that we know accurately how many
429 things there are. (We do not bother resizing the arrays down.) */
430 num_lang_dirs = langno;
431 /* Add the plugin files if provided. */
432 if (plugin_files)
434 size_t i;
435 for (i = 0; i < nb_plugin_files; i++)
436 gt_files[nfiles++] = plugin_files[i];
438 num_gt_files = nfiles;
441 /* Sanity check: any file that resides in a language subdirectory
442 (e.g. 'cp') ought to belong to the corresponding language.
443 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
444 (Can you even do that? Should you be allowed to?) */
446 size_t f;
447 for (f = 0; f < num_gt_files; f++)
449 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
450 const char *basename = get_file_basename (gt_files[f]);
451 const char *slashpos = strchr (basename, '/');
452 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
453 const char *slashpos2 = strchr (basename, '\\');
455 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
456 slashpos = slashpos2;
457 #endif
459 if (slashpos)
461 size_t l;
462 for (l = 0; l < num_lang_dirs; l++)
463 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
464 && memcmp (basename, lang_dir_names[l],
465 strlen (lang_dir_names[l])) == 0)
467 if (!(bitmap & (1 << l)))
468 error ("%s is in language directory '%s' but is not "
469 "tagged for that language",
470 basename, lang_dir_names[l]);
471 break;
477 if (ferror (list))
478 fatal ("error reading %s: %s", listname, xstrerror (errno));
480 fclose (list);
485 /* The one and only TYPE_STRING. */
487 struct type string_type = {
488 TYPE_STRING, 0, 0, 0, GC_USED, {0}
491 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
492 set early in main. */
494 struct type scalar_nonchar = {
495 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
498 struct type scalar_char = {
499 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
502 /* Lists of various things. */
504 pair_p typedefs = NULL;
505 type_p structures = NULL;
506 type_p param_structs = NULL;
507 pair_p variables = NULL;
509 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
510 static type_p adjust_field_tree_exp (type_p t, options_p opt);
511 static type_p adjust_field_rtx_def (type_p t, options_p opt);
513 /* Define S as a typedef to T at POS. */
515 void
516 do_typedef (const char *s, type_p t, struct fileloc *pos)
518 pair_p p;
520 /* temporary kludge - gengtype doesn't handle conditionals or
521 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
522 is coming from this file (main() sets them up with safe dummy
523 definitions). */
524 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
525 return;
527 for (p = typedefs; p != NULL; p = p->next)
528 if (strcmp (p->name, s) == 0)
530 if (p->type != t)
532 error_at_line (pos, "type `%s' previously defined", s);
533 error_at_line (&p->line, "previously defined here");
535 return;
538 p = XNEW (struct pair);
539 p->next = typedefs;
540 p->name = s;
541 p->type = t;
542 p->line = *pos;
543 p->opt = NULL;
544 typedefs = p;
547 /* Define S as a typename of a scalar. Cannot be used to define
548 typedefs of 'char'. Note: is also used for pointer-to-function
549 typedefs (which are therefore not treated as pointers). */
551 void
552 do_scalar_typedef (const char *s, struct fileloc *pos)
554 do_typedef (s, &scalar_nonchar, pos);
558 /* Define TYPE_NAME to be a user defined type at location POS. */
560 type_p
561 create_user_defined_type (const char *type_name, struct fileloc *pos)
563 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
564 ty->u.s.line = *pos;
565 ty->u.s.bitmap = get_lang_bitmap (pos->file);
566 do_typedef (type_name, ty, pos);
568 /* If TYPE_NAME specifies a template, create references to the types
569 in the template by pretending that each type is a field of TY.
570 This is needed to make sure that the types referenced by the
571 template are marked as used. */
572 char *str = xstrdup (type_name);
573 char *open_bracket = strchr (str, '<');
574 if (open_bracket)
576 /* We only accept simple template declarations (see
577 require_template_declaration), so we only need to parse a
578 comma-separated list of strings, implicitly assumed to
579 be type names. */
580 char *arg = open_bracket + 1;
581 char *type_id = strtok (arg, ",>");
582 pair_p fields = 0;
583 while (type_id)
585 /* Create a new field for every type found inside the template
586 parameter list. */
587 const char *field_name = xstrdup (type_id);
588 type_p arg_type = resolve_typedef (field_name, pos);
589 fields = create_field_at (fields, arg_type, field_name, 0, pos);
590 type_id = strtok (0, ",>");
593 /* Associate the field list to TY. */
594 ty->u.s.fields = fields;
596 free (str);
598 return ty;
602 /* Given a typedef name S, return its associated type. Return NULL if
603 S is not a registered type name. */
605 static type_p
606 type_for_name (const char *s)
608 pair_p p;
609 for (p = typedefs; p != NULL; p = p->next)
610 if (strcmp (p->name, s) == 0)
611 return p->type;
612 return NULL;
616 /* Create an undefined type with name S and location POS. Return the
617 newly created type. */
619 static type_p
620 create_undefined_type (const char *s, struct fileloc *pos)
622 type_p ty = find_structure (s, TYPE_UNDEFINED);
623 ty->u.s.line = *pos;
624 ty->u.s.bitmap = get_lang_bitmap (pos->file);
625 do_typedef (s, ty, pos);
626 return ty;
630 /* Return the type previously defined for S. Use POS to report errors. */
632 type_p
633 resolve_typedef (const char *s, struct fileloc *pos)
635 bool is_template_instance = (strchr (s, '<') != NULL);
636 type_p p = type_for_name (s);
638 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
639 type for regular type identifiers. If the type identifier S is a
640 template instantiation, however, we treat it as a user defined
641 type.
643 FIXME, this is actually a limitation in gengtype. Supporting
644 template types and their instances would require keeping separate
645 track of the basic types definition and its instances. This
646 essentially forces all template classes in GC to be marked
647 GTY((user)). */
648 if (!p)
649 p = (is_template_instance)
650 ? create_user_defined_type (s, pos)
651 : create_undefined_type (s, pos);
653 return p;
657 /* Create and return a new structure with tag NAME at POS with fields
658 FIELDS and options O. The KIND of structure must be one of
659 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
661 type_p
662 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
663 pair_p fields, options_p o)
665 type_p si;
666 type_p s = NULL;
667 lang_bitmap bitmap = get_lang_bitmap (pos->file);
668 bool isunion = (kind == TYPE_UNION);
670 gcc_assert (union_or_struct_p (kind));
672 for (si = structures; si != NULL; si = si->next)
673 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
675 type_p ls = NULL;
676 if (si->kind == TYPE_LANG_STRUCT)
678 ls = si;
680 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
681 if (si->u.s.bitmap == bitmap)
682 s = si;
684 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
686 ls = si;
687 type_count++;
688 si = XCNEW (struct type);
689 memcpy (si, ls, sizeof (struct type));
690 ls->kind = TYPE_LANG_STRUCT;
691 ls->u.s.lang_struct = si;
692 ls->u.s.fields = NULL;
693 si->next = NULL;
694 si->state_number = -type_count;
695 si->pointer_to = NULL;
696 si->u.s.lang_struct = ls;
698 else
699 s = si;
701 if (ls != NULL && s == NULL)
703 type_count++;
704 s = XCNEW (struct type);
705 s->state_number = -type_count;
706 s->next = ls->u.s.lang_struct;
707 ls->u.s.lang_struct = s;
708 s->u.s.lang_struct = ls;
710 break;
713 if (s == NULL)
715 type_count++;
716 s = XCNEW (struct type);
717 s->state_number = -type_count;
718 s->next = structures;
719 structures = s;
722 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
724 error_at_line (pos, "duplicate definition of '%s %s'",
725 isunion ? "union" : "struct", s->u.s.tag);
726 error_at_line (&s->u.s.line, "previous definition here");
729 s->kind = kind;
730 s->u.s.tag = name;
731 s->u.s.line = *pos;
732 s->u.s.fields = fields;
733 s->u.s.opt = o;
734 s->u.s.bitmap = bitmap;
735 if (s->u.s.lang_struct)
736 s->u.s.lang_struct->u.s.bitmap |= bitmap;
738 return s;
741 /* Return the previously-defined structure or union with tag NAME,
742 or a new empty structure or union if none was defined previously.
743 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
744 TYPE_USER_STRUCT. */
746 type_p
747 find_structure (const char *name, enum typekind kind)
749 type_p s;
750 bool isunion = (kind == TYPE_UNION);
752 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
754 for (s = structures; s != NULL; s = s->next)
755 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
756 return s;
758 type_count++;
759 s = XCNEW (struct type);
760 s->next = structures;
761 s->state_number = -type_count;
762 structures = s;
763 s->kind = kind;
764 s->u.s.tag = name;
765 structures = s;
766 return s;
769 /* Return the previously-defined parameterized structure for structure
770 T and parameters PARAM, or a new parameterized empty structure or
771 union if none was defined previously. */
773 static type_p
774 find_param_structure (type_p t, type_p param[NUM_PARAM])
776 type_p res;
778 for (res = param_structs; res; res = res->next)
779 if (res->u.param_struct.stru == t
780 && memcmp (res->u.param_struct.param, param,
781 sizeof (type_p) * NUM_PARAM) == 0)
782 break;
783 if (res == NULL)
785 type_count++;
786 res = XCNEW (struct type);
787 res->kind = TYPE_PARAM_STRUCT;
788 res->next = param_structs;
789 res->state_number = -type_count;
790 param_structs = res;
791 res->u.param_struct.stru = t;
792 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
794 return res;
797 /* Return a scalar type with name NAME. */
799 type_p
800 create_scalar_type (const char *name)
802 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
803 return &scalar_char;
804 else
805 return &scalar_nonchar;
809 /* Return a pointer to T. */
811 type_p
812 create_pointer (type_p t)
814 if (!t->pointer_to)
816 type_p r = XCNEW (struct type);
817 type_count++;
818 r->state_number = -type_count;
819 r->kind = TYPE_POINTER;
820 r->u.p = t;
821 t->pointer_to = r;
823 return t->pointer_to;
826 /* Return an array of length LEN. */
828 type_p
829 create_array (type_p t, const char *len)
831 type_p v;
833 type_count++;
834 v = XCNEW (struct type);
835 v->kind = TYPE_ARRAY;
836 v->state_number = -type_count;
837 v->u.a.p = t;
838 v->u.a.len = len;
839 return v;
842 /* Return a string options structure with name NAME and info INFO.
843 NEXT is the next option in the chain. */
844 options_p
845 create_string_option (options_p next, const char *name, const char *info)
847 options_p o = XNEW (struct options);
848 o->kind = OPTION_STRING;
849 o->next = next;
850 o->name = name;
851 o->info.string = info;
852 return o;
855 /* Create a type options structure with name NAME and info INFO. NEXT
856 is the next option in the chain. */
857 options_p
858 create_type_option (options_p next, const char* name, type_p info)
860 options_p o = XNEW (struct options);
861 o->next = next;
862 o->name = name;
863 o->kind = OPTION_TYPE;
864 o->info.type = info;
865 return o;
868 /* Create a nested pointer options structure with name NAME and info
869 INFO. NEXT is the next option in the chain. */
870 options_p
871 create_nested_option (options_p next, const char* name,
872 struct nested_ptr_data* info)
874 options_p o;
875 o = XNEW (struct options);
876 o->next = next;
877 o->name = name;
878 o->kind = OPTION_NESTED;
879 o->info.nested = info;
880 return o;
883 /* Return an options structure for a "nested_ptr" option. */
884 options_p
885 create_nested_ptr_option (options_p next, type_p t,
886 const char *to, const char *from)
888 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
890 d->type = adjust_field_type (t, 0);
891 d->convert_to = to;
892 d->convert_from = from;
893 return create_nested_option (next, "nested_ptr", d);
896 /* Add a variable named S of type T with options O defined at POS,
897 to `variables'. */
898 void
899 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
901 pair_p n;
902 n = XNEW (struct pair);
903 n->name = s;
904 n->type = t;
905 n->line = *pos;
906 n->opt = o;
907 n->next = variables;
908 variables = n;
911 /* Most-general structure field creator. */
912 static pair_p
913 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
914 const input_file *inpf, int line)
916 pair_p field;
918 field = XNEW (struct pair);
919 field->next = next;
920 field->type = type;
921 field->name = name;
922 field->opt = opt;
923 field->line.file = inpf;
924 field->line.line = line;
925 return field;
928 /* Create a field that came from the source code we are scanning,
929 i.e. we have a 'struct fileloc', and possibly options; also,
930 adjust_field_type should be called. */
931 pair_p
932 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
933 struct fileloc *pos)
935 return create_field_all (next, adjust_field_type (type, opt),
936 name, opt, pos->file, pos->line);
939 /* Create a fake field with the given type and name. NEXT is the next
940 field in the chain. */
941 #define create_field(next,type,name) \
942 create_field_all(next,type,name, 0, this_file, __LINE__)
944 /* Like create_field, but the field is only valid when condition COND
945 is true. */
947 static pair_p
948 create_optional_field_ (pair_p next, type_p type, const char *name,
949 const char *cond, int line)
951 static int id = 1;
952 pair_p union_fields;
953 type_p union_type;
955 /* Create a fake union type with a single nameless field of type TYPE.
956 The field has a tag of "1". This allows us to make the presence
957 of a field of type TYPE depend on some boolean "desc" being true. */
958 union_fields = create_field (NULL, type, "");
959 union_fields->opt =
960 create_string_option (union_fields->opt, "dot", "");
961 union_fields->opt =
962 create_string_option (union_fields->opt, "tag", "1");
963 union_type =
964 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
965 &lexer_line, union_fields, NULL);
967 /* Create the field and give it the new fake union type. Add a "desc"
968 tag that specifies the condition under which the field is valid. */
969 return create_field_all (next, union_type, name,
970 create_string_option (0, "desc", cond),
971 this_file, line);
974 #define create_optional_field(next,type,name,cond) \
975 create_optional_field_(next,type,name,cond,__LINE__)
977 /* Reverse a linked list of 'struct pair's in place. */
978 pair_p
979 nreverse_pairs (pair_p list)
981 pair_p prev = 0, p, next;
982 for (p = list; p; p = next)
984 next = p->next;
985 p->next = prev;
986 prev = p;
988 return prev;
992 /* We don't care how long a CONST_DOUBLE is. */
993 #define CONST_DOUBLE_FORMAT "ww"
994 /* We don't want to see codes that are only for generator files. */
995 #undef GENERATOR_FILE
997 enum rtx_code
999 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1000 #include "rtl.def"
1001 #undef DEF_RTL_EXPR
1002 NUM_RTX_CODE
1005 static const char *const rtx_name[NUM_RTX_CODE] = {
1006 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1007 #include "rtl.def"
1008 #undef DEF_RTL_EXPR
1011 static const char *const rtx_format[NUM_RTX_CODE] = {
1012 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1013 #include "rtl.def"
1014 #undef DEF_RTL_EXPR
1017 static int rtx_next_new[NUM_RTX_CODE];
1019 /* We also need codes and names for insn notes (not register notes).
1020 Note that we do *not* bias the note values here. */
1021 enum insn_note
1023 #define DEF_INSN_NOTE(NAME) NAME,
1024 #include "insn-notes.def"
1025 #undef DEF_INSN_NOTE
1027 NOTE_INSN_MAX
1030 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1031 default field for line number notes. */
1032 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1033 #define DEF_INSN_NOTE(NAME) #NAME,
1034 #include "insn-notes.def"
1035 #undef DEF_INSN_NOTE
1038 #undef CONST_DOUBLE_FORMAT
1039 #define GENERATOR_FILE
1041 /* Generate the contents of the rtx_next array. This really doesn't belong
1042 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1044 static void
1045 gen_rtx_next (void)
1047 int i;
1048 for (i = 0; i < NUM_RTX_CODE; i++)
1050 int k;
1052 rtx_next_new[i] = -1;
1053 if (strncmp (rtx_format[i], "iuu", 3) == 0)
1054 rtx_next_new[i] = 2;
1055 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1056 rtx_next_new[i] = 1;
1057 else
1058 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1059 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1060 rtx_next_new[i] = k;
1064 /* Write out the contents of the rtx_next array. */
1065 static void
1066 write_rtx_next (void)
1068 outf_p f = get_output_file_with_visibility (NULL);
1069 int i;
1070 if (!f)
1071 return;
1073 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1074 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1075 for (i = 0; i < NUM_RTX_CODE; i++)
1076 if (rtx_next_new[i] == -1)
1077 oprintf (f, " 0,\n");
1078 else
1079 oprintf (f,
1080 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1081 oprintf (f, "};\n");
1084 /* Handle `special("rtx_def")'. This is a special case for field
1085 `fld' of struct rtx_def, which is an array of unions whose values
1086 are based in a complex way on the type of RTL. */
1088 static type_p
1089 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1091 pair_p flds = NULL;
1092 options_p nodot;
1093 int i;
1094 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1095 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1097 if (t->kind != TYPE_UNION)
1099 error_at_line (&lexer_line,
1100 "special `rtx_def' must be applied to a union");
1101 return &string_type;
1104 nodot = create_string_option (NULL, "dot", "");
1106 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1107 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1108 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1109 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1110 reg_attrs_tp =
1111 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1112 basic_block_tp =
1113 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1114 constant_tp =
1115 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1116 scalar_tp = &scalar_nonchar; /* rtunion int */
1119 pair_p note_flds = NULL;
1120 int c;
1122 for (c = 0; c <= NOTE_INSN_MAX; c++)
1124 switch (c)
1126 case NOTE_INSN_MAX:
1127 case NOTE_INSN_DELETED_LABEL:
1128 case NOTE_INSN_DELETED_DEBUG_LABEL:
1129 note_flds = create_field (note_flds, &string_type, "rt_str");
1130 break;
1132 case NOTE_INSN_BLOCK_BEG:
1133 case NOTE_INSN_BLOCK_END:
1134 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1135 break;
1137 case NOTE_INSN_VAR_LOCATION:
1138 case NOTE_INSN_CALL_ARG_LOCATION:
1139 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1140 break;
1142 default:
1143 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1144 break;
1146 /* NOTE_INSN_MAX is used as the default field for line
1147 number notes. */
1148 if (c == NOTE_INSN_MAX)
1149 note_flds->opt =
1150 create_string_option (nodot, "default", "");
1151 else
1152 note_flds->opt =
1153 create_string_option (nodot, "tag", note_insn_name[c]);
1155 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1156 &lexer_line, note_flds, NULL);
1158 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1160 pair_p sym_flds;
1161 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1162 sym_flds->opt = create_string_option (nodot, "default", "");
1163 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1164 sym_flds->opt = create_string_option (nodot, "tag", "1");
1165 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1166 &lexer_line, sym_flds, NULL);
1168 for (i = 0; i < NUM_RTX_CODE; i++)
1170 pair_p subfields = NULL;
1171 size_t aindex, nmindex;
1172 const char *sname;
1173 type_p substruct;
1174 char *ftag;
1176 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1178 type_p t;
1179 const char *subname;
1181 switch (rtx_format[i][aindex])
1183 case '*':
1184 case 'i':
1185 case 'n':
1186 case 'w':
1187 t = scalar_tp;
1188 subname = "rt_int";
1189 break;
1191 case '0':
1192 if (i == MEM && aindex == 1)
1193 t = mem_attrs_tp, subname = "rt_mem";
1194 else if (i == JUMP_INSN && aindex == 8)
1195 t = rtx_tp, subname = "rt_rtx";
1196 else if (i == CODE_LABEL && aindex == 5)
1197 t = scalar_tp, subname = "rt_int";
1198 else if (i == CODE_LABEL && aindex == 4)
1199 t = rtx_tp, subname = "rt_rtx";
1200 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1201 t = rtx_tp, subname = "rt_rtx";
1202 else if (i == NOTE && aindex == 4)
1203 t = note_union_tp, subname = "";
1204 else if (i == NOTE && aindex == 5)
1205 t = scalar_tp, subname = "rt_int";
1206 else if (i == NOTE && aindex >= 7)
1207 t = scalar_tp, subname = "rt_int";
1208 else if (i == ADDR_DIFF_VEC && aindex == 4)
1209 t = scalar_tp, subname = "rt_int";
1210 else if (i == VALUE && aindex == 0)
1211 t = scalar_tp, subname = "rt_int";
1212 else if (i == DEBUG_EXPR && aindex == 0)
1213 t = tree_tp, subname = "rt_tree";
1214 else if (i == REG && aindex == 1)
1215 t = scalar_tp, subname = "rt_int";
1216 else if (i == REG && aindex == 2)
1217 t = reg_attrs_tp, subname = "rt_reg";
1218 else if (i == SCRATCH && aindex == 0)
1219 t = scalar_tp, subname = "rt_int";
1220 else if (i == SYMBOL_REF && aindex == 1)
1221 t = scalar_tp, subname = "rt_int";
1222 else if (i == SYMBOL_REF && aindex == 2)
1223 t = symbol_union_tp, subname = "";
1224 else if (i == BARRIER && aindex >= 3)
1225 t = scalar_tp, subname = "rt_int";
1226 else if (i == ENTRY_VALUE && aindex == 0)
1227 t = rtx_tp, subname = "rt_rtx";
1228 else
1230 error_at_line
1231 (&lexer_line,
1232 "rtx type `%s' has `0' in position %lu, can't handle",
1233 rtx_name[i], (unsigned long) aindex);
1234 t = &string_type;
1235 subname = "rt_int";
1237 break;
1239 case 's':
1240 case 'S':
1241 case 'T':
1242 t = &string_type;
1243 subname = "rt_str";
1244 break;
1246 case 'e':
1247 case 'u':
1248 t = rtx_tp;
1249 subname = "rt_rtx";
1250 break;
1252 case 'E':
1253 case 'V':
1254 t = rtvec_tp;
1255 subname = "rt_rtvec";
1256 break;
1258 case 't':
1259 t = tree_tp;
1260 subname = "rt_tree";
1261 break;
1263 case 'B':
1264 t = basic_block_tp;
1265 subname = "rt_bb";
1266 break;
1268 default:
1269 error_at_line
1270 (&lexer_line,
1271 "rtx type `%s' has `%c' in position %lu, can't handle",
1272 rtx_name[i], rtx_format[i][aindex],
1273 (unsigned long) aindex);
1274 t = &string_type;
1275 subname = "rt_int";
1276 break;
1279 subfields = create_field (subfields, t,
1280 xasprintf (".fld[%lu].%s",
1281 (unsigned long) aindex,
1282 subname));
1283 subfields->opt = nodot;
1284 if (t == note_union_tp)
1285 subfields->opt =
1286 create_string_option (subfields->opt, "desc",
1287 "NOTE_KIND (&%0)");
1288 if (t == symbol_union_tp)
1289 subfields->opt =
1290 create_string_option (subfields->opt, "desc",
1291 "CONSTANT_POOL_ADDRESS_P (&%0)");
1294 if (i == SYMBOL_REF)
1296 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1297 holds. */
1298 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1299 subfields
1300 = create_optional_field (subfields, field_tp, "block_sym",
1301 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1304 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1305 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1306 NULL);
1308 ftag = xstrdup (rtx_name[i]);
1309 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1310 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1311 flds = create_field (flds, substruct, "");
1312 flds->opt = create_string_option (nodot, "tag", ftag);
1314 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1315 nodot);
1318 /* Handle `special("tree_exp")'. This is a special case for
1319 field `operands' of struct tree_exp, which although it claims to contain
1320 pointers to trees, actually sometimes contains pointers to RTL too.
1321 Passed T, the old type of the field, and OPT its options. Returns
1322 a new type for the field. */
1324 static type_p
1325 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1327 pair_p flds;
1328 options_p nodot;
1330 if (t->kind != TYPE_ARRAY)
1332 error_at_line (&lexer_line,
1333 "special `tree_exp' must be applied to an array");
1334 return &string_type;
1337 nodot = create_string_option (NULL, "dot", "");
1339 flds = create_field (NULL, t, "");
1340 flds->opt = create_string_option (nodot, "length",
1341 "TREE_OPERAND_LENGTH ((tree) &%0)");
1342 flds->opt = create_string_option (flds->opt, "default", "");
1344 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1345 nodot);
1348 /* Perform any special processing on a type T, about to become the type
1349 of a field. Return the appropriate type for the field.
1350 At present:
1351 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1352 - Similarly for arrays of pointer-to-char;
1353 - Converts structures for which a parameter is provided to
1354 TYPE_PARAM_STRUCT;
1355 - Handles "special" options.
1358 type_p
1359 adjust_field_type (type_p t, options_p opt)
1361 int length_p = 0;
1362 const int pointer_p = t->kind == TYPE_POINTER;
1363 type_p params[NUM_PARAM];
1364 int params_p = 0;
1365 int i;
1367 for (i = 0; i < NUM_PARAM; i++)
1368 params[i] = NULL;
1370 for (; opt; opt = opt->next)
1371 if (strcmp (opt->name, "length") == 0)
1373 if (length_p)
1374 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1375 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1377 error_at_line (&lexer_line,
1378 "option `%s' may not be applied to "
1379 "arrays of atomic types", opt->name);
1381 length_p = 1;
1383 else if ((strcmp (opt->name, "param_is") == 0
1384 || (strncmp (opt->name, "param", 5) == 0
1385 && ISDIGIT (opt->name[5])
1386 && strcmp (opt->name + 6, "_is") == 0))
1387 && opt->kind == OPTION_TYPE)
1389 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1391 if (!union_or_struct_p (t)
1392 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1394 error_at_line (&lexer_line,
1395 "option `%s' may only be applied to structures or structure pointers",
1396 opt->name);
1397 return t;
1400 params_p = 1;
1401 if (params[num] != NULL)
1402 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1403 if (!ISDIGIT (opt->name[5]))
1404 params[num] = create_pointer (opt->info.type);
1405 else
1406 params[num] = opt->info.type;
1408 else if (strcmp (opt->name, "special") == 0
1409 && opt->kind == OPTION_STRING)
1411 const char *special_name = opt->info.string;
1412 if (strcmp (special_name, "tree_exp") == 0)
1413 t = adjust_field_tree_exp (t, opt);
1414 else if (strcmp (special_name, "rtx_def") == 0)
1415 t = adjust_field_rtx_def (t, opt);
1416 else
1417 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1420 if (params_p)
1422 type_p realt;
1424 if (pointer_p)
1425 t = t->u.p;
1426 realt = find_param_structure (t, params);
1427 t = pointer_p ? create_pointer (realt) : realt;
1430 if (!length_p
1431 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1432 return &string_type;
1433 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1434 && t->u.a.p->u.p->kind == TYPE_SCALAR
1435 && t->u.a.p->u.p->u.scalar_is_char)
1436 return create_array (&string_type, t->u.a.len);
1438 return t;
1442 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1443 bool = false);
1444 static void set_gc_used (pair_p);
1446 /* Handle OPT for set_gc_used_type. */
1448 static void
1449 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1450 int *pass_param, int *length, int *skip,
1451 type_p *nested_ptr)
1453 options_p o;
1454 for (o = opt; o; o = o->next)
1455 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1456 && o->kind == OPTION_TYPE)
1457 set_gc_used_type (o->info.type,
1458 GC_POINTED_TO, NULL);
1459 else if (strcmp (o->name, "maybe_undef") == 0)
1460 *maybe_undef = 1;
1461 else if (strcmp (o->name, "use_params") == 0)
1462 *pass_param = 1;
1463 else if (strcmp (o->name, "length") == 0)
1464 *length = 1;
1465 else if (strcmp (o->name, "skip") == 0)
1466 *skip = 1;
1467 else if (strcmp (o->name, "nested_ptr") == 0
1468 && o->kind == OPTION_NESTED)
1469 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1473 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1475 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1476 are set to GC_UNUSED. Otherwise, an error is emitted for
1477 TYPE_UNDEFINED types. This is used to support user-defined
1478 template types with non-type arguments.
1480 For instance, when we parse a template type with enum arguments
1481 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1482 artificial fields for 'MyType', one for 'AnotherType', the other
1483 one for 'EnumValue'.
1485 At the time that we parse this type we don't know that 'EnumValue'
1486 is really an enum value, so the parser creates a TYPE_UNDEFINED
1487 type for it. Since 'EnumValue' is never resolved to a known
1488 structure, it will stay with TYPE_UNDEFINED.
1490 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1491 'EnumValue'. Generating marking code for it would cause
1492 compilation failures since the marking routines assumes that
1493 'EnumValue' is a type. */
1495 static void
1496 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1497 bool allow_undefined_types)
1499 if (t->gc_used >= level)
1500 return;
1502 t->gc_used = level;
1504 switch (t->kind)
1506 case TYPE_STRUCT:
1507 case TYPE_UNION:
1508 case TYPE_USER_STRUCT:
1510 pair_p f;
1511 int dummy;
1512 type_p dummy2;
1513 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1515 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1516 &dummy2);
1518 for (f = t->u.s.fields; f; f = f->next)
1520 int maybe_undef = 0;
1521 int pass_param = 0;
1522 int length = 0;
1523 int skip = 0;
1524 type_p nested_ptr = NULL;
1525 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1526 &length, &skip, &nested_ptr);
1528 if (nested_ptr && f->type->kind == TYPE_POINTER)
1529 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1530 pass_param ? param : NULL);
1531 else if (length && f->type->kind == TYPE_POINTER)
1532 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1533 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1534 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1535 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1536 set_gc_used_type (find_param_structure (f->type->u.p, param),
1537 GC_POINTED_TO, NULL);
1538 else if (skip)
1539 ; /* target type is not used through this field */
1540 else
1541 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1542 allow_undefined_field_types);
1544 break;
1547 case TYPE_UNDEFINED:
1548 if (level > GC_UNUSED)
1550 if (!allow_undefined_types)
1551 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1552 t->gc_used = GC_UNUSED;
1554 break;
1556 case TYPE_POINTER:
1557 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1558 break;
1560 case TYPE_ARRAY:
1561 set_gc_used_type (t->u.a.p, GC_USED, param);
1562 break;
1564 case TYPE_LANG_STRUCT:
1565 for (t = t->u.s.lang_struct; t; t = t->next)
1566 set_gc_used_type (t, level, param);
1567 break;
1569 case TYPE_PARAM_STRUCT:
1571 int i;
1572 for (i = 0; i < NUM_PARAM; i++)
1573 if (t->u.param_struct.param[i] != 0)
1574 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1576 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1577 level = GC_POINTED_TO;
1578 else
1579 level = GC_USED;
1580 t->u.param_struct.stru->gc_used = GC_UNUSED;
1581 set_gc_used_type (t->u.param_struct.stru, level,
1582 t->u.param_struct.param);
1583 break;
1585 default:
1586 break;
1590 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1592 static void
1593 set_gc_used (pair_p variables)
1595 int nbvars = 0;
1596 pair_p p;
1597 for (p = variables; p; p = p->next)
1599 set_gc_used_type (p->type, GC_USED, NULL);
1600 nbvars++;
1602 if (verbosity_level >= 2)
1603 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1606 /* File mapping routines. For each input file, there is one output .c file
1607 (but some output files have many input files), and there is one .h file
1608 for the whole build. */
1610 /* Output file handling. */
1612 /* Create and return an outf_p for a new file for NAME, to be called
1613 ONAME. */
1615 static outf_p
1616 create_file (const char *name, const char *oname)
1618 static const char *const hdr[] = {
1619 " Copyright (C) 2004, 2007, 2009, 2012 Free Software Foundation, Inc.\n",
1620 "\n",
1621 "This file is part of GCC.\n",
1622 "\n",
1623 "GCC is free software; you can redistribute it and/or modify it under\n",
1624 "the terms of the GNU General Public License as published by the Free\n",
1625 "Software Foundation; either version 3, or (at your option) any later\n",
1626 "version.\n",
1627 "\n",
1628 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1629 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1630 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1631 "for more details.\n",
1632 "\n",
1633 "You should have received a copy of the GNU General Public License\n",
1634 "along with GCC; see the file COPYING3. If not see\n",
1635 "<http://www.gnu.org/licenses/>. */\n",
1636 "\n",
1637 "/* This file is machine generated. Do not edit. */\n"
1639 outf_p f;
1640 size_t i;
1642 gcc_assert (name != NULL);
1643 gcc_assert (oname != NULL);
1644 f = XCNEW (struct outf);
1645 f->next = output_files;
1646 f->name = oname;
1647 output_files = f;
1649 oprintf (f, "/* Type information for %s.\n", name);
1650 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1651 oprintf (f, "%s", hdr[i]);
1652 return f;
1655 /* Print, like fprintf, to O.
1656 N.B. You might think this could be implemented more efficiently
1657 with vsnprintf(). Unfortunately, there are C libraries that
1658 provide that function but without the C99 semantics for its return
1659 value, making it impossible to know how much space is required. */
1660 void
1661 oprintf (outf_p o, const char *format, ...)
1663 char *s;
1664 size_t slength;
1665 va_list ap;
1667 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1668 in that case. */
1669 if (!o)
1670 return;
1672 va_start (ap, format);
1673 slength = vasprintf (&s, format, ap);
1674 if (s == NULL || (int) slength < 0)
1675 fatal ("out of memory");
1676 va_end (ap);
1678 if (o->bufused + slength > o->buflength)
1680 size_t new_len = o->buflength;
1681 if (new_len == 0)
1682 new_len = 1024;
1685 new_len *= 2;
1687 while (o->bufused + slength >= new_len);
1688 o->buf = XRESIZEVEC (char, o->buf, new_len);
1689 o->buflength = new_len;
1691 memcpy (o->buf + o->bufused, s, slength);
1692 o->bufused += slength;
1693 free (s);
1696 /* Open the global header file and the language-specific header files. */
1698 static void
1699 open_base_files (void)
1701 size_t i;
1703 if (nb_plugin_files > 0 && plugin_files)
1704 return;
1706 header_file = create_file ("GCC", "gtype-desc.h");
1708 base_files = XNEWVEC (outf_p, num_lang_dirs);
1710 for (i = 0; i < num_lang_dirs; i++)
1711 base_files[i] = create_file (lang_dir_names[i],
1712 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1714 /* gtype-desc.c is a little special, so we create it here. */
1716 /* The order of files here matters very much. */
1717 static const char *const ifiles[] = {
1718 "config.h", "system.h", "coretypes.h", "tm.h",
1719 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1720 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1721 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1722 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1723 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1724 "except.h", "output.h", "gimple.h", "cfgloop.h",
1725 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1726 "ipa-inline.h", "dwarf2out.h", NULL
1728 const char *const *ifp;
1729 outf_p gtype_desc_c;
1731 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1732 for (ifp = ifiles; *ifp; ifp++)
1733 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1735 /* Make sure we handle "cfun" specially. */
1736 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1737 oprintf (gtype_desc_c, "#undef cfun\n");
1741 /* For INPF an input file, return the real basename of INPF, with all
1742 the directory components skipped. */
1744 static const char *
1745 get_file_realbasename (const input_file *inpf)
1747 return lbasename (get_input_file_name (inpf));
1750 /* For INPF a filename, return the relative path to INPF from
1751 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1753 const char *
1754 get_file_srcdir_relative_path (const input_file *inpf)
1756 const char *f = get_input_file_name (inpf);
1757 if (strlen (f) > srcdir_len
1758 && IS_DIR_SEPARATOR (f[srcdir_len])
1759 && strncmp (f, srcdir, srcdir_len) == 0)
1760 return f + srcdir_len + 1;
1761 else
1762 return NULL;
1765 /* For INPF an input_file, return the relative path to INPF from
1766 $(srcdir) if the latter is a prefix in INPF, or the real basename
1767 of INPF otherwise. */
1769 static const char *
1770 get_file_basename (const input_file *inpf)
1772 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1774 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1777 /* For F a filename, return the lang_dir_names relative index of the language
1778 directory that is a prefix in F, if any, -1 otherwise. */
1780 static int
1781 get_prefix_langdir_index (const char *f)
1783 size_t f_len = strlen (f);
1784 size_t lang_index;
1786 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1788 const char *langdir = lang_dir_names[lang_index];
1789 size_t langdir_len = strlen (langdir);
1791 if (f_len > langdir_len
1792 && IS_DIR_SEPARATOR (f[langdir_len])
1793 && memcmp (f, langdir, langdir_len) == 0)
1794 return lang_index;
1797 return -1;
1800 /* For INPF an input file, return the name of language directory where
1801 F is located, if any, NULL otherwise. */
1803 static const char *
1804 get_file_langdir (const input_file *inpf)
1806 /* Get the relative path to INPF from $(srcdir) and find the
1807 language by comparing the prefix with language directory names.
1808 If INPF is not even srcdir relative, no point in looking
1809 further. */
1811 int lang_index;
1812 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1813 const char *r;
1815 if (!srcdir_relative_path)
1816 return NULL;
1818 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1819 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1820 r = "c-family";
1821 else if (lang_index >= 0)
1822 r = lang_dir_names[lang_index];
1823 else
1824 r = NULL;
1826 return r;
1829 /* The gt- output file name for INPF. */
1831 static const char *
1832 get_file_gtfilename (const input_file *inpf)
1834 /* Cook up an initial version of the gt- file name from the file real
1835 basename and the language name, if any. */
1837 const char *basename = get_file_realbasename (inpf);
1838 const char *langdir = get_file_langdir (inpf);
1840 char *result =
1841 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1842 : xasprintf ("gt-%s", basename));
1844 /* Then replace all non alphanumerics characters by '-' and change the
1845 extension to ".h". We expect the input filename extension was at least
1846 one character long. */
1848 char *s = result;
1850 for (; *s != '.'; s++)
1851 if (!ISALNUM (*s) && *s != '-')
1852 *s = '-';
1854 memcpy (s, ".h", sizeof (".h"));
1856 return result;
1859 /* Each input_file has its associated output file outf_p. The
1860 association is computed by the function
1861 get_output_file_with_visibility. The associated file is cached
1862 inside input_file in its inpoutf field, so is really computed only
1863 once. Associated output file paths (i.e. output_name-s) are
1864 computed by a rule based regexp machinery, using the files_rules
1865 array of struct file_rule_st. A for_name is also computed, giving
1866 the source file name for which the output_file is generated; it is
1867 often the last component of the input_file path. */
1871 Regexpr machinery to compute the output_name and for_name-s of each
1872 input_file. We have a sequence of file rules which gives the POSIX
1873 extended regular expression to match an input file path, and two
1874 transformed strings for the corresponding output_name and the
1875 corresponding for_name. The transformed string contain dollars: $0
1876 is replaced by the entire match, $1 is replaced by the substring
1877 matching the first parenthesis in the regexp, etc. And $$ is replaced
1878 by a single verbatim dollar. The rule order is important. The
1879 general case is last, and the particular cases should come before.
1880 An action routine can, when needed, update the out_name & for_name
1881 and/or return the appropriate output file. It is invoked only when a
1882 rule is triggered. When a rule is triggered, the output_name and
1883 for_name are computed using their transform string in while $$, $0,
1884 $1, ... are suitably replaced. If there is an action, it is called.
1885 In some few cases, the action can directly return the outf_p, but
1886 usually it just updates the output_name and for_name so should free
1887 them before replacing them. The get_output_file_with_visibility
1888 function creates an outf_p only once per each output_name, so it
1889 scans the output_files list for previously seen output file names.
1892 /* Signature of actions in file rules. */
1893 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1896 struct file_rule_st {
1897 const char* frul_srcexpr; /* Source string for regexp. */
1898 int frul_rflags; /* Flags passed to regcomp, usually
1899 * REG_EXTENDED. */
1900 regex_t* frul_re; /* Compiled regular expression
1901 obtained by regcomp. */
1902 const char* frul_tr_out; /* Transformation string for making
1903 * the output_name, with $1 ... $9 for
1904 * subpatterns and $0 for the whole
1905 * matched filename. */
1906 const char* frul_tr_for; /* Tranformation string for making the
1907 for_name. */
1908 frul_actionrout_t* frul_action; /* The action, if non null, is
1909 * called once the rule matches, on
1910 * the transformed out_name &
1911 * for_name. It could change them
1912 * and/or give the output file. */
1915 /* File rule action handling *.h files. */
1916 static outf_p header_dot_h_frul (input_file*, char**, char**);
1918 /* File rule action handling *.c files. */
1919 static outf_p source_dot_c_frul (input_file*, char**, char**);
1921 #define NULL_REGEX (regex_t*)0
1923 /* The prefix in our regexp-s matching the directory. */
1924 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1926 #define NULL_FRULACT (frul_actionrout_t*)0
1928 /* The array of our rules governing file name generation. Rules order
1929 matters, so change with extreme care! */
1931 struct file_rule_st files_rules[] = {
1932 /* The general rule assumes that files in subdirectories belong to a
1933 particular front-end, and files not in subdirectories are shared.
1934 The following rules deal with exceptions - files that are in
1935 subdirectories and yet are shared, and files that are top-level,
1936 but are not shared. */
1938 /* the c-family/ source directory is special. */
1939 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1940 REG_EXTENDED, NULL_REGEX,
1941 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1943 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1944 REG_EXTENDED, NULL_REGEX,
1945 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1947 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1948 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1949 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1951 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1952 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1954 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1955 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1956 REG_EXTENDED, NULL_REGEX,
1957 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1959 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1960 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1961 REG_EXTENDED, NULL_REGEX,
1962 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1964 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1965 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1966 REG_EXTENDED, NULL_REGEX,
1967 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1969 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1970 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1971 REG_EXTENDED, NULL_REGEX,
1972 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1974 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1975 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1976 REG_EXTENDED, NULL_REGEX,
1977 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1979 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1980 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1981 REG_EXTENDED, NULL_REGEX,
1982 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
1984 /* General cases. For header *.h and source *.c files, we need
1985 * special actions to handle the language. */
1987 /* Source *.c files are using get_file_gtfilename to compute their
1988 output_name and get_file_basename to compute their for_name
1989 through the source_dot_c_frul action. */
1990 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
1991 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
1992 /* Common header files get "gtype-desc.c" as their output_name,
1993 * while language specific header files are handled specially. So
1994 * we need the header_dot_h_frul action. */
1995 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
1996 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
1998 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
1999 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2001 /* Mandatory null last entry signaling end of rules. */
2002 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2005 /* Special file rules action for handling *.h header files. It gives
2006 "gtype-desc.c" for common headers and corresponding output
2007 files for language-specific header files. */
2008 static outf_p
2009 header_dot_h_frul (input_file* inpf, char**poutname,
2010 char**pforname ATTRIBUTE_UNUSED)
2012 const char *basename = 0;
2013 int lang_index = 0;
2014 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2015 (void*) inpf, get_input_file_name (inpf),
2016 *poutname, *pforname);
2017 basename = get_file_basename (inpf);
2018 lang_index = get_prefix_langdir_index (basename);
2019 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2021 if (lang_index >= 0)
2023 /* The header is language specific. Given output_name &
2024 for_name remains unchanged. The base_files array gives the
2025 outf_p. */
2026 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2027 (void*) base_files[lang_index],
2028 (base_files[lang_index])->name);
2029 return base_files[lang_index];
2031 else
2033 /* The header is common to all front-end languages. So
2034 output_name is "gtype-desc.c" file. The calling function
2035 get_output_file_with_visibility will find its outf_p. */
2036 free (*poutname);
2037 *poutname = xstrdup ("gtype-desc.c");
2038 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2039 get_input_file_name (inpf));
2040 return NULL;
2045 /* Special file rules action for handling *.c source files using
2046 * get_file_gtfilename to compute their output_name and
2047 * get_file_basename to compute their for_name. The output_name is
2048 * gt-<LANG>-<BASE>.h for language specific source files, and
2049 * gt-<BASE>.h for common source files. */
2050 static outf_p
2051 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2053 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2054 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2055 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2056 (void*) inpf, get_input_file_name (inpf),
2057 *poutname, *pforname);
2058 DBGPRINTF ("newoutname %s", newoutname);
2059 DBGPRINTF ("newbasename %s", newbasename);
2060 free (*poutname);
2061 free (*pforname);
2062 *poutname = newoutname;
2063 *pforname = newbasename;
2064 return NULL;
2067 /* Utility function for get_output_file_with_visibility which returns
2068 * a malloc-ed substituted string using TRS on matching of the FILNAM
2069 * file name, using the PMATCH array. */
2070 static char*
2071 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2072 const char *trs)
2074 struct obstack str_obstack;
2075 char *str = NULL;
2076 char *rawstr = NULL;
2077 const char *pt = NULL;
2078 DBGPRINTF ("filnam %s", filnam);
2079 obstack_init (&str_obstack);
2080 for (pt = trs; *pt; pt++) {
2081 char c = *pt;
2082 if (c == '$')
2084 if (pt[1] == '$')
2086 /* A double dollar $$ is substituted by a single verbatim
2087 dollar, but who really uses dollar signs in file
2088 paths? */
2089 obstack_1grow (&str_obstack, '$');
2091 else if (ISDIGIT (pt[1]))
2093 /* Handle $0 $1 ... $9 by appropriate substitution. */
2094 int dolnum = pt[1] - '0';
2095 int so = pmatch[dolnum].rm_so;
2096 int eo = pmatch[dolnum].rm_eo;
2097 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2098 if (so>=0 && eo>=so)
2099 obstack_grow (&str_obstack, filnam + so, eo - so);
2101 else
2103 /* This can happen only when files_rules is buggy! */
2104 gcc_unreachable();
2106 /* Always skip the character after the dollar. */
2107 pt++;
2109 else
2110 obstack_1grow (&str_obstack, c);
2112 obstack_1grow (&str_obstack, '\0');
2113 rawstr = XOBFINISH (&str_obstack, char *);
2114 str = xstrdup (rawstr);
2115 obstack_free (&str_obstack, NULL);
2116 DBGPRINTF ("matched replacement %s", str);
2117 rawstr = NULL;
2118 return str;
2122 /* An output file, suitable for definitions, that can see declarations
2123 made in INPF and is linked into every language that uses INPF.
2124 Since the result is cached inside INPF, that argument cannot be
2125 declared constant, but is "almost" constant. */
2127 outf_p
2128 get_output_file_with_visibility (input_file *inpf)
2130 outf_p r;
2131 char *for_name = NULL;
2132 char *output_name = NULL;
2133 const char* inpfname;
2135 /* This can happen when we need a file with visibility on a
2136 structure that we've never seen. We have to just hope that it's
2137 globally visible. */
2138 if (inpf == NULL)
2139 inpf = system_h_file;
2141 /* The result is cached in INPF, so return it if already known. */
2142 if (inpf->inpoutf)
2143 return inpf->inpoutf;
2145 /* In plugin mode, return NULL unless the input_file is one of the
2146 plugin_files. */
2147 if (plugin_files)
2149 size_t i;
2150 for (i = 0; i < nb_plugin_files; i++)
2151 if (inpf == plugin_files[i])
2153 inpf->inpoutf = plugin_output;
2154 return plugin_output;
2157 return NULL;
2160 inpfname = get_input_file_name (inpf);
2162 /* Try each rule in sequence in files_rules until one is triggered. */
2164 int rulix = 0;
2165 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2166 (void*) inpf, inpfname);
2168 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2170 DBGPRINTF ("rulix#%d srcexpr %s",
2171 rulix, files_rules[rulix].frul_srcexpr);
2173 if (!files_rules[rulix].frul_re)
2175 /* Compile the regexpr lazily. */
2176 int err = 0;
2177 files_rules[rulix].frul_re = XCNEW (regex_t);
2178 err = regcomp (files_rules[rulix].frul_re,
2179 files_rules[rulix].frul_srcexpr,
2180 files_rules[rulix].frul_rflags);
2181 if (err)
2183 /* The regular expression compilation fails only when
2184 file_rules is buggy. */
2185 gcc_unreachable ();
2189 output_name = NULL;
2190 for_name = NULL;
2192 /* Match the regexpr and trigger the rule if matched. */
2194 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2195 $3, ... $9. */
2196 regmatch_t pmatch[10];
2197 memset (pmatch, 0, sizeof (pmatch));
2198 if (!regexec (files_rules[rulix].frul_re,
2199 inpfname, 10, pmatch, 0))
2201 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2202 (void*) inpf, inpfname, rulix,
2203 files_rules[rulix].frul_srcexpr);
2204 for_name =
2205 matching_file_name_substitute (inpfname, pmatch,
2206 files_rules[rulix].frul_tr_for);
2207 DBGPRINTF ("for_name %s", for_name);
2208 output_name =
2209 matching_file_name_substitute (inpfname, pmatch,
2210 files_rules[rulix].frul_tr_out);
2211 DBGPRINTF ("output_name %s", output_name);
2212 if (files_rules[rulix].frul_action)
2214 /* Invoke our action routine. */
2215 outf_p of = NULL;
2216 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2217 rulix, output_name, for_name);
2218 of =
2219 (files_rules[rulix].frul_action) (inpf,
2220 &output_name, &for_name);
2221 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2222 rulix, (void*)of, output_name, for_name);
2223 /* If the action routine returned something, give it back
2224 immediately and cache it in inpf. */
2225 if (of)
2227 inpf->inpoutf = of;
2228 return of;
2231 /* The rule matched, and had no action, or that action did
2232 not return any output file but could have changed the
2233 output_name or for_name. We break out of the loop on the
2234 files_rules. */
2235 break;
2237 else
2239 /* The regexpr did not match. */
2240 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2241 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2242 continue;
2247 if (!output_name || !for_name)
2249 /* This is impossible, and could only happen if the files_rules is
2250 incomplete or buggy. */
2251 gcc_unreachable ();
2254 /* Look through to see if we've ever seen this output filename
2255 before. If found, cache the result in inpf. */
2256 for (r = output_files; r; r = r->next)
2257 if (filename_cmp (r->name, output_name) == 0)
2259 inpf->inpoutf = r;
2260 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2261 output_name, for_name);
2262 return r;
2265 /* If not found, create it, and cache it in inpf. */
2266 r = create_file (for_name, output_name);
2268 gcc_assert (r && r->name);
2269 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2270 output_name, for_name);
2271 inpf->inpoutf = r;
2272 return r;
2277 /* The name of an output file, suitable for definitions, that can see
2278 declarations made in INPF and is linked into every language that
2279 uses INPF. */
2281 const char *
2282 get_output_file_name (input_file* inpf)
2284 outf_p o = get_output_file_with_visibility (inpf);
2285 if (o)
2286 return o->name;
2287 return NULL;
2290 /* Check if existing file is equal to the in memory buffer. */
2292 static bool
2293 is_file_equal (outf_p of)
2295 FILE *newfile = fopen (of->name, "r");
2296 size_t i;
2297 bool equal;
2298 if (newfile == NULL)
2299 return false;
2301 equal = true;
2302 for (i = 0; i < of->bufused; i++)
2304 int ch;
2305 ch = fgetc (newfile);
2306 if (ch == EOF || ch != (unsigned char) of->buf[i])
2308 equal = false;
2309 break;
2312 fclose (newfile);
2313 return equal;
2316 /* Copy the output to its final destination,
2317 but don't unnecessarily change modification times. */
2319 static void
2320 close_output_files (void)
2322 int nbwrittenfiles = 0;
2323 outf_p of;
2325 for (of = output_files; of; of = of->next)
2327 if (!is_file_equal (of))
2329 FILE *newfile = NULL;
2330 char *backupname = NULL;
2331 /* Back up the old version of the output file gt-FOO.c as
2332 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2333 if (backup_dir)
2335 backupname = concat (backup_dir, "/",
2336 lbasename (of->name), "~", NULL);
2337 if (!access (of->name, F_OK) && rename (of->name, backupname))
2338 fatal ("failed to back up %s as %s: %s",
2339 of->name, backupname, xstrerror (errno));
2342 newfile = fopen (of->name, "w");
2343 if (newfile == NULL)
2344 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2345 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2346 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2347 if (fclose (newfile) != 0)
2348 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2349 nbwrittenfiles++;
2350 if (verbosity_level >= 2 && backupname)
2351 printf ("%s wrote #%-3d %s backed-up in %s\n",
2352 progname, nbwrittenfiles, of->name, backupname);
2353 else if (verbosity_level >= 1)
2354 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2355 free (backupname);
2357 else
2359 /* output file remains unchanged. */
2360 if (verbosity_level >= 2)
2361 printf ("%s keep %s\n", progname, of->name);
2363 free (of->buf);
2364 of->buf = NULL;
2365 of->bufused = of->buflength = 0;
2367 if (verbosity_level >= 1)
2368 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2371 struct flist
2373 struct flist *next;
2374 int started_p;
2375 const input_file* file;
2376 outf_p f;
2379 struct walk_type_data;
2381 /* For scalars and strings, given the item in 'val'.
2382 For structures, given a pointer to the item in 'val'.
2383 For misc. pointers, given the item in 'val'.
2385 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2386 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2388 /* Parameters for write_types. */
2390 struct write_types_data
2392 const char *prefix;
2393 const char *param_prefix;
2394 const char *subfield_marker_routine;
2395 const char *marker_routine;
2396 const char *reorder_note_routine;
2397 const char *comment;
2398 int skip_hooks; /* skip hook generation if non zero */
2401 static void output_escaped_param (struct walk_type_data *d,
2402 const char *, const char *);
2403 static void output_mangled_typename (outf_p, const_type_p);
2404 static void walk_type (type_p t, struct walk_type_data *d);
2405 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2406 const struct write_types_data *wtd);
2407 static void write_types_process_field
2408 (type_p f, const struct walk_type_data *d);
2409 static void write_types (outf_p output_header,
2410 type_p structures,
2411 type_p param_structs,
2412 const struct write_types_data *wtd);
2413 static void write_types_local_process_field
2414 (type_p f, const struct walk_type_data *d);
2415 static void write_local_func_for_structure
2416 (const_type_p orig_s, type_p s, type_p *param);
2417 static void write_local (outf_p output_header,
2418 type_p structures, type_p param_structs);
2419 static void write_enum_defn (type_p structures, type_p param_structs);
2420 static int contains_scalar_p (type_p t);
2421 static void put_mangled_filename (outf_p, const input_file *);
2422 static void finish_root_table (struct flist *flp, const char *pfx,
2423 const char *tname, const char *lastname,
2424 const char *name);
2425 static void write_root (outf_p, pair_p, type_p, const char *, int,
2426 struct fileloc *, const char *, bool);
2427 static void write_array (outf_p f, pair_p v,
2428 const struct write_types_data *wtd);
2429 static void write_roots (pair_p, bool);
2431 /* Parameters for walk_type. */
2433 struct walk_type_data
2435 process_field_fn process_field;
2436 const void *cookie;
2437 outf_p of;
2438 options_p opt;
2439 const char *val;
2440 const char *prev_val[4];
2441 int indent;
2442 int counter;
2443 const struct fileloc *line;
2444 lang_bitmap bitmap;
2445 type_p *param;
2446 int used_length;
2447 type_p orig_s;
2448 const char *reorder_fn;
2449 bool needs_cast_p;
2450 bool fn_wants_lvalue;
2451 bool in_record_p;
2452 int loopcounter;
2453 bool in_ptr_field;
2454 bool have_this_obj;
2458 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2459 pre-processor identifier to use in a #define directive. This replaces
2460 special characters used in C++ identifiers like '>', '<' and ':' with
2461 '_'.
2463 If no C++ special characters are found in TYPE_NAME, return
2464 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2465 characters replaced with '_'. In this case, the caller is
2466 responsible for freeing the allocated string. */
2468 static const char *
2469 filter_type_name (const char *type_name)
2471 if (strchr (type_name, '<') || strchr (type_name, ':'))
2473 size_t i;
2474 char *s = xstrdup (type_name);
2475 for (i = 0; i < strlen (s); i++)
2476 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ',')
2477 s[i] = '_';
2478 return s;
2480 else
2481 return type_name;
2485 /* Print a mangled name representing T to OF. */
2487 static void
2488 output_mangled_typename (outf_p of, const_type_p t)
2490 if (t == NULL)
2491 oprintf (of, "Z");
2492 else
2493 switch (t->kind)
2495 case TYPE_NONE:
2496 case TYPE_UNDEFINED:
2497 gcc_unreachable ();
2498 break;
2499 case TYPE_POINTER:
2500 oprintf (of, "P");
2501 output_mangled_typename (of, t->u.p);
2502 break;
2503 case TYPE_SCALAR:
2504 oprintf (of, "I");
2505 break;
2506 case TYPE_STRING:
2507 oprintf (of, "S");
2508 break;
2509 case TYPE_STRUCT:
2510 case TYPE_UNION:
2511 case TYPE_LANG_STRUCT:
2512 case TYPE_USER_STRUCT:
2514 const char *id_for_tag = filter_type_name (t->u.s.tag);
2515 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2516 id_for_tag);
2517 if (id_for_tag != t->u.s.tag)
2518 free (CONST_CAST(char *, id_for_tag));
2520 break;
2521 case TYPE_PARAM_STRUCT:
2523 int i;
2524 for (i = 0; i < NUM_PARAM; i++)
2525 if (t->u.param_struct.param[i] != NULL)
2526 output_mangled_typename (of, t->u.param_struct.param[i]);
2527 output_mangled_typename (of, t->u.param_struct.stru);
2529 break;
2530 case TYPE_ARRAY:
2531 gcc_unreachable ();
2535 /* Print PARAM to D->OF processing escapes. D->VAL references the
2536 current object, D->PREV_VAL the object containing the current
2537 object, ONAME is the name of the option and D->LINE is used to
2538 print error messages. */
2540 static void
2541 output_escaped_param (struct walk_type_data *d, const char *param,
2542 const char *oname)
2544 const char *p;
2546 for (p = param; *p; p++)
2547 if (*p != '%')
2548 oprintf (d->of, "%c", *p);
2549 else
2550 switch (*++p)
2552 case 'h':
2553 oprintf (d->of, "(%s)", d->prev_val[2]);
2554 break;
2555 case '0':
2556 oprintf (d->of, "(%s)", d->prev_val[0]);
2557 break;
2558 case '1':
2559 oprintf (d->of, "(%s)", d->prev_val[1]);
2560 break;
2561 case 'a':
2563 const char *pp = d->val + strlen (d->val);
2564 while (pp[-1] == ']')
2565 while (*pp != '[')
2566 pp--;
2567 oprintf (d->of, "%s", pp);
2569 break;
2570 default:
2571 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2572 oname, '%', *p);
2577 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2578 which is of type T. Write code to D->OF to constrain execution (at
2579 the point that D->PROCESS_FIELD is called) to the appropriate
2580 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2581 pointers to those objects. D->PREV_VAL lists the objects
2582 containing the current object, D->OPT is a list of options to
2583 apply, D->INDENT is the current indentation level, D->LINE is used
2584 to print error messages, D->BITMAP indicates which languages to
2585 print the structure for, and D->PARAM is the current parameter
2586 (from an enclosing param_is option). */
2588 static void
2589 walk_type (type_p t, struct walk_type_data *d)
2591 const char *length = NULL;
2592 const char *desc = NULL;
2593 int maybe_undef_p = 0;
2594 int use_param_num = -1;
2595 int use_params_p = 0;
2596 int atomic_p = 0;
2597 options_p oo;
2598 const struct nested_ptr_data *nested_ptr_d = NULL;
2600 d->needs_cast_p = false;
2601 for (oo = d->opt; oo; oo = oo->next)
2602 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2603 length = oo->info.string;
2604 else if (strcmp (oo->name, "maybe_undef") == 0)
2605 maybe_undef_p = 1;
2606 else if (strncmp (oo->name, "use_param", 9) == 0
2607 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2608 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2609 else if (strcmp (oo->name, "use_params") == 0)
2610 use_params_p = 1;
2611 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2612 desc = oo->info.string;
2613 else if (strcmp (oo->name, "mark_hook") == 0)
2615 else if (strcmp (oo->name, "nested_ptr") == 0
2616 && oo->kind == OPTION_NESTED)
2617 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2618 else if (strcmp (oo->name, "dot") == 0)
2620 else if (strcmp (oo->name, "tag") == 0)
2622 else if (strcmp (oo->name, "special") == 0)
2624 else if (strcmp (oo->name, "skip") == 0)
2626 else if (strcmp (oo->name, "atomic") == 0)
2627 atomic_p = 1;
2628 else if (strcmp (oo->name, "default") == 0)
2630 else if (strcmp (oo->name, "param_is") == 0)
2632 else if (strncmp (oo->name, "param", 5) == 0
2633 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2635 else if (strcmp (oo->name, "chain_next") == 0)
2637 else if (strcmp (oo->name, "chain_prev") == 0)
2639 else if (strcmp (oo->name, "chain_circular") == 0)
2641 else if (strcmp (oo->name, "reorder") == 0)
2643 else if (strcmp (oo->name, "variable_size") == 0)
2645 else
2646 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2648 if (d->used_length)
2649 length = NULL;
2651 if (use_params_p)
2653 int pointer_p = t->kind == TYPE_POINTER;
2655 if (pointer_p)
2656 t = t->u.p;
2657 if (!union_or_struct_p (t))
2658 error_at_line (d->line, "`use_params' option on unimplemented type");
2659 else
2660 t = find_param_structure (t, d->param);
2661 if (pointer_p)
2662 t = create_pointer (t);
2665 if (use_param_num != -1)
2667 if (d->param != NULL && d->param[use_param_num] != NULL)
2669 type_p nt = d->param[use_param_num];
2671 if (t->kind == TYPE_ARRAY)
2672 nt = create_array (nt, t->u.a.len);
2673 else if (length != NULL && t->kind == TYPE_POINTER)
2674 nt = create_pointer (nt);
2675 d->needs_cast_p = (t->kind != TYPE_POINTER
2676 && (nt->kind == TYPE_POINTER
2677 || nt->kind == TYPE_STRING));
2678 t = nt;
2680 else
2681 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2684 if (maybe_undef_p
2685 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2687 error_at_line (d->line,
2688 "field `%s' has invalid option `maybe_undef_p'\n",
2689 d->val);
2690 return;
2693 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2695 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2696 return;
2699 switch (t->kind)
2701 case TYPE_SCALAR:
2702 case TYPE_STRING:
2703 d->process_field (t, d);
2704 break;
2706 case TYPE_POINTER:
2708 d->in_ptr_field = true;
2709 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2711 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2712 break;
2715 /* If a pointer type is marked as "atomic", we process the
2716 field itself, but we don't walk the data that they point to.
2718 There are two main cases where we walk types: to mark
2719 pointers that are reachable, and to relocate pointers when
2720 writing a PCH file. In both cases, an atomic pointer is
2721 itself marked or relocated, but the memory that it points
2722 to is left untouched. In the case of PCH, that memory will
2723 be read/written unchanged to the PCH file. */
2724 if (atomic_p)
2726 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2727 d->indent += 2;
2728 d->process_field (t, d);
2729 d->indent -= 2;
2730 oprintf (d->of, "%*s}\n", d->indent, "");
2731 break;
2734 if (!length)
2736 if (!union_or_struct_p (t->u.p)
2737 && t->u.p->kind != TYPE_PARAM_STRUCT)
2739 error_at_line (d->line,
2740 "field `%s' is pointer to unimplemented type",
2741 d->val);
2742 break;
2745 if (nested_ptr_d)
2747 const char *oldprevval2 = d->prev_val[2];
2749 if (!union_or_struct_p (nested_ptr_d->type))
2751 error_at_line (d->line,
2752 "field `%s' has invalid "
2753 "option `nested_ptr'\n", d->val);
2754 return;
2757 d->prev_val[2] = d->val;
2758 oprintf (d->of, "%*s{\n", d->indent, "");
2759 d->indent += 2;
2760 d->val = xasprintf ("x%d", d->counter++);
2761 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2762 (nested_ptr_d->type->kind == TYPE_UNION
2763 ? "union" : "struct"),
2764 nested_ptr_d->type->u.s.tag,
2765 d->fn_wants_lvalue ? "" : "const ", d->val);
2766 oprintf (d->of, "%*s", d->indent + 2, "");
2767 output_escaped_param (d, nested_ptr_d->convert_from,
2768 "nested_ptr");
2769 oprintf (d->of, ";\n");
2771 d->process_field (nested_ptr_d->type, d);
2773 if (d->fn_wants_lvalue)
2775 oprintf (d->of, "%*s%s = ", d->indent, "",
2776 d->prev_val[2]);
2777 d->prev_val[2] = d->val;
2778 output_escaped_param (d, nested_ptr_d->convert_to,
2779 "nested_ptr");
2780 oprintf (d->of, ";\n");
2783 d->indent -= 2;
2784 oprintf (d->of, "%*s}\n", d->indent, "");
2785 d->val = d->prev_val[2];
2786 d->prev_val[2] = oldprevval2;
2788 else
2789 d->process_field (t->u.p, d);
2791 else
2793 int loopcounter = d->loopcounter;
2794 const char *oldval = d->val;
2795 const char *oldprevval3 = d->prev_val[3];
2796 char *newval;
2798 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2799 d->indent += 2;
2800 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2801 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2802 "", loopcounter, loopcounter);
2803 if (!d->in_record_p)
2804 output_escaped_param (d, length, "length");
2805 else
2806 oprintf (d->of, "l%d", loopcounter);
2807 if (d->have_this_obj)
2808 /* Try to unswitch loops (see PR53880). */
2809 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2810 oprintf (d->of, "); i%d++) {\n", loopcounter);
2811 d->indent += 2;
2812 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2813 d->used_length = 1;
2814 d->prev_val[3] = oldval;
2815 walk_type (t->u.p, d);
2816 free (newval);
2817 d->val = oldval;
2818 d->prev_val[3] = oldprevval3;
2819 d->used_length = 0;
2820 d->indent -= 2;
2821 oprintf (d->of, "%*s}\n", d->indent, "");
2822 d->process_field (t, d);
2823 d->indent -= 2;
2824 oprintf (d->of, "%*s}\n", d->indent, "");
2826 d->in_ptr_field = false;
2828 break;
2830 case TYPE_ARRAY:
2832 int loopcounter;
2833 const char *oldval = d->val;
2834 char *newval;
2836 /* If it's an array of scalars, we optimize by not generating
2837 any code. */
2838 if (t->u.a.p->kind == TYPE_SCALAR)
2839 break;
2841 if (length)
2842 loopcounter = d->loopcounter;
2843 else
2844 loopcounter = d->counter++;
2846 /* When walking an array, compute the length and store it in a
2847 local variable before walking the array elements, instead of
2848 recomputing the length expression each time through the loop.
2849 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2850 where the length is stored in the first array element,
2851 because otherwise that operand can get overwritten on the
2852 first iteration. */
2853 oprintf (d->of, "%*s{\n", d->indent, "");
2854 d->indent += 2;
2855 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2856 if (!d->in_record_p || !length)
2858 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2859 d->indent, "", loopcounter);
2860 if (length)
2861 output_escaped_param (d, length, "length");
2862 else
2863 oprintf (d->of, "%s", t->u.a.len);
2864 oprintf (d->of, ");\n");
2867 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2868 d->indent, "",
2869 loopcounter, loopcounter, loopcounter, loopcounter);
2870 d->indent += 2;
2871 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2872 d->used_length = 1;
2873 walk_type (t->u.a.p, d);
2874 free (newval);
2875 d->used_length = 0;
2876 d->val = oldval;
2877 d->indent -= 2;
2878 oprintf (d->of, "%*s}\n", d->indent, "");
2879 d->indent -= 2;
2880 oprintf (d->of, "%*s}\n", d->indent, "");
2882 break;
2884 case TYPE_STRUCT:
2885 case TYPE_UNION:
2887 pair_p f;
2888 const char *oldval = d->val;
2889 const char *oldprevval1 = d->prev_val[1];
2890 const char *oldprevval2 = d->prev_val[2];
2891 const char *struct_mark_hook = NULL;
2892 const int union_p = t->kind == TYPE_UNION;
2893 int seen_default_p = 0;
2894 options_p o;
2895 int lengths_seen = 0;
2896 int endcounter;
2897 bool any_length_seen = false;
2899 if (!t->u.s.line.file)
2900 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2902 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2904 error_at_line (d->line,
2905 "structure `%s' defined for mismatching languages",
2906 t->u.s.tag);
2907 error_at_line (&t->u.s.line, "one structure defined here");
2910 /* Some things may also be defined in the structure's options. */
2911 for (o = t->u.s.opt; o; o = o->next)
2912 if (!desc && strcmp (o->name, "desc") == 0
2913 && o->kind == OPTION_STRING)
2914 desc = o->info.string;
2915 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2916 && o->kind == OPTION_STRING)
2917 struct_mark_hook = o->info.string;
2919 if (struct_mark_hook)
2920 oprintf (d->of, "%*s%s (&%s);\n",
2921 d->indent, "", struct_mark_hook, oldval);
2923 d->prev_val[2] = oldval;
2924 d->prev_val[1] = oldprevval2;
2925 if (union_p)
2927 if (desc == NULL)
2929 error_at_line (d->line,
2930 "missing `desc' option for union `%s'",
2931 t->u.s.tag);
2932 desc = "1";
2934 oprintf (d->of, "%*sswitch (", d->indent, "");
2935 output_escaped_param (d, desc, "desc");
2936 oprintf (d->of, ")\n");
2937 d->indent += 2;
2938 oprintf (d->of, "%*s{\n", d->indent, "");
2941 for (f = t->u.s.fields; f; f = f->next)
2943 options_p oo;
2944 int skip_p = 0;
2945 const char *fieldlength = NULL;
2947 d->reorder_fn = NULL;
2948 for (oo = f->opt; oo; oo = oo->next)
2949 if (strcmp (oo->name, "skip") == 0)
2950 skip_p = 1;
2951 else if (strcmp (oo->name, "length") == 0
2952 && oo->kind == OPTION_STRING)
2953 fieldlength = oo->info.string;
2955 if (skip_p)
2956 continue;
2957 if (fieldlength)
2959 lengths_seen++;
2960 d->counter++;
2961 if (!union_p)
2963 if (!any_length_seen)
2965 oprintf (d->of, "%*s{\n", d->indent, "");
2966 d->indent += 2;
2968 any_length_seen = true;
2970 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2971 d->indent, "", d->counter - 1);
2972 output_escaped_param (d, fieldlength, "length");
2973 oprintf (d->of, ");\n");
2977 endcounter = d->counter;
2979 for (f = t->u.s.fields; f; f = f->next)
2981 options_p oo;
2982 const char *dot = ".";
2983 const char *tagid = NULL;
2984 int skip_p = 0;
2985 int default_p = 0;
2986 int use_param_p = 0;
2987 const char *fieldlength = NULL;
2988 char *newval;
2990 d->reorder_fn = NULL;
2991 for (oo = f->opt; oo; oo = oo->next)
2992 if (strcmp (oo->name, "dot") == 0
2993 && oo->kind == OPTION_STRING)
2994 dot = oo->info.string;
2995 else if (strcmp (oo->name, "tag") == 0
2996 && oo->kind == OPTION_STRING)
2997 tagid = oo->info.string;
2998 else if (strcmp (oo->name, "skip") == 0)
2999 skip_p = 1;
3000 else if (strcmp (oo->name, "default") == 0)
3001 default_p = 1;
3002 else if (strcmp (oo->name, "reorder") == 0
3003 && oo->kind == OPTION_STRING)
3004 d->reorder_fn = oo->info.string;
3005 else if (strncmp (oo->name, "use_param", 9) == 0
3006 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3007 use_param_p = 1;
3008 else if (strcmp (oo->name, "length") == 0
3009 && oo->kind == OPTION_STRING)
3010 fieldlength = oo->info.string;
3012 if (skip_p)
3013 continue;
3015 if (union_p && tagid)
3017 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3018 d->indent += 2;
3020 else if (union_p && default_p)
3022 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3023 d->indent += 2;
3024 seen_default_p = 1;
3026 else if (!union_p && (default_p || tagid))
3027 error_at_line (d->line,
3028 "can't use `%s' outside a union on field `%s'",
3029 default_p ? "default" : "tag", f->name);
3030 else if (union_p && !(default_p || tagid)
3031 && f->type->kind == TYPE_SCALAR)
3033 fprintf (stderr,
3034 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3035 get_input_file_name (d->line->file), d->line->line,
3036 f->name);
3037 continue;
3039 else if (union_p && !(default_p || tagid))
3040 error_at_line (d->line,
3041 "field `%s' is missing `tag' or `default' option",
3042 f->name);
3044 if (fieldlength)
3046 d->loopcounter = endcounter - lengths_seen--;
3049 d->line = &f->line;
3050 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3051 d->opt = f->opt;
3052 d->used_length = false;
3053 d->in_record_p = !union_p;
3055 if (union_p && use_param_p && d->param == NULL)
3056 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3057 else
3058 walk_type (f->type, d);
3060 d->in_record_p = false;
3062 free (newval);
3064 if (union_p)
3066 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3067 d->indent -= 2;
3070 d->reorder_fn = NULL;
3072 d->val = oldval;
3073 d->prev_val[1] = oldprevval1;
3074 d->prev_val[2] = oldprevval2;
3076 if (union_p && !seen_default_p)
3078 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3079 oprintf (d->of, "%*s break;\n", d->indent, "");
3081 if (union_p)
3083 oprintf (d->of, "%*s}\n", d->indent, "");
3084 d->indent -= 2;
3086 if (any_length_seen)
3088 d->indent -= 2;
3089 oprintf (d->of, "%*s}\n", d->indent, "");
3092 break;
3094 case TYPE_LANG_STRUCT:
3096 type_p nt;
3097 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3098 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3099 break;
3100 if (nt == NULL)
3101 error_at_line (d->line, "structure `%s' differs between languages",
3102 t->u.s.tag);
3103 else
3104 walk_type (nt, d);
3106 break;
3108 case TYPE_PARAM_STRUCT:
3110 type_p *oldparam = d->param;
3112 d->param = t->u.param_struct.param;
3113 walk_type (t->u.param_struct.stru, d);
3114 d->param = oldparam;
3116 break;
3118 case TYPE_USER_STRUCT:
3119 d->process_field (t, d);
3120 break;
3122 case TYPE_NONE:
3123 case TYPE_UNDEFINED:
3124 gcc_unreachable ();
3128 /* process_field routine for marking routines. */
3130 static void
3131 write_types_process_field (type_p f, const struct walk_type_data *d)
3133 const struct write_types_data *wtd;
3134 const char *cast = d->needs_cast_p ? "(void *)" : "";
3135 wtd = (const struct write_types_data *) d->cookie;
3137 switch (f->kind)
3139 case TYPE_NONE:
3140 case TYPE_UNDEFINED:
3141 gcc_unreachable ();
3142 case TYPE_POINTER:
3143 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3144 wtd->subfield_marker_routine, cast, d->val);
3145 if (wtd->param_prefix)
3147 if (f->u.p->kind == TYPE_SCALAR)
3148 /* The current type is a pointer to a scalar (so not
3149 considered like a pointer to instances of user defined
3150 types) and we are seeing it; it means we must be even
3151 more careful about the second argument of the
3152 SUBFIELD_MARKER_ROUTINE call. That argument must
3153 always be the instance of the type for which
3154 write_func_for_structure was called - this really is
3155 what the function SUBFIELD_MARKER_ROUTINE expects.
3156 That is, it must be an instance of the ORIG_S type
3157 parameter of write_func_for_structure. The convention
3158 is that that argument must be "x" in that case (as set
3159 by write_func_for_structure). The problem is, we can't
3160 count on d->prev_val[3] to be always set to "x" in that
3161 case. Sometimes walk_type can set it to something else
3162 (to e.g cooperate with write_array when called from
3163 write_roots). So let's set it to "x" here then. */
3164 oprintf (d->of, ", x");
3165 else
3166 oprintf (d->of, ", %s", d->prev_val[3]);
3167 if (d->orig_s)
3169 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3170 output_mangled_typename (d->of, d->orig_s);
3172 else
3173 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3175 if (f->u.p->kind == TYPE_PARAM_STRUCT
3176 && f->u.p->u.s.line.file != NULL)
3178 oprintf (d->of, ", gt_e_");
3179 output_mangled_typename (d->of, f);
3181 else if (union_or_struct_p (f) && f->u.p->u.s.line.file != NULL)
3183 oprintf (d->of, ", gt_ggc_e_");
3184 output_mangled_typename (d->of, f);
3186 else
3187 oprintf (d->of, ", gt_types_enum_last");
3189 oprintf (d->of, ");\n");
3190 if (d->reorder_fn && wtd->reorder_note_routine)
3191 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3192 wtd->reorder_note_routine, cast, d->val,
3193 d->prev_val[3], d->reorder_fn);
3194 break;
3196 case TYPE_STRING:
3197 case TYPE_STRUCT:
3198 case TYPE_UNION:
3199 case TYPE_LANG_STRUCT:
3200 case TYPE_PARAM_STRUCT:
3201 case TYPE_USER_STRUCT:
3202 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3204 /* If F is a user-defined type and the field is not a
3205 pointer to the type, then we should not generate the
3206 standard pointer-marking code. All we need to do is call
3207 the user-provided marking function to process the fields
3208 of F. */
3209 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3210 d->val);
3212 else
3214 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3215 output_mangled_typename (d->of, f);
3216 oprintf (d->of, " (%s%s);\n", cast, d->val);
3217 if (d->reorder_fn && wtd->reorder_note_routine)
3218 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3219 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3220 d->reorder_fn);
3222 break;
3224 case TYPE_SCALAR:
3225 break;
3227 case TYPE_ARRAY:
3228 gcc_unreachable ();
3232 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
3234 static void
3235 output_type_enum (outf_p of, type_p s)
3237 if (s->kind == TYPE_PARAM_STRUCT && s->u.param_struct.line.file != NULL)
3239 oprintf (of, ", gt_e_");
3240 output_mangled_typename (of, s);
3242 else if (union_or_struct_p (s) && s->u.s.line.file != NULL)
3244 oprintf (of, ", gt_ggc_e_");
3245 output_mangled_typename (of, s);
3247 else
3248 oprintf (of, ", gt_types_enum_last");
3251 /* Return an output file that is suitable for definitions which can
3252 reference struct S */
3254 static outf_p
3255 get_output_file_for_structure (const_type_p s, type_p *param)
3257 const input_file *fn;
3258 int i;
3260 gcc_assert (union_or_struct_p (s));
3261 fn = s->u.s.line.file;
3263 /* This is a hack, and not the good kind either. */
3264 for (i = NUM_PARAM - 1; i >= 0; i--)
3265 if (param && param[i] && param[i]->kind == TYPE_POINTER
3266 && union_or_struct_p (param[i]->u.p))
3267 fn = param[i]->u.p->u.s.line.file;
3269 /* The call to get_output_file_with_visibility may update fn by
3270 caching its result inside, so we need the CONST_CAST. */
3271 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3275 /* Returns the specifier keyword for a string or union type S, empty string
3276 otherwise. */
3278 static const char *
3279 get_type_specifier (const type_p s)
3281 if (s->kind == TYPE_STRUCT)
3282 return "struct ";
3283 else if (s->kind == TYPE_LANG_STRUCT)
3284 return get_type_specifier (s->u.s.lang_struct);
3285 else if (s->kind == TYPE_UNION)
3286 return "union ";
3287 return "";
3291 /* Emits a declaration for type TY (assumed to be a union or a
3292 structure) on stream OUT. */
3294 static void
3295 write_type_decl (outf_p out, type_p ty)
3297 if (union_or_struct_p (ty))
3298 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3299 else if (ty->kind == TYPE_SCALAR)
3301 if (ty->u.scalar_is_char)
3302 oprintf (out, "const char");
3303 else
3304 oprintf (out, "void");
3306 else if (ty->kind == TYPE_POINTER)
3308 write_type_decl (out, ty->u.p);
3309 oprintf (out, " *");
3311 else if (ty->kind == TYPE_ARRAY)
3313 write_type_decl (out, ty->u.a.p);
3314 oprintf (out, " *");
3316 else if (ty->kind == TYPE_STRING)
3318 oprintf (out, "const char *");
3320 else
3321 gcc_unreachable ();
3325 /* Write on OF the name of the marker function for structure S. PREFIX
3326 is the prefix to use (to distinguish ggc from pch markers). */
3328 static void
3329 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3331 if (union_or_struct_p (s))
3333 const char *id_for_tag = filter_type_name (s->u.s.tag);
3334 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3335 if (id_for_tag != s->u.s.tag)
3336 free (CONST_CAST(char *, id_for_tag));
3338 else if (s->kind == TYPE_PARAM_STRUCT)
3340 oprintf (of, "gt_%s_", prefix);
3341 output_mangled_typename (of, s);
3343 else
3344 gcc_unreachable ();
3347 /* Write on OF a user-callable routine to act as an entry point for
3348 the marking routine for S, generated by write_func_for_structure.
3349 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3351 static void
3352 write_user_func_for_structure_ptr (outf_p of, type_p s, const char *prefix)
3354 /* Parameterized structures are not supported in user markers. There
3355 is no way for the marker function to know which specific type
3356 to use to generate the call to the void * entry point. For
3357 instance, a marker for struct htab may need to call different
3358 routines to mark the fields, depending on the paramN_is attributes.
3360 A user-defined marker that accepts 'struct htab' as its argument
3361 would not know which variant to call. Generating several entry
3362 points accepting 'struct htab' would cause multiply-defined
3363 errors during compilation. */
3364 gcc_assert (union_or_struct_p (s));
3366 type_p alias_of = NULL;
3367 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3368 if (strcmp (opt->name, "ptr_alias") == 0)
3370 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3371 we do not generate marking code for ORIG_S here. Instead, a
3372 forwarder #define in gtype-desc.h will cause every call to its
3373 marker to call the target of this alias.
3375 However, we still want to create a user entry code for the
3376 aliased type. So, if ALIAS_OF is set, we only generate the
3377 user-callable marker function. */
3378 alias_of = opt->info.type;
3379 break;
3382 oprintf (of, "\nvoid\n");
3383 oprintf (of, "gt_%sx (", prefix);
3384 write_type_decl (of, s);
3385 oprintf (of, " *& x)\n");
3386 oprintf (of, "{\n");
3387 oprintf (of, " if (x)\n ");
3388 write_marker_function_name (of, alias_of ? alias_of : s, prefix);
3389 oprintf (of, " ((void *) x);\n");
3390 oprintf (of, "}\n");
3394 /* Write a function to mark all the fields of type S on OF. PREFIX
3395 and D are as in write_user_marking_functions. */
3397 static void
3398 write_user_func_for_structure_body (type_p s, const char *prefix,
3399 struct walk_type_data *d)
3401 oprintf (d->of, "\nvoid\n");
3402 oprintf (d->of, "gt_%sx (", prefix);
3403 write_type_decl (d->of, s);
3404 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3405 oprintf (d->of, "{\n");
3406 oprintf (d->of, " ");
3407 write_type_decl (d->of, s);
3408 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3409 d->val = "(*x)";
3410 d->indent = 2;
3411 walk_type (s, d);
3412 oprintf (d->of, "}\n");
3416 /* Emit the user-callable functions needed to mark all the types used
3417 by the user structure S. PREFIX is the prefix to use to
3418 distinguish ggc and pch markers. D contains data needed to pass to
3419 walk_type when traversing the fields of a type.
3421 For every type T referenced by S, two routines are generated: one
3422 that takes 'T *', marks the pointer and calls the second routine,
3423 which just marks the fields of T. */
3425 static void
3426 write_user_marking_functions (type_p s, const char *prefix,
3427 struct walk_type_data *d)
3429 gcc_assert (s->kind == TYPE_USER_STRUCT);
3431 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3433 type_p fld_type = fld->type;
3434 if (fld_type->kind == TYPE_POINTER)
3436 type_p pointed_to_type = fld_type->u.p;
3437 if (union_or_struct_p (pointed_to_type))
3438 write_user_func_for_structure_ptr (d->of, pointed_to_type, prefix);
3440 else if (union_or_struct_p (fld_type))
3441 write_user_func_for_structure_body (fld_type, prefix, d);
3446 /* For S, a structure that's part of ORIG_S, and using parameters
3447 PARAM, write out a routine that:
3448 - Takes a parameter, a void * but actually of type *S
3449 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3450 field of S or its substructures and (in some cases) things
3451 that are pointed to by S. */
3453 static void
3454 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3455 const struct write_types_data *wtd)
3457 const char *chain_next = NULL;
3458 const char *chain_prev = NULL;
3459 const char *chain_circular = NULL;
3460 const char *mark_hook_name = NULL;
3461 options_p opt;
3462 struct walk_type_data d;
3464 memset (&d, 0, sizeof (d));
3465 d.of = get_output_file_for_structure (s, param);
3466 for (opt = s->u.s.opt; opt; opt = opt->next)
3467 if (strcmp (opt->name, "chain_next") == 0
3468 && opt->kind == OPTION_STRING)
3469 chain_next = opt->info.string;
3470 else if (strcmp (opt->name, "chain_prev") == 0
3471 && opt->kind == OPTION_STRING)
3472 chain_prev = opt->info.string;
3473 else if (strcmp (opt->name, "chain_circular") == 0
3474 && opt->kind == OPTION_STRING)
3475 chain_circular = opt->info.string;
3476 else if (strcmp (opt->name, "mark_hook") == 0
3477 && opt->kind == OPTION_STRING)
3478 mark_hook_name = opt->info.string;
3479 if (chain_prev != NULL && chain_next == NULL)
3480 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3481 if (chain_circular != NULL && chain_next != NULL)
3482 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3483 if (chain_circular != NULL)
3484 chain_next = chain_circular;
3486 d.process_field = write_types_process_field;
3487 d.cookie = wtd;
3488 d.orig_s = orig_s;
3489 d.opt = s->u.s.opt;
3490 d.line = &s->u.s.line;
3491 d.bitmap = s->u.s.bitmap;
3492 d.param = param;
3493 d.prev_val[0] = "*x";
3494 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3495 d.prev_val[3] = "x";
3496 d.val = "(*x)";
3497 d.have_this_obj = false;
3499 oprintf (d.of, "\n");
3500 oprintf (d.of, "void\n");
3501 write_marker_function_name (d.of, orig_s, wtd->prefix);
3502 oprintf (d.of, " (void *x_p)\n");
3503 oprintf (d.of, "{\n ");
3504 write_type_decl (d.of, s);
3505 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3506 write_type_decl (d.of, s);
3507 oprintf (d.of, " *)x_p;\n");
3508 if (chain_next != NULL)
3510 /* TYPE_USER_STRUCTs should not occur here. These structures
3511 are completely handled by user code. */
3512 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3514 oprintf (d.of, " ");
3515 write_type_decl (d.of, s);
3516 oprintf (d.of, " * xlimit = x;\n");
3518 if (chain_next == NULL)
3520 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3521 if (wtd->param_prefix)
3523 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3524 output_mangled_typename (d.of, orig_s);
3525 output_type_enum (d.of, orig_s);
3527 oprintf (d.of, "))\n");
3529 else
3531 if (chain_circular != NULL)
3532 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3533 else
3534 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3535 if (wtd->param_prefix)
3537 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3538 output_mangled_typename (d.of, orig_s);
3539 output_type_enum (d.of, orig_s);
3541 oprintf (d.of, "))\n");
3542 if (chain_circular != NULL)
3543 oprintf (d.of, " return;\n do\n");
3544 if (mark_hook_name && !wtd->skip_hooks)
3546 oprintf (d.of, " {\n");
3547 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3549 oprintf (d.of, " xlimit = (");
3550 d.prev_val[2] = "*xlimit";
3551 output_escaped_param (&d, chain_next, "chain_next");
3552 oprintf (d.of, ");\n");
3553 if (mark_hook_name && !wtd->skip_hooks)
3554 oprintf (d.of, " }\n");
3555 if (chain_prev != NULL)
3557 oprintf (d.of, " if (x != xlimit)\n");
3558 oprintf (d.of, " for (;;)\n");
3559 oprintf (d.of, " {\n");
3560 oprintf (d.of, " %s %s * const xprev = (",
3561 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3563 d.prev_val[2] = "*x";
3564 output_escaped_param (&d, chain_prev, "chain_prev");
3565 oprintf (d.of, ");\n");
3566 oprintf (d.of, " if (xprev == NULL) break;\n");
3567 oprintf (d.of, " x = xprev;\n");
3568 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3569 if (wtd->param_prefix)
3571 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3572 output_mangled_typename (d.of, orig_s);
3573 output_type_enum (d.of, orig_s);
3575 oprintf (d.of, ");\n");
3576 oprintf (d.of, " }\n");
3578 if (chain_circular != NULL)
3580 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3581 if (wtd->param_prefix)
3583 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3584 output_mangled_typename (d.of, orig_s);
3585 output_type_enum (d.of, orig_s);
3587 oprintf (d.of, "));\n");
3588 if (mark_hook_name && !wtd->skip_hooks)
3589 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3590 oprintf (d.of, " do\n");
3592 else
3593 oprintf (d.of, " while (x != xlimit)\n");
3595 oprintf (d.of, " {\n");
3596 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3598 oprintf (d.of, " %s (x);\n", mark_hook_name);
3601 d.prev_val[2] = "*x";
3602 d.indent = 6;
3603 if (orig_s->kind != TYPE_USER_STRUCT)
3604 walk_type (s, &d);
3605 else
3607 /* User structures have no fields to walk. Simply generate a call
3608 to the user-provided structure marker. */
3609 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3612 if (chain_next != NULL)
3614 oprintf (d.of, " x = (");
3615 output_escaped_param (&d, chain_next, "chain_next");
3616 oprintf (d.of, ");\n");
3619 oprintf (d.of, " }\n");
3620 if (chain_circular != NULL)
3621 oprintf (d.of, " while (x != xlimit);\n");
3622 oprintf (d.of, "}\n");
3624 if (orig_s->kind == TYPE_USER_STRUCT)
3625 write_user_marking_functions (orig_s, wtd->prefix, &d);
3629 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3631 static void
3632 write_types (outf_p output_header, type_p structures, type_p param_structs,
3633 const struct write_types_data *wtd)
3635 int nbfun = 0; /* Count the emitted functions. */
3636 type_p s;
3638 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3640 /* We first emit the macros and the declarations. Functions' code is
3641 emitted afterwards. This is needed in plugin mode. */
3642 oprintf (output_header, "/* Macros and declarations. */\n");
3643 for (s = structures; s; s = s->next)
3644 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3646 options_p opt;
3648 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3649 continue;
3651 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3653 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3654 output_mangled_typename (output_header, s);
3655 oprintf (output_header, "(X) do { \\\n");
3656 oprintf (output_header,
3657 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3658 s_id_for_tag);
3659 oprintf (output_header, " } while (0)\n");
3661 for (opt = s->u.s.opt; opt; opt = opt->next)
3662 if (strcmp (opt->name, "ptr_alias") == 0
3663 && opt->kind == OPTION_TYPE)
3665 const_type_p const t = (const_type_p) opt->info.type;
3666 if (t->kind == TYPE_STRUCT
3667 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3669 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3670 oprintf (output_header,
3671 "#define gt_%sx_%s gt_%sx_%s\n",
3672 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3673 if (t_id_for_tag != t->u.s.tag)
3674 free (CONST_CAST(char *, t_id_for_tag));
3676 else
3677 error_at_line (&s->u.s.line,
3678 "structure alias is not a structure");
3679 break;
3681 if (opt)
3682 continue;
3684 /* Declare the marker procedure only once. */
3685 oprintf (output_header,
3686 "extern void gt_%sx_%s (void *);\n",
3687 wtd->prefix, s_id_for_tag);
3689 if (s_id_for_tag != s->u.s.tag)
3690 free (CONST_CAST(char *, s_id_for_tag));
3692 if (s->u.s.line.file == NULL)
3694 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3695 s->u.s.tag);
3696 continue;
3700 for (s = param_structs; s; s = s->next)
3701 if (s->gc_used == GC_POINTED_TO)
3703 type_p stru = s->u.param_struct.stru;
3705 /* Declare the marker procedure. */
3706 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3707 output_mangled_typename (output_header, s);
3708 oprintf (output_header, " (void *);\n");
3710 if (stru->u.s.line.file == NULL)
3712 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3713 stru->u.s.tag);
3714 continue;
3718 /* At last we emit the functions code. */
3719 oprintf (output_header, "\n/* functions code */\n");
3720 for (s = structures; s; s = s->next)
3721 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3723 options_p opt;
3725 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3726 continue;
3727 for (opt = s->u.s.opt; opt; opt = opt->next)
3728 if (strcmp (opt->name, "ptr_alias") == 0)
3729 break;
3730 if (opt)
3731 continue;
3733 if (s->kind == TYPE_LANG_STRUCT)
3735 type_p ss;
3736 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3738 nbfun++;
3739 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3740 nbfun, (void*) ss, ss->u.s.tag);
3741 write_func_for_structure (s, ss, NULL, wtd);
3744 else
3746 nbfun++;
3747 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3748 nbfun, (void*) s, s->u.s.tag);
3749 write_func_for_structure (s, s, NULL, wtd);
3752 else
3754 /* Structure s is not possibly pointed to, so can be ignored. */
3755 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3756 (void*)s, s->u.s.tag,
3757 (int) s->gc_used);
3760 for (s = param_structs; s; s = s->next)
3761 if (s->gc_used == GC_POINTED_TO)
3763 type_p *param = s->u.param_struct.param;
3764 type_p stru = s->u.param_struct.stru;
3765 if (stru->u.s.line.file == NULL)
3766 continue;
3767 if (stru->kind == TYPE_LANG_STRUCT)
3769 type_p ss;
3770 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3772 nbfun++;
3773 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3774 nbfun, (void*) ss, ss->u.s.tag);
3775 write_func_for_structure (s, ss, param, wtd);
3778 else
3780 nbfun++;
3781 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3782 nbfun, (void*) s,
3783 (void*) stru, stru->u.s.tag);
3784 write_func_for_structure (s, stru, param, wtd);
3787 else
3789 /* Param structure s is not pointed to, so should be ignored. */
3790 DBGPRINTF ("ignored s @ %p", (void*)s);
3792 if (verbosity_level >= 2)
3793 printf ("%s emitted %d routines for %s\n",
3794 progname, nbfun, wtd->comment);
3797 static const struct write_types_data ggc_wtd = {
3798 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3799 "GC marker procedures. ",
3800 FALSE
3803 static const struct write_types_data pch_wtd = {
3804 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3805 "gt_pch_note_reorder",
3806 "PCH type-walking procedures. ",
3807 TRUE
3810 /* Write out the local pointer-walking routines. */
3812 /* process_field routine for local pointer-walking for user-callable
3813 routines. The difference between this and
3814 write_types_local_process_field is that, in this case, we do not
3815 need to check whether the given pointer matches the address of the
3816 parent structure. This check was already generated by the call
3817 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3818 this code. */
3820 static void
3821 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3823 switch (f->kind)
3825 case TYPE_POINTER:
3826 case TYPE_STRUCT:
3827 case TYPE_UNION:
3828 case TYPE_LANG_STRUCT:
3829 case TYPE_PARAM_STRUCT:
3830 case TYPE_STRING:
3831 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3832 break;
3834 case TYPE_USER_STRUCT:
3835 if (d->in_ptr_field)
3836 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3837 else
3838 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3839 d->indent, "", d->val);
3840 break;
3842 case TYPE_SCALAR:
3843 break;
3845 case TYPE_ARRAY:
3846 case TYPE_NONE:
3847 case TYPE_UNDEFINED:
3848 gcc_unreachable ();
3853 /* Write a function to PCH walk all the fields of type S on OF.
3854 D contains data needed by walk_type to recurse into the fields of S. */
3856 static void
3857 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3859 oprintf (d->of, "\nvoid\n");
3860 oprintf (d->of, "gt_pch_nx (");
3861 write_type_decl (d->of, s);
3862 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3863 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3864 "\tATTRIBUTE_UNUSED void *cookie)\n");
3865 oprintf (d->of, "{\n");
3866 d->val = "(*x)";
3867 d->indent = 2;
3868 d->process_field = write_types_local_user_process_field;
3869 walk_type (s, d);
3870 oprintf (d->of, "}\n");
3874 /* Emit the user-callable functions needed to mark all the types used
3875 by the user structure S. PREFIX is the prefix to use to
3876 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3877 chain_next option defined. D contains data needed to pass to
3878 walk_type when traversing the fields of a type.
3880 For every type T referenced by S, two routines are generated: one
3881 that takes 'T *', marks the pointer and calls the second routine,
3882 which just marks the fields of T. */
3884 static void
3885 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3887 gcc_assert (s->kind == TYPE_USER_STRUCT);
3889 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3891 type_p fld_type = fld->type;
3892 if (union_or_struct_p (fld_type))
3893 write_pch_user_walking_for_structure_body (fld_type, d);
3898 /* process_field routine for local pointer-walking. */
3900 static void
3901 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3903 gcc_assert (d->have_this_obj);
3904 switch (f->kind)
3906 case TYPE_POINTER:
3907 case TYPE_STRUCT:
3908 case TYPE_UNION:
3909 case TYPE_LANG_STRUCT:
3910 case TYPE_PARAM_STRUCT:
3911 case TYPE_STRING:
3912 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3913 d->prev_val[3]);
3914 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3915 break;
3917 case TYPE_USER_STRUCT:
3918 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3919 d->prev_val[3]);
3920 if (d->in_ptr_field)
3921 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3922 else
3923 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3924 d->indent, "", d->val);
3925 break;
3927 case TYPE_SCALAR:
3928 break;
3930 case TYPE_ARRAY:
3931 case TYPE_NONE:
3932 case TYPE_UNDEFINED:
3933 gcc_unreachable ();
3938 /* For S, a structure that's part of ORIG_S, and using parameters
3939 PARAM, write out a routine that:
3940 - Is of type gt_note_pointers
3941 - Calls PROCESS_FIELD on each field of S or its substructures.
3944 static void
3945 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3947 struct walk_type_data d;
3949 memset (&d, 0, sizeof (d));
3950 d.of = get_output_file_for_structure (s, param);
3951 d.process_field = write_types_local_process_field;
3952 d.opt = s->u.s.opt;
3953 d.line = &s->u.s.line;
3954 d.bitmap = s->u.s.bitmap;
3955 d.param = param;
3956 d.prev_val[0] = d.prev_val[2] = "*x";
3957 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3958 d.prev_val[3] = "x";
3959 d.val = "(*x)";
3960 d.fn_wants_lvalue = true;
3962 oprintf (d.of, "\n");
3963 oprintf (d.of, "void\n");
3964 oprintf (d.of, "gt_pch_p_");
3965 output_mangled_typename (d.of, orig_s);
3966 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3967 "\tvoid *x_p,\n"
3968 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3969 "\tATTRIBUTE_UNUSED void *cookie)\n");
3970 oprintf (d.of, "{\n");
3971 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3972 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3973 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3974 d.indent = 2;
3975 d.have_this_obj = true;
3977 if (s->kind != TYPE_USER_STRUCT)
3978 walk_type (s, &d);
3979 else
3981 /* User structures have no fields to walk. Simply generate a
3982 call to the user-provided PCH walker. */
3983 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3984 d.prev_val[3]);
3985 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3986 d.indent, "", d.val);
3989 oprintf (d.of, "}\n");
3991 /* Write user-callable entry points for the PCH walking routines. */
3992 if (orig_s->kind == TYPE_USER_STRUCT)
3993 write_pch_user_walking_functions (s, &d);
3996 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3998 static void
3999 write_local (outf_p output_header, type_p structures, type_p param_structs)
4001 type_p s;
4003 if (!output_header)
4004 return;
4006 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4007 for (s = structures; s; s = s->next)
4008 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4010 options_p opt;
4012 if (s->u.s.line.file == NULL)
4013 continue;
4014 for (opt = s->u.s.opt; opt; opt = opt->next)
4015 if (strcmp (opt->name, "ptr_alias") == 0
4016 && opt->kind == OPTION_TYPE)
4018 const_type_p const t = (const_type_p) opt->info.type;
4019 if (t->kind == TYPE_STRUCT
4020 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4022 oprintf (output_header, "#define gt_pch_p_");
4023 output_mangled_typename (output_header, s);
4024 oprintf (output_header, " gt_pch_p_");
4025 output_mangled_typename (output_header, t);
4026 oprintf (output_header, "\n");
4028 else
4029 error_at_line (&s->u.s.line,
4030 "structure alias is not a structure");
4031 break;
4033 if (opt)
4034 continue;
4036 /* Declare the marker procedure only once. */
4037 oprintf (output_header, "extern void gt_pch_p_");
4038 output_mangled_typename (output_header, s);
4039 oprintf (output_header,
4040 "\n (void *, void *, gt_pointer_operator, void *);\n");
4042 if (s->kind == TYPE_LANG_STRUCT)
4044 type_p ss;
4045 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4046 write_local_func_for_structure (s, ss, NULL);
4048 else
4049 write_local_func_for_structure (s, s, NULL);
4052 for (s = param_structs; s; s = s->next)
4053 if (s->gc_used == GC_POINTED_TO)
4055 type_p *param = s->u.param_struct.param;
4056 type_p stru = s->u.param_struct.stru;
4058 /* Declare the marker procedure. */
4059 oprintf (output_header, "extern void gt_pch_p_");
4060 output_mangled_typename (output_header, s);
4061 oprintf (output_header,
4062 "\n (void *, void *, gt_pointer_operator, void *);\n");
4064 if (stru->u.s.line.file == NULL)
4066 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4067 stru->u.s.tag);
4068 continue;
4071 if (stru->kind == TYPE_LANG_STRUCT)
4073 type_p ss;
4074 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4075 write_local_func_for_structure (s, ss, param);
4077 else
4078 write_local_func_for_structure (s, stru, param);
4082 /* Nonzero if S is a type for which typed GC allocators should be output. */
4084 #define USED_BY_TYPED_GC_P(s) \
4085 ((s->kind == TYPE_POINTER \
4086 && (s->u.p->gc_used == GC_POINTED_TO \
4087 || s->u.p->gc_used == GC_USED)) \
4088 || (union_or_struct_p (s) \
4089 && ((s)->gc_used == GC_POINTED_TO \
4090 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4091 && s->u.s.line.file != NULL) \
4092 || ((s)->gc_used == GC_USED \
4093 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
4096 /* Write out the 'enum' definition for gt_types_enum. */
4098 static void
4099 write_enum_defn (type_p structures, type_p param_structs)
4101 type_p s;
4102 int nbstruct = 0;
4103 int nbparamstruct = 0;
4105 if (!header_file)
4106 return;
4107 oprintf (header_file, "\n/* Enumeration of types known. */\n");
4108 oprintf (header_file, "enum gt_types_enum {\n");
4109 for (s = structures; s; s = s->next)
4110 if (USED_BY_TYPED_GC_P (s))
4112 nbstruct++;
4113 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
4114 (void*) s, nbstruct);
4115 if (union_or_struct_p (s))
4116 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
4117 (void*) s, nbstruct, s->u.s.tag);
4118 oprintf (header_file, " gt_ggc_e_");
4119 output_mangled_typename (header_file, s);
4120 oprintf (header_file, ",\n");
4122 for (s = param_structs; s; s = s->next)
4123 if (s->gc_used == GC_POINTED_TO)
4125 nbparamstruct++;
4126 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
4127 (void*) s, nbparamstruct);
4128 oprintf (header_file, " gt_e_");
4129 output_mangled_typename (header_file, s);
4130 oprintf (header_file, ",\n");
4132 oprintf (header_file, " gt_types_enum_last\n");
4133 oprintf (header_file, "};\n");
4134 if (verbosity_level >= 2)
4135 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
4136 progname, nbstruct, nbparamstruct);
4140 /* Might T contain any non-pointer elements? */
4142 static int
4143 contains_scalar_p (type_p t)
4145 switch (t->kind)
4147 case TYPE_STRING:
4148 case TYPE_POINTER:
4149 return 0;
4150 case TYPE_ARRAY:
4151 return contains_scalar_p (t->u.a.p);
4152 case TYPE_USER_STRUCT:
4153 /* User-marked structures will typically contain pointers. */
4154 return 0;
4155 default:
4156 /* Could also check for structures that have no non-pointer
4157 fields, but there aren't enough of those to worry about. */
4158 return 1;
4162 /* Mangle INPF and print it to F. */
4164 static void
4165 put_mangled_filename (outf_p f, const input_file *inpf)
4167 /* The call to get_output_file_name may indirectly update fn since
4168 get_output_file_with_visibility caches its result inside, so we
4169 need the CONST_CAST. */
4170 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4171 if (!f || !name)
4172 return;
4173 for (; *name != 0; name++)
4174 if (ISALNUM (*name))
4175 oprintf (f, "%c", *name);
4176 else
4177 oprintf (f, "%c", '_');
4180 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4181 LASTNAME, and NAME are all strings to insert in various places in
4182 the resulting code. */
4184 static void
4185 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4186 const char *tname, const char *name)
4188 struct flist *fli2;
4190 for (fli2 = flp; fli2; fli2 = fli2->next)
4191 if (fli2->started_p)
4193 oprintf (fli2->f, " %s\n", lastname);
4194 oprintf (fli2->f, "};\n\n");
4197 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4198 if (fli2->started_p)
4200 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4201 int fnum;
4203 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4204 if (bitmap & 1)
4206 oprintf (base_files[fnum],
4207 "extern const struct %s gt_%s_", tname, pfx);
4208 put_mangled_filename (base_files[fnum], fli2->file);
4209 oprintf (base_files[fnum], "[];\n");
4214 size_t fnum;
4215 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4216 oprintf (base_files[fnum],
4217 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4221 for (fli2 = flp; fli2; fli2 = fli2->next)
4222 if (fli2->started_p)
4224 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4225 int fnum;
4227 fli2->started_p = 0;
4229 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4230 if (bitmap & 1)
4232 oprintf (base_files[fnum], " gt_%s_", pfx);
4233 put_mangled_filename (base_files[fnum], fli2->file);
4234 oprintf (base_files[fnum], ",\n");
4239 size_t fnum;
4240 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4242 oprintf (base_files[fnum], " NULL\n");
4243 oprintf (base_files[fnum], "};\n");
4248 /* Write the first three fields (pointer, count and stride) for
4249 root NAME to F. V and LINE are as for write_root.
4251 Return true if the entry could be written; return false on error. */
4253 static bool
4254 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4256 type_p ap;
4258 if (!v)
4260 error_at_line (line, "`%s' is too complex to be a root", name);
4261 return false;
4264 oprintf (f, " {\n");
4265 oprintf (f, " &%s,\n", name);
4266 oprintf (f, " 1");
4268 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4269 if (ap->u.a.len[0])
4270 oprintf (f, " * (%s)", ap->u.a.len);
4271 else if (ap == v->type)
4272 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4273 oprintf (f, ",\n");
4274 oprintf (f, " sizeof (%s", v->name);
4275 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4276 oprintf (f, "[0]");
4277 oprintf (f, "),\n");
4278 return true;
4281 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4282 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4283 of the caller. */
4285 static void
4286 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4287 int has_length, struct fileloc *line, const char *if_marked,
4288 bool emit_pch, type_p field_type, const char *field_name)
4290 struct pair newv;
4291 /* If the field reference is relative to V, rather than to some
4292 subcomponent of V, we can mark any subarrays with a single stride.
4293 We're effectively treating the field as a global variable in its
4294 own right. */
4295 if (v && type == v->type)
4297 newv = *v;
4298 newv.type = field_type;
4299 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4300 v = &newv;
4302 /* Otherwise, any arrays nested in the structure are too complex to
4303 handle. */
4304 else if (field_type->kind == TYPE_ARRAY)
4305 v = NULL;
4306 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4307 has_length, line, if_marked, emit_pch);
4310 /* Write out to F the table entry and any marker routines needed to
4311 mark NAME as TYPE. V can be one of three values:
4313 - null, if NAME is too complex to represent using a single
4314 count and stride. In this case, it is an error for NAME to
4315 contain any gc-ed data.
4317 - the outermost array that contains NAME, if NAME is part of an array.
4319 - the C variable that contains NAME, if NAME is not part of an array.
4321 LINE is the line of the C source that declares the root variable.
4322 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4323 is nonzero iff we are building the root table for hash table caches. */
4325 static void
4326 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4327 struct fileloc *line, const char *if_marked, bool emit_pch)
4329 switch (type->kind)
4331 case TYPE_STRUCT:
4333 pair_p fld;
4334 for (fld = type->u.s.fields; fld; fld = fld->next)
4336 int skip_p = 0;
4337 const char *desc = NULL;
4338 options_p o;
4340 for (o = fld->opt; o; o = o->next)
4341 if (strcmp (o->name, "skip") == 0)
4342 skip_p = 1;
4343 else if (strcmp (o->name, "desc") == 0
4344 && o->kind == OPTION_STRING)
4345 desc = o->info.string;
4346 else if (strcmp (o->name, "param_is") == 0)
4348 else
4349 error_at_line (line,
4350 "field `%s' of global `%s' has unknown option `%s'",
4351 fld->name, name, o->name);
4353 if (skip_p)
4354 continue;
4355 else if (desc && fld->type->kind == TYPE_UNION)
4357 pair_p validf = NULL;
4358 pair_p ufld;
4360 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4362 const char *tag = NULL;
4363 options_p oo;
4364 for (oo = ufld->opt; oo; oo = oo->next)
4365 if (strcmp (oo->name, "tag") == 0
4366 && oo->kind == OPTION_STRING)
4367 tag = oo->info.string;
4368 if (tag == NULL || strcmp (tag, desc) != 0)
4369 continue;
4370 if (validf != NULL)
4371 error_at_line (line,
4372 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4373 name, fld->name, validf->name,
4374 name, fld->name, ufld->name, tag);
4375 validf = ufld;
4377 if (validf != NULL)
4378 write_field_root (f, v, type, name, 0, line, if_marked,
4379 emit_pch, validf->type,
4380 ACONCAT ((fld->name, ".",
4381 validf->name, NULL)));
4383 else if (desc)
4384 error_at_line (line,
4385 "global `%s.%s' has `desc' option but is not union",
4386 name, fld->name);
4387 else
4388 write_field_root (f, v, type, name, 0, line, if_marked,
4389 emit_pch, fld->type, fld->name);
4392 break;
4394 case TYPE_ARRAY:
4396 char *newname;
4397 newname = xasprintf ("%s[0]", name);
4398 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4399 emit_pch);
4400 free (newname);
4402 break;
4404 case TYPE_USER_STRUCT:
4405 error_at_line (line, "`%s' must be a pointer type, because it is "
4406 "a GC root and its type is marked with GTY((user))",
4407 v->name);
4408 break;
4410 case TYPE_POINTER:
4412 type_p tp;
4414 if (!start_root_entry (f, v, name, line))
4415 return;
4417 tp = type->u.p;
4419 if (!has_length && union_or_struct_p (tp))
4421 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4422 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4423 if (emit_pch)
4424 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4425 else
4426 oprintf (f, " NULL");
4427 if (id_for_tag != tp->u.s.tag)
4428 free (CONST_CAST(char *, id_for_tag));
4430 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4432 oprintf (f, " &gt_ggc_m_");
4433 output_mangled_typename (f, tp);
4434 if (emit_pch)
4436 oprintf (f, ",\n &gt_pch_n_");
4437 output_mangled_typename (f, tp);
4439 else
4440 oprintf (f, ",\n NULL");
4442 else if (has_length
4443 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4445 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4446 if (emit_pch)
4447 oprintf (f, " &gt_pch_na_%s", name);
4448 else
4449 oprintf (f, " NULL");
4451 else
4453 error_at_line (line,
4454 "global `%s' is pointer to unimplemented type",
4455 name);
4457 if (if_marked)
4458 oprintf (f, ",\n &%s", if_marked);
4459 oprintf (f, "\n },\n");
4461 break;
4463 case TYPE_STRING:
4465 if (!start_root_entry (f, v, name, line))
4466 return;
4468 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4469 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4470 oprintf (f, " },\n");
4472 break;
4474 case TYPE_SCALAR:
4475 break;
4477 case TYPE_NONE:
4478 case TYPE_UNDEFINED:
4479 case TYPE_UNION:
4480 case TYPE_LANG_STRUCT:
4481 case TYPE_PARAM_STRUCT:
4482 error_at_line (line, "global `%s' is unimplemented type", name);
4486 /* This generates a routine to walk an array. */
4488 static void
4489 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4491 struct walk_type_data d;
4492 char *prevval3;
4494 memset (&d, 0, sizeof (d));
4495 d.of = f;
4496 d.cookie = wtd;
4497 d.indent = 2;
4498 d.line = &v->line;
4499 d.opt = v->opt;
4500 d.bitmap = get_lang_bitmap (v->line.file);
4501 d.param = NULL;
4503 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4505 if (wtd->param_prefix)
4507 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4508 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4509 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4510 wtd->param_prefix, v->name);
4511 oprintf (d.of,
4512 " ATTRIBUTE_UNUSED void *x_p,\n"
4513 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4514 " ATTRIBUTE_UNUSED void * cookie)\n");
4515 oprintf (d.of, "{\n");
4516 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4517 d.process_field = write_types_local_process_field;
4518 d.have_this_obj = true;
4519 walk_type (v->type, &d);
4520 oprintf (f, "}\n\n");
4523 d.opt = v->opt;
4524 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4525 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4526 wtd->prefix, v->name);
4527 oprintf (f, "{\n");
4528 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4529 d.process_field = write_types_process_field;
4530 d.have_this_obj = false;
4531 walk_type (v->type, &d);
4532 free (prevval3);
4533 oprintf (f, "}\n\n");
4536 /* Output a table describing the locations and types of VARIABLES. */
4538 static void
4539 write_roots (pair_p variables, bool emit_pch)
4541 pair_p v;
4542 struct flist *flp = NULL;
4544 for (v = variables; v; v = v->next)
4546 outf_p f =
4547 get_output_file_with_visibility (CONST_CAST (input_file*,
4548 v->line.file));
4549 struct flist *fli;
4550 const char *length = NULL;
4551 int deletable_p = 0;
4552 options_p o;
4553 for (o = v->opt; o; o = o->next)
4554 if (strcmp (o->name, "length") == 0
4555 && o->kind == OPTION_STRING)
4556 length = o->info.string;
4557 else if (strcmp (o->name, "deletable") == 0)
4558 deletable_p = 1;
4559 else if (strcmp (o->name, "param_is") == 0)
4561 else if (strncmp (o->name, "param", 5) == 0
4562 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4564 else if (strcmp (o->name, "if_marked") == 0)
4566 else
4567 error_at_line (&v->line,
4568 "global `%s' has unknown option `%s'",
4569 v->name, o->name);
4571 for (fli = flp; fli; fli = fli->next)
4572 if (fli->f == f && f)
4573 break;
4574 if (fli == NULL)
4576 fli = XNEW (struct flist);
4577 fli->f = f;
4578 fli->next = flp;
4579 fli->started_p = 0;
4580 fli->file = v->line.file;
4581 gcc_assert (fli->file);
4582 flp = fli;
4584 oprintf (f, "\n/* GC roots. */\n\n");
4587 if (!deletable_p
4588 && length
4589 && v->type->kind == TYPE_POINTER
4590 && (v->type->u.p->kind == TYPE_POINTER
4591 || v->type->u.p->kind == TYPE_STRUCT))
4593 write_array (f, v, &ggc_wtd);
4594 write_array (f, v, &pch_wtd);
4598 for (v = variables; v; v = v->next)
4600 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4601 v->line.file));
4602 struct flist *fli;
4603 int skip_p = 0;
4604 int length_p = 0;
4605 options_p o;
4607 for (o = v->opt; o; o = o->next)
4608 if (strcmp (o->name, "length") == 0)
4609 length_p = 1;
4610 else if (strcmp (o->name, "deletable") == 0
4611 || strcmp (o->name, "if_marked") == 0)
4612 skip_p = 1;
4614 if (skip_p)
4615 continue;
4617 for (fli = flp; fli; fli = fli->next)
4618 if (fli->f == f)
4619 break;
4620 if (!fli->started_p)
4622 fli->started_p = 1;
4624 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4625 put_mangled_filename (f, v->line.file);
4626 oprintf (f, "[] = {\n");
4629 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4632 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4633 "gt_ggc_rtab");
4635 for (v = variables; v; v = v->next)
4637 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4638 v->line.file));
4639 struct flist *fli;
4640 int skip_p = 1;
4641 options_p o;
4643 for (o = v->opt; o; o = o->next)
4644 if (strcmp (o->name, "deletable") == 0)
4645 skip_p = 0;
4646 else if (strcmp (o->name, "if_marked") == 0)
4647 skip_p = 1;
4649 if (skip_p)
4650 continue;
4652 for (fli = flp; fli; fli = fli->next)
4653 if (fli->f == f)
4654 break;
4655 if (!fli->started_p)
4657 fli->started_p = 1;
4659 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4660 put_mangled_filename (f, v->line.file);
4661 oprintf (f, "[] = {\n");
4664 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4665 v->name, v->name);
4668 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4669 "gt_ggc_deletable_rtab");
4671 for (v = variables; v; v = v->next)
4673 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4674 v->line.file));
4675 struct flist *fli;
4676 const char *if_marked = NULL;
4677 int length_p = 0;
4678 options_p o;
4680 for (o = v->opt; o; o = o->next)
4681 if (strcmp (o->name, "length") == 0)
4682 length_p = 1;
4683 else if (strcmp (o->name, "if_marked") == 0
4684 && o->kind == OPTION_STRING)
4685 if_marked = o->info.string;
4686 if (if_marked == NULL)
4687 continue;
4688 if (v->type->kind != TYPE_POINTER
4689 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4690 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4691 TYPE_STRUCT))
4693 error_at_line (&v->line,
4694 "if_marked option used but not hash table");
4695 continue;
4698 for (fli = flp; fli; fli = fli->next)
4699 if (fli->f == f)
4700 break;
4701 if (!fli->started_p)
4703 fli->started_p = 1;
4705 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4706 put_mangled_filename (f, v->line.file);
4707 oprintf (f, "[] = {\n");
4710 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4711 v->name, length_p, &v->line, if_marked, emit_pch);
4714 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4715 "gt_ggc_cache_rtab");
4717 if (!emit_pch)
4718 return;
4720 for (v = variables; v; v = v->next)
4722 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4723 v->line.file));
4724 struct flist *fli;
4725 int length_p = 0;
4726 int if_marked_p = 0;
4727 options_p o;
4729 for (o = v->opt; o; o = o->next)
4730 if (strcmp (o->name, "length") == 0)
4731 length_p = 1;
4732 else if (strcmp (o->name, "if_marked") == 0)
4733 if_marked_p = 1;
4735 if (!if_marked_p)
4736 continue;
4738 for (fli = flp; fli; fli = fli->next)
4739 if (fli->f == f)
4740 break;
4741 if (!fli->started_p)
4743 fli->started_p = 1;
4745 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4746 put_mangled_filename (f, v->line.file);
4747 oprintf (f, "[] = {\n");
4750 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4753 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4754 "gt_pch_cache_rtab");
4756 for (v = variables; v; v = v->next)
4758 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4759 v->line.file));
4760 struct flist *fli;
4761 int skip_p = 0;
4762 options_p o;
4764 for (o = v->opt; o; o = o->next)
4765 if (strcmp (o->name, "deletable") == 0
4766 || strcmp (o->name, "if_marked") == 0)
4767 skip_p = 1;
4769 if (skip_p)
4770 continue;
4772 if (!contains_scalar_p (v->type))
4773 continue;
4775 for (fli = flp; fli; fli = fli->next)
4776 if (fli->f == f)
4777 break;
4778 if (!fli->started_p)
4780 fli->started_p = 1;
4782 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4783 put_mangled_filename (f, v->line.file);
4784 oprintf (f, "[] = {\n");
4787 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4788 v->name, v->name);
4791 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4792 "gt_pch_scalar_rtab");
4795 /* TRUE if type S has the GTY variable_size annotation. */
4797 static bool
4798 variable_size_p (const type_p s)
4800 options_p o;
4801 for (o = s->u.s.opt; o; o = o->next)
4802 if (strcmp (o->name, "variable_size") == 0)
4803 return true;
4804 return false;
4807 enum alloc_quantity
4808 { single, vector };
4809 enum alloc_zone
4810 { any_zone, specific_zone };
4812 /* Writes one typed allocator definition into output F for type
4813 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4814 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4815 is true, the allocator will have an extra parameter specifying
4816 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4817 vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
4818 the allocator will be zone-specific. */
4820 static void
4821 write_typed_alloc_def (outf_p f,
4822 bool variable_size, const char *type_specifier,
4823 const char *type_name, const char *allocator_type,
4824 enum alloc_quantity quantity, enum alloc_zone zone)
4826 bool two_args = variable_size && (quantity == vector);
4827 bool third_arg = ((zone == specific_zone)
4828 && (variable_size || (quantity == vector)));
4829 gcc_assert (f != NULL);
4830 const char *type_name_as_id = filter_type_name (type_name);
4831 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name_as_id);
4832 oprintf (f, "(%s%s%s%s%s) ",
4833 (variable_size ? "SIZE" : ""),
4834 (two_args ? ", " : ""),
4835 (quantity == vector) ? "n" : "",
4836 (third_arg ? ", " : ""), (zone == specific_zone) ? "z" : "");
4837 oprintf (f, "((%s%s *)", type_specifier, type_name);
4838 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4839 if (zone == specific_zone)
4840 oprintf (f, "z, ");
4841 if (variable_size)
4842 oprintf (f, "SIZE");
4843 else
4844 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4845 if (quantity == vector)
4846 oprintf (f, ", n");
4847 oprintf (f, " MEM_STAT_INFO)))\n");
4848 if (type_name_as_id != type_name)
4849 free (CONST_CAST(char *, type_name_as_id));
4852 /* Writes a typed allocator definition into output F for a struct or
4853 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4855 static void
4856 write_typed_struct_alloc_def (outf_p f,
4857 const type_p s, const char *allocator_type,
4858 enum alloc_quantity quantity,
4859 enum alloc_zone zone)
4861 gcc_assert (union_or_struct_p (s));
4862 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4863 s->u.s.tag, allocator_type, quantity, zone);
4866 /* Writes a typed allocator definition into output F for a typedef P,
4867 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4869 static void
4870 write_typed_typedef_alloc_def (outf_p f,
4871 const pair_p p, const char *allocator_type,
4872 enum alloc_quantity quantity,
4873 enum alloc_zone zone)
4875 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4876 allocator_type, quantity, zone);
4879 /* Writes typed allocator definitions into output F for the types in
4880 STRUCTURES and TYPEDEFS that are used by GC. */
4882 static void
4883 write_typed_alloc_defns (outf_p f,
4884 const type_p structures, const pair_p typedefs)
4886 type_p s;
4887 pair_p p;
4889 gcc_assert (f != NULL);
4890 oprintf (f,
4891 "\n/* Allocators for known structs and unions. */\n\n");
4892 for (s = structures; s; s = s->next)
4894 if (!USED_BY_TYPED_GC_P (s))
4895 continue;
4896 gcc_assert (union_or_struct_p (s));
4897 /* In plugin mode onput output ggc_alloc macro definitions
4898 relevant to plugin input files. */
4899 if (nb_plugin_files > 0
4900 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
4901 continue;
4902 write_typed_struct_alloc_def (f, s, "", single, any_zone);
4903 write_typed_struct_alloc_def (f, s, "cleared_", single, any_zone);
4904 write_typed_struct_alloc_def (f, s, "vec_", vector, any_zone);
4905 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector, any_zone);
4906 write_typed_struct_alloc_def (f, s, "zone_", single, specific_zone);
4907 write_typed_struct_alloc_def (f, s, "zone_cleared_", single,
4908 specific_zone);
4909 write_typed_struct_alloc_def (f, s, "zone_vec_", vector, specific_zone);
4910 write_typed_struct_alloc_def (f, s, "zone_cleared_vec_", vector,
4911 specific_zone);
4914 oprintf (f, "\n/* Allocators for known typedefs. */\n");
4915 for (p = typedefs; p; p = p->next)
4917 s = p->type;
4918 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4919 continue;
4920 /* In plugin mode onput output ggc_alloc macro definitions
4921 relevant to plugin input files. */
4922 if (nb_plugin_files > 0)
4924 struct fileloc* filoc = type_fileloc(s);
4925 if (!filoc || !filoc->file->inpisplugin)
4926 continue;
4928 write_typed_typedef_alloc_def (f, p, "", single, any_zone);
4929 write_typed_typedef_alloc_def (f, p, "cleared_", single, any_zone);
4930 write_typed_typedef_alloc_def (f, p, "vec_", vector, any_zone);
4931 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector, any_zone);
4932 write_typed_typedef_alloc_def (f, p, "zone_", single, specific_zone);
4933 write_typed_typedef_alloc_def (f, p, "zone_cleared_", single,
4934 specific_zone);
4935 write_typed_typedef_alloc_def (f, p, "zone_cleared_vec_", vector,
4936 specific_zone);
4940 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4941 guaranteee for somewhat increased readability. If name conflicts do happen,
4942 this funcion will have to be adjusted to be more like
4943 output_mangled_typename. */
4945 static void
4946 output_typename (outf_p of, const_type_p t)
4948 switch (t->kind)
4950 case TYPE_STRING:
4951 oprintf (of, "str");
4952 break;
4953 case TYPE_SCALAR:
4954 oprintf (of, "scalar");
4955 break;
4956 case TYPE_POINTER:
4957 output_typename (of, t->u.p);
4958 break;
4959 case TYPE_STRUCT:
4960 case TYPE_USER_STRUCT:
4961 case TYPE_UNION:
4962 case TYPE_LANG_STRUCT:
4963 oprintf (of, "%s", t->u.s.tag);
4964 break;
4965 case TYPE_PARAM_STRUCT:
4967 int i;
4968 for (i = 0; i < NUM_PARAM; i++)
4969 if (t->u.param_struct.param[i] != NULL)
4971 output_typename (of, t->u.param_struct.param[i]);
4972 oprintf (of, "_");
4974 output_typename (of, t->u.param_struct.stru);
4975 break;
4977 case TYPE_NONE:
4978 case TYPE_UNDEFINED:
4979 case TYPE_ARRAY:
4980 gcc_unreachable ();
4984 /* Writes a typed GC allocator for type S that is suitable as a callback for
4985 the splay tree implementation in libiberty. */
4987 static void
4988 write_splay_tree_allocator_def (const_type_p s)
4990 outf_p of = get_output_file_with_visibility (NULL);
4991 oprintf (of, "void * ggc_alloc_splay_tree_");
4992 output_typename (of, s);
4993 oprintf (of, " (int sz, void * nl)\n");
4994 oprintf (of, "{\n");
4995 oprintf (of, " return ggc_splay_alloc (");
4996 oprintf (of, "gt_e_");
4997 output_mangled_typename (of, s);
4998 oprintf (of, ", sz, nl);\n");
4999 oprintf (of, "}\n\n");
5002 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5003 for the splay tree implementation in libiberty. */
5005 static void
5006 write_splay_tree_allocators (const_type_p param_structs)
5008 const_type_p s;
5010 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
5011 for (s = param_structs; s; s = s->next)
5012 if (s->gc_used == GC_POINTED_TO)
5014 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
5015 output_typename (header_file, s);
5016 oprintf (header_file, " (int, void *);\n");
5017 write_splay_tree_allocator_def (s);
5021 #define INDENT 2
5023 /* Dumps the value of typekind KIND. */
5025 static void
5026 dump_typekind (int indent, enum typekind kind)
5028 printf ("%*ckind = ", indent, ' ');
5029 switch (kind)
5031 case TYPE_SCALAR:
5032 printf ("TYPE_SCALAR");
5033 break;
5034 case TYPE_STRING:
5035 printf ("TYPE_STRING");
5036 break;
5037 case TYPE_STRUCT:
5038 printf ("TYPE_STRUCT");
5039 break;
5040 case TYPE_UNDEFINED:
5041 printf ("TYPE_UNDEFINED");
5042 break;
5043 case TYPE_USER_STRUCT:
5044 printf ("TYPE_USER_STRUCT");
5045 break;
5046 case TYPE_UNION:
5047 printf ("TYPE_UNION");
5048 break;
5049 case TYPE_POINTER:
5050 printf ("TYPE_POINTER");
5051 break;
5052 case TYPE_ARRAY:
5053 printf ("TYPE_ARRAY");
5054 break;
5055 case TYPE_LANG_STRUCT:
5056 printf ("TYPE_LANG_STRUCT");
5057 break;
5058 case TYPE_PARAM_STRUCT:
5059 printf ("TYPE_PARAM_STRUCT");
5060 break;
5061 default:
5062 gcc_unreachable ();
5064 printf ("\n");
5067 /* Dumps the value of GC_USED flag. */
5069 static void
5070 dump_gc_used (int indent, enum gc_used_enum gc_used)
5072 printf ("%*cgc_used = ", indent, ' ');
5073 switch (gc_used)
5075 case GC_UNUSED:
5076 printf ("GC_UNUSED");
5077 break;
5078 case GC_USED:
5079 printf ("GC_USED");
5080 break;
5081 case GC_MAYBE_POINTED_TO:
5082 printf ("GC_MAYBE_POINTED_TO");
5083 break;
5084 case GC_POINTED_TO:
5085 printf ("GC_POINTED_TO");
5086 break;
5087 default:
5088 gcc_unreachable ();
5090 printf ("\n");
5093 /* Dumps the type options OPT. */
5095 static void
5096 dump_options (int indent, options_p opt)
5098 options_p o;
5099 printf ("%*coptions = ", indent, ' ');
5100 o = opt;
5101 while (o)
5103 switch (o->kind)
5105 case OPTION_STRING:
5106 printf ("%s:string %s ", o->name, o->info.string);
5107 break;
5108 case OPTION_TYPE:
5109 printf ("%s:type ", o->name);
5110 dump_type (indent+1, o->info.type);
5111 break;
5112 case OPTION_NESTED:
5113 printf ("%s:nested ", o->name);
5114 break;
5115 case OPTION_NONE:
5116 gcc_unreachable ();
5118 o = o->next;
5120 printf ("\n");
5123 /* Dumps the source file location in LINE. */
5125 static void
5126 dump_fileloc (int indent, struct fileloc line)
5128 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5129 get_input_file_name (line.file),
5130 line.line);
5133 /* Recursively dumps the struct, union, or a language-specific
5134 struct T. */
5136 static void
5137 dump_type_u_s (int indent, type_p t)
5139 pair_p fields;
5141 gcc_assert (union_or_struct_p (t));
5142 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5143 dump_fileloc (indent, t->u.s.line);
5144 printf ("%*cu.s.fields =\n", indent, ' ');
5145 fields = t->u.s.fields;
5146 while (fields)
5148 dump_pair (indent + INDENT, fields);
5149 fields = fields->next;
5151 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5152 dump_options (indent, t->u.s.opt);
5153 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5154 if (t->kind == TYPE_LANG_STRUCT)
5156 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5157 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5161 /* Recursively dumps the array T. */
5163 static void
5164 dump_type_u_a (int indent, type_p t)
5166 gcc_assert (t->kind == TYPE_ARRAY);
5167 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5168 dump_type_list (indent + INDENT, t->u.a.p);
5171 /* Recursively dumps the parameterized struct T. */
5173 static void
5174 dump_type_u_param_struct (int indent, type_p t)
5176 int i;
5177 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5178 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5179 dump_type_list (indent, t->u.param_struct.stru);
5180 dump_fileloc (indent, t->u.param_struct.line);
5181 for (i = 0; i < NUM_PARAM; i++)
5183 if (t->u.param_struct.param[i] == NULL)
5184 continue;
5185 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5186 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5190 /* Recursively dumps the type list T. */
5192 static void
5193 dump_type_list (int indent, type_p t)
5195 type_p p = t;
5196 while (p)
5198 dump_type (indent, p);
5199 p = p->next;
5203 static htab_t seen_types;
5205 /* Recursively dumps the type T if it was not dumped previously. */
5207 static void
5208 dump_type (int indent, type_p t)
5210 PTR *slot;
5212 if (seen_types == NULL)
5213 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5215 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5216 slot = htab_find_slot (seen_types, t, INSERT);
5217 if (*slot != NULL)
5219 printf ("already seen.\n");
5220 return;
5222 *slot = t;
5223 printf ("\n");
5225 dump_typekind (indent, t->kind);
5226 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5227 (void *) t->pointer_to);
5228 dump_gc_used (indent + INDENT, t->gc_used);
5229 switch (t->kind)
5231 case TYPE_SCALAR:
5232 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5233 t->u.scalar_is_char ? "true" : "false");
5234 break;
5235 case TYPE_STRING:
5236 break;
5237 case TYPE_STRUCT:
5238 case TYPE_UNION:
5239 case TYPE_LANG_STRUCT:
5240 case TYPE_USER_STRUCT:
5241 dump_type_u_s (indent + INDENT, t);
5242 break;
5243 case TYPE_POINTER:
5244 printf ("%*cp:\n", indent + INDENT, ' ');
5245 dump_type (indent + INDENT, t->u.p);
5246 break;
5247 case TYPE_ARRAY:
5248 dump_type_u_a (indent + INDENT, t);
5249 break;
5250 case TYPE_PARAM_STRUCT:
5251 dump_type_u_param_struct (indent + INDENT, t);
5252 break;
5253 default:
5254 gcc_unreachable ();
5256 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5259 /* Dumps the pair P. */
5261 static void
5262 dump_pair (int indent, pair_p p)
5264 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5265 dump_type (indent, p->type);
5266 dump_fileloc (indent, p->line);
5267 dump_options (indent, p->opt);
5268 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5271 /* Dumps the list of pairs PP. */
5273 static void
5274 dump_pair_list (const char *name, pair_p pp)
5276 pair_p p;
5277 printf ("%s:\n", name);
5278 for (p = pp; p != NULL; p = p->next)
5279 dump_pair (0, p);
5280 printf ("End of %s\n\n", name);
5283 /* Dumps the STRUCTURES. */
5285 static void
5286 dump_structures (const char *name, type_p structures)
5288 printf ("%s:\n", name);
5289 dump_type_list (0, structures);
5290 printf ("End of %s\n\n", name);
5293 /* Dumps the internal structures of gengtype. This is useful to debug
5294 gengtype itself, or to understand what it does, e.g. for plugin
5295 developers. */
5297 static void
5298 dump_everything (void)
5300 dump_pair_list ("typedefs", typedefs);
5301 dump_structures ("structures", structures);
5302 dump_structures ("param_structs", param_structs);
5303 dump_pair_list ("variables", variables);
5305 /* Allocated with the first call to dump_type. */
5306 htab_delete (seen_types);
5311 /* Option specification for getopt_long. */
5312 static const struct option gengtype_long_options[] = {
5313 {"help", no_argument, NULL, 'h'},
5314 {"version", no_argument, NULL, 'V'},
5315 {"verbose", no_argument, NULL, 'v'},
5316 {"dump", no_argument, NULL, 'd'},
5317 {"debug", no_argument, NULL, 'D'},
5318 {"plugin", required_argument, NULL, 'P'},
5319 {"srcdir", required_argument, NULL, 'S'},
5320 {"backupdir", required_argument, NULL, 'B'},
5321 {"inputs", required_argument, NULL, 'I'},
5322 {"read-state", required_argument, NULL, 'r'},
5323 {"write-state", required_argument, NULL, 'w'},
5324 /* Terminating NULL placeholder. */
5325 {NULL, no_argument, NULL, 0},
5329 static void
5330 print_usage (void)
5332 printf ("Usage: %s\n", progname);
5333 printf ("\t -h | --help " " \t# Give this help.\n");
5334 printf ("\t -D | --debug "
5335 " \t# Give debug output to debug %s itself.\n", progname);
5336 printf ("\t -V | --version " " \t# Give version information.\n");
5337 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5338 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5339 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5340 " \t# Generate for plugin.\n");
5341 printf ("\t -S | --srcdir <GCC-directory> "
5342 " \t# Specify the GCC source directory.\n");
5343 printf ("\t -B | --backupdir <directory> "
5344 " \t# Specify the backup directory for updated files.\n");
5345 printf ("\t -I | --inputs <input-list> "
5346 " \t# Specify the file with source files list.\n");
5347 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5348 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5351 static void
5352 print_version (void)
5354 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5355 printf ("Report bugs: %s\n", bug_report_url);
5358 /* Parse the program options using getopt_long... */
5359 static void
5360 parse_program_options (int argc, char **argv)
5362 int opt = -1;
5363 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5364 gengtype_long_options, NULL)) >= 0)
5366 switch (opt)
5368 case 'h': /* --help */
5369 print_usage ();
5370 break;
5371 case 'V': /* --version */
5372 print_version ();
5373 break;
5374 case 'd': /* --dump */
5375 do_dump = 1;
5376 break;
5377 case 'D': /* --debug */
5378 do_debug = 1;
5379 break;
5380 case 'v': /* --verbose */
5381 verbosity_level++;
5382 break;
5383 case 'P': /* --plugin */
5384 if (optarg)
5385 plugin_output_filename = optarg;
5386 else
5387 fatal ("missing plugin output file name");
5388 break;
5389 case 'S': /* --srcdir */
5390 if (optarg)
5391 srcdir = optarg;
5392 else
5393 fatal ("missing source directory");
5394 srcdir_len = strlen (srcdir);
5395 break;
5396 case 'B': /* --backupdir */
5397 if (optarg)
5398 backup_dir = optarg;
5399 else
5400 fatal ("missing backup directory");
5401 break;
5402 case 'I': /* --inputs */
5403 if (optarg)
5404 inputlist = optarg;
5405 else
5406 fatal ("missing input list");
5407 break;
5408 case 'r': /* --read-state */
5409 if (optarg)
5410 read_state_filename = optarg;
5411 else
5412 fatal ("missing read state file");
5413 DBGPRINTF ("read state %s\n", optarg);
5414 break;
5415 case 'w': /* --write-state */
5416 DBGPRINTF ("write state %s\n", optarg);
5417 if (optarg)
5418 write_state_filename = optarg;
5419 else
5420 fatal ("missing write state file");
5421 break;
5422 default:
5423 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5424 print_usage ();
5425 fatal ("unexpected flag");
5428 if (plugin_output_filename)
5430 /* In plugin mode we require some input files. */
5431 int i = 0;
5432 if (optind >= argc)
5433 fatal ("no source files given in plugin mode");
5434 nb_plugin_files = argc - optind;
5435 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5436 for (i = 0; i < (int) nb_plugin_files; i++)
5438 char *name = argv[i + optind];
5439 plugin_files[i] = input_file_by_name (name);
5446 /******* Manage input files. ******/
5448 /* Hash table of unique input file names. */
5449 static htab_t input_file_htab;
5451 /* Find or allocate a new input_file by hash-consing it. */
5452 input_file*
5453 input_file_by_name (const char* name)
5455 PTR* slot;
5456 input_file* f = NULL;
5457 int namlen = 0;
5458 if (!name)
5459 return NULL;
5460 namlen = strlen (name);
5461 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5462 f->inpbitmap = 0;
5463 f->inpoutf = NULL;
5464 f->inpisplugin = false;
5465 strcpy (f->inpname, name);
5466 slot = htab_find_slot (input_file_htab, f, INSERT);
5467 gcc_assert (slot != NULL);
5468 if (*slot)
5470 /* Already known input file. */
5471 free (f);
5472 return (input_file*)(*slot);
5474 /* New input file. */
5475 *slot = f;
5476 return f;
5479 /* Hash table support routines for input_file-s. */
5480 static hashval_t
5481 htab_hash_inputfile (const void *p)
5483 const input_file *inpf = (const input_file *) p;
5484 gcc_assert (inpf);
5485 return htab_hash_string (get_input_file_name (inpf));
5488 static int
5489 htab_eq_inputfile (const void *x, const void *y)
5491 const input_file *inpfx = (const input_file *) x;
5492 const input_file *inpfy = (const input_file *) y;
5493 gcc_assert (inpfx != NULL && inpfy != NULL);
5494 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5499 main (int argc, char **argv)
5501 size_t i;
5502 static struct fileloc pos = { NULL, 0 };
5503 outf_p output_header;
5505 /* Mandatory common initializations. */
5506 progname = "gengtype"; /* For fatal and messages. */
5507 /* Create the hash-table used to hash-cons input files. */
5508 input_file_htab =
5509 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5510 /* Initialize our special input files. */
5511 this_file = input_file_by_name (__FILE__);
5512 system_h_file = input_file_by_name ("system.h");
5513 /* Set the scalar_is_char union number for predefined scalar types. */
5514 scalar_nonchar.u.scalar_is_char = FALSE;
5515 scalar_char.u.scalar_is_char = TRUE;
5517 parse_program_options (argc, argv);
5519 #if ENABLE_CHECKING
5520 if (do_debug)
5522 time_t now = (time_t) 0;
5523 time (&now);
5524 DBGPRINTF ("gengtype started pid %d at %s",
5525 (int) getpid (), ctime (&now));
5527 #endif /* ENABLE_CHECKING */
5529 /* Parse the input list and the input files. */
5530 DBGPRINTF ("inputlist %s", inputlist);
5531 if (read_state_filename)
5533 if (inputlist)
5534 fatal ("input list %s cannot be given with a read state file %s",
5535 inputlist, read_state_filename);
5536 read_state (read_state_filename);
5537 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5538 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5540 else if (inputlist)
5542 /* These types are set up with #define or else outside of where
5543 we can see them. We should initialize them before calling
5544 read_input_list. */
5545 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5546 Call;} while(0)
5547 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5548 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5549 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5550 POS_HERE (do_scalar_typedef ("double_int", &pos));
5551 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5552 POS_HERE (do_scalar_typedef ("uint8", &pos));
5553 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5554 POS_HERE (do_scalar_typedef ("jword", &pos));
5555 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5556 POS_HERE (do_scalar_typedef ("void", &pos));
5557 POS_HERE (do_typedef ("PTR",
5558 create_pointer (resolve_typedef ("void", &pos)),
5559 &pos));
5560 #undef POS_HERE
5561 read_input_list (inputlist);
5562 for (i = 0; i < num_gt_files; i++)
5564 parse_file (get_input_file_name (gt_files[i]));
5565 DBGPRINTF ("parsed file #%d %s",
5566 (int) i, get_input_file_name (gt_files[i]));
5568 if (verbosity_level >= 1)
5569 printf ("%s parsed %d files with %d GTY types\n",
5570 progname, (int) num_gt_files, type_count);
5572 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5573 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5576 else
5577 fatal ("either an input list or a read state file should be given");
5578 if (hit_error)
5579 return 1;
5582 if (plugin_output_filename)
5584 size_t ix = 0;
5585 /* In plugin mode, we should have read a state file, and have
5586 given at least one plugin file. */
5587 if (!read_state_filename)
5588 fatal ("No read state given in plugin mode for %s",
5589 plugin_output_filename);
5591 if (nb_plugin_files == 0 || !plugin_files)
5592 fatal ("No plugin files given in plugin mode for %s",
5593 plugin_output_filename);
5595 /* Parse our plugin files and augment the state. */
5596 for (ix = 0; ix < nb_plugin_files; ix++)
5598 input_file* pluginput = plugin_files [ix];
5599 pluginput->inpisplugin = true;
5600 parse_file (get_input_file_name (pluginput));
5602 if (hit_error)
5603 return 1;
5605 plugin_output = create_file ("GCC", plugin_output_filename);
5606 DBGPRINTF ("created plugin_output %p named %s",
5607 (void *) plugin_output, plugin_output->name);
5609 else
5610 { /* No plugin files, we are in normal mode. */
5611 if (!srcdir)
5612 fatal ("gengtype needs a source directory in normal mode");
5614 if (hit_error)
5615 return 1;
5617 gen_rtx_next ();
5619 /* The call to set_gc_used may indirectly call find_param_structure
5620 hence enlarge the param_structs list of types. */
5621 set_gc_used (variables);
5623 /* The state at this point is read from the state input file or by
5624 parsing source files and optionally augmented by parsing plugin
5625 source files. Write it now. */
5626 if (write_state_filename)
5628 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5629 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5631 if (hit_error)
5632 fatal ("didn't write state file %s after errors",
5633 write_state_filename);
5635 DBGPRINTF ("before write_state %s", write_state_filename);
5636 write_state (write_state_filename);
5638 if (do_dump)
5639 dump_everything ();
5641 /* After having written the state file we return immediately to
5642 avoid generating any output file. */
5643 if (hit_error)
5644 return 1;
5645 else
5646 return 0;
5650 open_base_files ();
5652 write_enum_defn (structures, param_structs);
5653 output_header = plugin_output ? plugin_output : header_file;
5654 write_typed_alloc_defns (output_header, structures, typedefs);
5655 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5656 structures);
5657 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5658 param_structs);
5660 write_types (output_header, structures, param_structs, &ggc_wtd);
5661 if (plugin_files == NULL)
5663 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5664 structures);
5665 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5666 param_structs);
5667 write_types (header_file, structures, param_structs, &pch_wtd);
5668 write_local (header_file, structures, param_structs);
5670 write_splay_tree_allocators (param_structs);
5671 write_roots (variables, plugin_files == NULL);
5672 write_rtx_next ();
5673 close_output_files ();
5675 if (do_dump)
5676 dump_everything ();
5678 /* Don't bother about free-ing any input or plugin file, etc. */
5680 if (hit_error)
5681 return 1;
5682 return 0;