2012-09-04 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / gengtype.c
blob2ae43726b59a50524d72db8b62f2d391f3ecbf47
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;
175 type_p p = NULL;
176 for (p = t; p; p = p->next)
178 nb_types++;
179 switch (p->kind)
181 case TYPE_SCALAR:
182 nb_scalar++;
183 break;
184 case TYPE_STRING:
185 nb_string++;
186 break;
187 case TYPE_STRUCT:
188 nb_struct++;
189 break;
190 case TYPE_USER_STRUCT:
191 nb_user_struct++;
192 break;
193 case TYPE_UNION:
194 nb_union++;
195 break;
196 case TYPE_POINTER:
197 nb_pointer++;
198 break;
199 case TYPE_ARRAY:
200 nb_array++;
201 break;
202 case TYPE_LANG_STRUCT:
203 nb_lang_struct++;
204 break;
205 case TYPE_PARAM_STRUCT:
206 nb_param_struct++;
207 break;
208 default:
209 gcc_unreachable ();
212 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
213 lbasename (fil), lin, msg, nb_types);
214 if (nb_scalar > 0 || nb_string > 0)
215 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
216 if (nb_struct > 0 || nb_union > 0)
217 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
218 if (nb_pointer > 0 || nb_array > 0)
219 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
220 if (nb_lang_struct > 0 || nb_param_struct > 0)
221 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
222 nb_lang_struct, nb_param_struct);
223 if (nb_user_struct > 0)
224 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
225 fprintf (stderr, "\n");
227 #endif /* ENABLE_CHECKING */
229 /* Scan the input file, LIST, and determine how much space we need to
230 store strings in. Also, count the number of language directories
231 and files. The numbers returned are overestimates as they does not
232 consider repeated files. */
233 static size_t
234 measure_input_list (FILE *list)
236 size_t n = 0;
237 int c;
238 bool atbol = true;
239 num_lang_dirs = 0;
240 num_gt_files = plugin_files ? nb_plugin_files : 0;
241 while ((c = getc (list)) != EOF)
243 n++;
244 if (atbol)
246 if (c == '[')
247 num_lang_dirs++;
248 else
250 /* Add space for a lang_bitmap before the input file name. */
251 n += sizeof (lang_bitmap);
252 num_gt_files++;
254 atbol = false;
257 if (c == '\n')
258 atbol = true;
261 rewind (list);
262 return n;
265 /* Read one input line from LIST to HEREP (which is updated). A
266 pointer to the string is returned via LINEP. If it was a language
267 subdirectory in square brackets, strip off the square brackets and
268 return true. Otherwise, leave space before the string for a
269 lang_bitmap, and return false. At EOF, returns false, does not
270 touch *HEREP, and sets *LINEP to NULL. POS is used for
271 diagnostics. */
272 static bool
273 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
275 char *here = *herep;
276 char *line;
277 int c = getc (list);
279 /* Read over whitespace. */
280 while (c == '\n' || c == ' ')
281 c = getc (list);
283 if (c == EOF)
285 *linep = 0;
286 return false;
288 else if (c == '[')
290 /* No space for a lang_bitmap is necessary. Discard the '['. */
291 c = getc (list);
292 line = here;
293 while (c != ']' && c != '\n' && c != EOF)
295 *here++ = c;
296 c = getc (list);
298 *here++ = '\0';
300 if (c == ']')
302 c = getc (list); /* eat what should be a newline */
303 if (c != '\n' && c != EOF)
304 error_at_line (pos, "junk on line after language tag [%s]", line);
306 else
307 error_at_line (pos, "missing close bracket for language tag [%s",
308 line);
310 *herep = here;
311 *linep = line;
312 return true;
314 else
316 /* Leave space for a lang_bitmap. */
317 memset (here, 0, sizeof (lang_bitmap));
318 here += sizeof (lang_bitmap);
319 line = here;
322 *here++ = c;
323 c = getc (list);
325 while (c != EOF && c != '\n');
326 *here++ = '\0';
327 *herep = here;
328 *linep = line;
329 return false;
333 /* Read the list of input files from LIST and compute all of the
334 relevant tables. There is one file per line of the list. At
335 first, all the files on the list are language-generic, but
336 eventually a line will appear which is the name of a language
337 subdirectory in square brackets, like this: [cp]. All subsequent
338 files are specific to that language, until another language
339 subdirectory tag appears. Files can appear more than once, if
340 they apply to more than one language. */
341 static void
342 read_input_list (const char *listname)
344 FILE *list = fopen (listname, "r");
345 if (!list)
346 fatal ("cannot open %s: %s", listname, xstrerror (errno));
347 else
349 struct fileloc epos;
350 size_t bufsz = measure_input_list (list);
351 char *buf = XNEWVEC (char, bufsz);
352 char *here = buf;
353 char *committed = buf;
354 char *limit = buf + bufsz;
355 char *line;
356 bool is_language;
357 size_t langno = 0;
358 size_t nfiles = 0;
359 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
361 epos.file = input_file_by_name (listname);
362 epos.line = 0;
364 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
365 gt_files = XNEWVEC (const input_file *, num_gt_files);
367 for (;;)
369 next_line:
370 epos.line++;
371 committed = here;
372 is_language = read_input_line (list, &here, &line, &epos);
373 gcc_assert (here <= limit);
374 if (line == 0)
375 break;
376 else if (is_language)
378 size_t i;
379 gcc_assert (langno <= num_lang_dirs);
380 for (i = 0; i < langno; i++)
381 if (strcmp (lang_dir_names[i], line) == 0)
383 error_at_line (&epos, "duplicate language tag [%s]",
384 line);
385 curlangs = 1 << i;
386 here = committed;
387 goto next_line;
390 curlangs = 1 << langno;
391 lang_dir_names[langno++] = line;
393 else
395 size_t i;
396 input_file *inpf = input_file_by_name (line);
397 gcc_assert (nfiles <= num_gt_files);
398 for (i = 0; i < nfiles; i++)
399 /* Since the input_file-s are uniquely hash-consed, we
400 can just compare pointers! */
401 if (gt_files[i] == inpf)
403 /* Throw away the string we just read, and add the
404 current language to the existing string's bitmap. */
405 lang_bitmap bmap = get_lang_bitmap (inpf);
406 if (bmap & curlangs)
407 error_at_line (&epos,
408 "file %s specified more than once "
409 "for language %s", line,
410 langno ==
411 0 ? "(all)" : lang_dir_names[langno -
412 1]);
414 bmap |= curlangs;
415 set_lang_bitmap (inpf, bmap);
416 here = committed;
417 goto next_line;
420 set_lang_bitmap (inpf, curlangs);
421 gt_files[nfiles++] = inpf;
424 /* Update the global counts now that we know accurately how many
425 things there are. (We do not bother resizing the arrays down.) */
426 num_lang_dirs = langno;
427 /* Add the plugin files if provided. */
428 if (plugin_files)
430 size_t i;
431 for (i = 0; i < nb_plugin_files; i++)
432 gt_files[nfiles++] = plugin_files[i];
434 num_gt_files = nfiles;
437 /* Sanity check: any file that resides in a language subdirectory
438 (e.g. 'cp') ought to belong to the corresponding language.
439 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
440 (Can you even do that? Should you be allowed to?) */
442 size_t f;
443 for (f = 0; f < num_gt_files; f++)
445 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
446 const char *basename = get_file_basename (gt_files[f]);
447 const char *slashpos = strchr (basename, '/');
448 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
449 const char *slashpos2 = strchr (basename, '\\');
451 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
452 slashpos = slashpos2;
453 #endif
455 if (slashpos)
457 size_t l;
458 for (l = 0; l < num_lang_dirs; l++)
459 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
460 && memcmp (basename, lang_dir_names[l],
461 strlen (lang_dir_names[l])) == 0)
463 if (!(bitmap & (1 << l)))
464 error ("%s is in language directory '%s' but is not "
465 "tagged for that language",
466 basename, lang_dir_names[l]);
467 break;
473 if (ferror (list))
474 fatal ("error reading %s: %s", listname, xstrerror (errno));
476 fclose (list);
481 /* The one and only TYPE_STRING. */
483 struct type string_type = {
484 TYPE_STRING, 0, 0, 0, GC_USED, {0}
487 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
488 set early in main. */
490 struct type scalar_nonchar = {
491 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
494 struct type scalar_char = {
495 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
498 /* Lists of various things. */
500 pair_p typedefs;
501 type_p structures;
502 type_p param_structs;
503 pair_p variables;
505 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
506 static type_p adjust_field_tree_exp (type_p t, options_p opt);
507 static type_p adjust_field_rtx_def (type_p t, options_p opt);
509 /* Define S as a typedef to T at POS. */
511 void
512 do_typedef (const char *s, type_p t, struct fileloc *pos)
514 pair_p p;
516 /* temporary kludge - gengtype doesn't handle conditionals or
517 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
518 is coming from this file (main() sets them up with safe dummy
519 definitions). */
520 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
521 return;
523 for (p = typedefs; p != NULL; p = p->next)
524 if (strcmp (p->name, s) == 0)
526 if (p->type != t)
528 error_at_line (pos, "type `%s' previously defined", s);
529 error_at_line (&p->line, "previously defined here");
531 return;
534 p = XNEW (struct pair);
535 p->next = typedefs;
536 p->name = s;
537 p->type = t;
538 p->line = *pos;
539 p->opt = NULL;
540 typedefs = p;
543 /* Define S as a typename of a scalar. Cannot be used to define
544 typedefs of 'char'. Note: is also used for pointer-to-function
545 typedefs (which are therefore not treated as pointers). */
547 void
548 do_scalar_typedef (const char *s, struct fileloc *pos)
550 do_typedef (s, &scalar_nonchar, pos);
554 /* Define TYPE_NAME to be a user defined type at location POS. */
556 static type_p
557 create_user_defined_type (const char *type_name, struct fileloc *pos)
559 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
560 ty->u.s.line = *pos;
561 ty->u.s.bitmap = get_lang_bitmap (pos->file);
562 do_typedef (type_name, ty, pos);
564 /* If TYPE_NAME specifies a template, create references to the types
565 in the template by pretending that each type is a field of TY.
566 This is needed to make sure that the types referenced by the
567 template are marked as used. */
568 char *str = xstrdup (type_name);
569 char *open_bracket = strchr (str, '<');
570 if (open_bracket)
572 /* We only accept simple template declarations (see
573 require_template_declaration), so we only need to parse a
574 comma-separated list of strings, implicitly assumed to
575 be type names. */
576 char *arg = open_bracket + 1;
577 char *type_id = strtok (arg, ",>");
578 pair_p fields = 0;
579 while (type_id)
581 /* Create a new field for every type found inside the template
582 parameter list. */
583 const char *field_name = xstrdup (type_id);
584 type_p arg_type = resolve_typedef (field_name, pos);
585 fields = create_field_at (fields, arg_type, field_name, 0, pos);
586 type_id = strtok (0, ",>");
589 /* Associate the field list to TY. */
590 ty->u.s.fields = fields;
592 free (str);
594 return ty;
598 /* Return the type previously defined for S. Use POS to report errors. */
600 type_p
601 resolve_typedef (const char *s, struct fileloc *pos)
603 pair_p p;
604 for (p = typedefs; p != NULL; p = p->next)
605 if (strcmp (p->name, s) == 0)
606 return p->type;
608 /* If we did not find a typedef registered, assume this is a name
609 for a user-defined type which will need to provide its own
610 marking functions. */
611 return create_user_defined_type (s, pos);
614 /* Create and return a new structure with tag NAME at POS with fields
615 FIELDS and options O. The KIND of structure must be one of
616 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
618 type_p
619 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
620 pair_p fields, options_p o)
622 type_p si;
623 type_p s = NULL;
624 lang_bitmap bitmap = get_lang_bitmap (pos->file);
625 bool isunion = (kind == TYPE_UNION);
627 gcc_assert (union_or_struct_p (kind));
629 for (si = structures; si != NULL; si = si->next)
630 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
632 type_p ls = NULL;
633 if (si->kind == TYPE_LANG_STRUCT)
635 ls = si;
637 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
638 if (si->u.s.bitmap == bitmap)
639 s = si;
641 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
643 ls = si;
644 type_count++;
645 si = XCNEW (struct type);
646 memcpy (si, ls, sizeof (struct type));
647 ls->kind = TYPE_LANG_STRUCT;
648 ls->u.s.lang_struct = si;
649 ls->u.s.fields = NULL;
650 si->next = NULL;
651 si->state_number = -type_count;
652 si->pointer_to = NULL;
653 si->u.s.lang_struct = ls;
655 else
656 s = si;
658 if (ls != NULL && s == NULL)
660 type_count++;
661 s = XCNEW (struct type);
662 s->state_number = -type_count;
663 s->next = ls->u.s.lang_struct;
664 ls->u.s.lang_struct = s;
665 s->u.s.lang_struct = ls;
667 break;
670 if (s == NULL)
672 type_count++;
673 s = XCNEW (struct type);
674 s->state_number = -type_count;
675 s->next = structures;
676 structures = s;
679 if (s->u.s.line.file != NULL
680 || (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap)))
682 error_at_line (pos, "duplicate definition of '%s %s'",
683 isunion ? "union" : "struct", s->u.s.tag);
684 error_at_line (&s->u.s.line, "previous definition here");
687 s->kind = kind;
688 s->u.s.tag = name;
689 s->u.s.line = *pos;
690 s->u.s.fields = fields;
691 s->u.s.opt = o;
692 s->u.s.bitmap = bitmap;
693 if (s->u.s.lang_struct)
694 s->u.s.lang_struct->u.s.bitmap |= bitmap;
696 return s;
699 /* Return the previously-defined structure or union with tag NAME,
700 or a new empty structure or union if none was defined previously.
701 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
702 TYPE_USER_STRUCT. */
704 type_p
705 find_structure (const char *name, enum typekind kind)
707 type_p s;
708 bool isunion = (kind == TYPE_UNION);
710 gcc_assert (union_or_struct_p (kind));
712 for (s = structures; s != NULL; s = s->next)
713 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
714 return s;
716 type_count++;
717 s = XCNEW (struct type);
718 s->next = structures;
719 s->state_number = -type_count;
720 structures = s;
721 s->kind = kind;
722 s->u.s.tag = name;
723 structures = s;
724 return s;
727 /* Return the previously-defined parameterized structure for structure
728 T and parameters PARAM, or a new parameterized empty structure or
729 union if none was defined previously. */
731 static type_p
732 find_param_structure (type_p t, type_p param[NUM_PARAM])
734 type_p res;
736 for (res = param_structs; res; res = res->next)
737 if (res->u.param_struct.stru == t
738 && memcmp (res->u.param_struct.param, param,
739 sizeof (type_p) * NUM_PARAM) == 0)
740 break;
741 if (res == NULL)
743 type_count++;
744 res = XCNEW (struct type);
745 res->kind = TYPE_PARAM_STRUCT;
746 res->next = param_structs;
747 res->state_number = -type_count;
748 param_structs = res;
749 res->u.param_struct.stru = t;
750 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
752 return res;
755 /* Return a scalar type with name NAME. */
757 type_p
758 create_scalar_type (const char *name)
760 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
761 return &scalar_char;
762 else
763 return &scalar_nonchar;
766 /* Return a pointer to T. */
768 type_p
769 create_pointer (type_p t)
771 if (!t->pointer_to)
773 type_p r = XCNEW (struct type);
774 type_count++;
775 r->state_number = -type_count;
776 r->kind = TYPE_POINTER;
777 r->u.p = t;
778 t->pointer_to = r;
780 return t->pointer_to;
783 /* Return an array of length LEN. */
785 type_p
786 create_array (type_p t, const char *len)
788 type_p v;
790 type_count++;
791 v = XCNEW (struct type);
792 v->kind = TYPE_ARRAY;
793 v->state_number = -type_count;
794 v->u.a.p = t;
795 v->u.a.len = len;
796 return v;
799 /* Return a string options structure with name NAME and info INFO.
800 NEXT is the next option in the chain. */
801 options_p
802 create_string_option (options_p next, const char *name, const char *info)
804 options_p o = XNEW (struct options);
805 o->kind = OPTION_STRING;
806 o->next = next;
807 o->name = name;
808 o->info.string = info;
809 return o;
812 /* Create a type options structure with name NAME and info INFO. NEXT
813 is the next option in the chain. */
814 options_p
815 create_type_option (options_p next, const char* name, type_p info)
817 options_p o = XNEW (struct options);
818 o->next = next;
819 o->name = name;
820 o->kind = OPTION_TYPE;
821 o->info.type = info;
822 return o;
825 /* Create a nested pointer options structure with name NAME and info
826 INFO. NEXT is the next option in the chain. */
827 options_p
828 create_nested_option (options_p next, const char* name,
829 struct nested_ptr_data* info)
831 options_p o;
832 o = XNEW (struct options);
833 o->next = next;
834 o->name = name;
835 o->kind = OPTION_NESTED;
836 o->info.nested = info;
837 return o;
840 /* Return an options structure for a "nested_ptr" option. */
841 options_p
842 create_nested_ptr_option (options_p next, type_p t,
843 const char *to, const char *from)
845 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
847 d->type = adjust_field_type (t, 0);
848 d->convert_to = to;
849 d->convert_from = from;
850 return create_nested_option (next, "nested_ptr", d);
853 /* Add a variable named S of type T with options O defined at POS,
854 to `variables'. */
855 void
856 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
858 pair_p n;
859 n = XNEW (struct pair);
860 n->name = s;
861 n->type = t;
862 n->line = *pos;
863 n->opt = o;
864 n->next = variables;
865 variables = n;
868 /* Most-general structure field creator. */
869 static pair_p
870 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
871 const input_file *inpf, int line)
873 pair_p field;
875 field = XNEW (struct pair);
876 field->next = next;
877 field->type = type;
878 field->name = name;
879 field->opt = opt;
880 field->line.file = inpf;
881 field->line.line = line;
882 return field;
885 /* Create a field that came from the source code we are scanning,
886 i.e. we have a 'struct fileloc', and possibly options; also,
887 adjust_field_type should be called. */
888 pair_p
889 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
890 struct fileloc *pos)
892 return create_field_all (next, adjust_field_type (type, opt),
893 name, opt, pos->file, pos->line);
896 /* Create a fake field with the given type and name. NEXT is the next
897 field in the chain. */
898 #define create_field(next,type,name) \
899 create_field_all(next,type,name, 0, this_file, __LINE__)
901 /* Like create_field, but the field is only valid when condition COND
902 is true. */
904 static pair_p
905 create_optional_field_ (pair_p next, type_p type, const char *name,
906 const char *cond, int line)
908 static int id = 1;
909 pair_p union_fields;
910 type_p union_type;
912 /* Create a fake union type with a single nameless field of type TYPE.
913 The field has a tag of "1". This allows us to make the presence
914 of a field of type TYPE depend on some boolean "desc" being true. */
915 union_fields = create_field (NULL, type, "");
916 union_fields->opt =
917 create_string_option (union_fields->opt, "dot", "");
918 union_fields->opt =
919 create_string_option (union_fields->opt, "tag", "1");
920 union_type =
921 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
922 &lexer_line, union_fields, NULL);
924 /* Create the field and give it the new fake union type. Add a "desc"
925 tag that specifies the condition under which the field is valid. */
926 return create_field_all (next, union_type, name,
927 create_string_option (0, "desc", cond),
928 this_file, line);
931 #define create_optional_field(next,type,name,cond) \
932 create_optional_field_(next,type,name,cond,__LINE__)
934 /* Reverse a linked list of 'struct pair's in place. */
935 pair_p
936 nreverse_pairs (pair_p list)
938 pair_p prev = 0, p, next;
939 for (p = list; p; p = next)
941 next = p->next;
942 p->next = prev;
943 prev = p;
945 return prev;
949 /* We don't care how long a CONST_DOUBLE is. */
950 #define CONST_DOUBLE_FORMAT "ww"
951 /* We don't want to see codes that are only for generator files. */
952 #undef GENERATOR_FILE
954 enum rtx_code
956 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
957 #include "rtl.def"
958 #undef DEF_RTL_EXPR
959 NUM_RTX_CODE
962 static const char *const rtx_name[NUM_RTX_CODE] = {
963 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
964 #include "rtl.def"
965 #undef DEF_RTL_EXPR
968 static const char *const rtx_format[NUM_RTX_CODE] = {
969 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
970 #include "rtl.def"
971 #undef DEF_RTL_EXPR
974 static int rtx_next_new[NUM_RTX_CODE];
976 /* We also need codes and names for insn notes (not register notes).
977 Note that we do *not* bias the note values here. */
978 enum insn_note
980 #define DEF_INSN_NOTE(NAME) NAME,
981 #include "insn-notes.def"
982 #undef DEF_INSN_NOTE
984 NOTE_INSN_MAX
987 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
988 default field for line number notes. */
989 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
990 #define DEF_INSN_NOTE(NAME) #NAME,
991 #include "insn-notes.def"
992 #undef DEF_INSN_NOTE
995 #undef CONST_DOUBLE_FORMAT
996 #define GENERATOR_FILE
998 /* Generate the contents of the rtx_next array. This really doesn't belong
999 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1001 static void
1002 gen_rtx_next (void)
1004 int i;
1005 for (i = 0; i < NUM_RTX_CODE; i++)
1007 int k;
1009 rtx_next_new[i] = -1;
1010 if (strncmp (rtx_format[i], "iuu", 3) == 0)
1011 rtx_next_new[i] = 2;
1012 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1013 rtx_next_new[i] = 1;
1014 else
1015 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1016 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1017 rtx_next_new[i] = k;
1021 /* Write out the contents of the rtx_next array. */
1022 static void
1023 write_rtx_next (void)
1025 outf_p f = get_output_file_with_visibility (NULL);
1026 int i;
1027 if (!f)
1028 return;
1030 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1031 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1032 for (i = 0; i < NUM_RTX_CODE; i++)
1033 if (rtx_next_new[i] == -1)
1034 oprintf (f, " 0,\n");
1035 else
1036 oprintf (f,
1037 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1038 oprintf (f, "};\n");
1041 /* Handle `special("rtx_def")'. This is a special case for field
1042 `fld' of struct rtx_def, which is an array of unions whose values
1043 are based in a complex way on the type of RTL. */
1045 static type_p
1046 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1048 pair_p flds = NULL;
1049 options_p nodot;
1050 int i;
1051 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1052 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1054 if (t->kind != TYPE_UNION)
1056 error_at_line (&lexer_line,
1057 "special `rtx_def' must be applied to a union");
1058 return &string_type;
1061 nodot = create_string_option (NULL, "dot", "");
1063 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1064 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1065 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1066 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1067 reg_attrs_tp =
1068 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1069 basic_block_tp =
1070 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1071 constant_tp =
1072 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1073 scalar_tp = &scalar_nonchar; /* rtunion int */
1076 pair_p note_flds = NULL;
1077 int c;
1079 for (c = 0; c <= NOTE_INSN_MAX; c++)
1081 switch (c)
1083 case NOTE_INSN_MAX:
1084 case NOTE_INSN_DELETED_LABEL:
1085 case NOTE_INSN_DELETED_DEBUG_LABEL:
1086 note_flds = create_field (note_flds, &string_type, "rt_str");
1087 break;
1089 case NOTE_INSN_BLOCK_BEG:
1090 case NOTE_INSN_BLOCK_END:
1091 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1092 break;
1094 case NOTE_INSN_VAR_LOCATION:
1095 case NOTE_INSN_CALL_ARG_LOCATION:
1096 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1097 break;
1099 default:
1100 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1101 break;
1103 /* NOTE_INSN_MAX is used as the default field for line
1104 number notes. */
1105 if (c == NOTE_INSN_MAX)
1106 note_flds->opt =
1107 create_string_option (nodot, "default", "");
1108 else
1109 note_flds->opt =
1110 create_string_option (nodot, "tag", note_insn_name[c]);
1112 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1113 &lexer_line, note_flds, NULL);
1115 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1117 pair_p sym_flds;
1118 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1119 sym_flds->opt = create_string_option (nodot, "default", "");
1120 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1121 sym_flds->opt = create_string_option (nodot, "tag", "1");
1122 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1123 &lexer_line, sym_flds, NULL);
1125 for (i = 0; i < NUM_RTX_CODE; i++)
1127 pair_p subfields = NULL;
1128 size_t aindex, nmindex;
1129 const char *sname;
1130 type_p substruct;
1131 char *ftag;
1133 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1135 type_p t;
1136 const char *subname;
1138 switch (rtx_format[i][aindex])
1140 case '*':
1141 case 'i':
1142 case 'n':
1143 case 'w':
1144 t = scalar_tp;
1145 subname = "rt_int";
1146 break;
1148 case '0':
1149 if (i == MEM && aindex == 1)
1150 t = mem_attrs_tp, subname = "rt_mem";
1151 else if (i == JUMP_INSN && aindex == 8)
1152 t = rtx_tp, subname = "rt_rtx";
1153 else if (i == CODE_LABEL && aindex == 5)
1154 t = scalar_tp, subname = "rt_int";
1155 else if (i == CODE_LABEL && aindex == 4)
1156 t = rtx_tp, subname = "rt_rtx";
1157 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1158 t = rtx_tp, subname = "rt_rtx";
1159 else if (i == NOTE && aindex == 4)
1160 t = note_union_tp, subname = "";
1161 else if (i == NOTE && aindex == 5)
1162 t = scalar_tp, subname = "rt_int";
1163 else if (i == NOTE && aindex >= 7)
1164 t = scalar_tp, subname = "rt_int";
1165 else if (i == ADDR_DIFF_VEC && aindex == 4)
1166 t = scalar_tp, subname = "rt_int";
1167 else if (i == VALUE && aindex == 0)
1168 t = scalar_tp, subname = "rt_int";
1169 else if (i == DEBUG_EXPR && aindex == 0)
1170 t = tree_tp, subname = "rt_tree";
1171 else if (i == REG && aindex == 1)
1172 t = scalar_tp, subname = "rt_int";
1173 else if (i == REG && aindex == 2)
1174 t = reg_attrs_tp, subname = "rt_reg";
1175 else if (i == SCRATCH && aindex == 0)
1176 t = scalar_tp, subname = "rt_int";
1177 else if (i == SYMBOL_REF && aindex == 1)
1178 t = scalar_tp, subname = "rt_int";
1179 else if (i == SYMBOL_REF && aindex == 2)
1180 t = symbol_union_tp, subname = "";
1181 else if (i == BARRIER && aindex >= 3)
1182 t = scalar_tp, subname = "rt_int";
1183 else if (i == ENTRY_VALUE && aindex == 0)
1184 t = rtx_tp, subname = "rt_rtx";
1185 else
1187 error_at_line
1188 (&lexer_line,
1189 "rtx type `%s' has `0' in position %lu, can't handle",
1190 rtx_name[i], (unsigned long) aindex);
1191 t = &string_type;
1192 subname = "rt_int";
1194 break;
1196 case 's':
1197 case 'S':
1198 case 'T':
1199 t = &string_type;
1200 subname = "rt_str";
1201 break;
1203 case 'e':
1204 case 'u':
1205 t = rtx_tp;
1206 subname = "rt_rtx";
1207 break;
1209 case 'E':
1210 case 'V':
1211 t = rtvec_tp;
1212 subname = "rt_rtvec";
1213 break;
1215 case 't':
1216 t = tree_tp;
1217 subname = "rt_tree";
1218 break;
1220 case 'B':
1221 t = basic_block_tp;
1222 subname = "rt_bb";
1223 break;
1225 default:
1226 error_at_line
1227 (&lexer_line,
1228 "rtx type `%s' has `%c' in position %lu, can't handle",
1229 rtx_name[i], rtx_format[i][aindex],
1230 (unsigned long) aindex);
1231 t = &string_type;
1232 subname = "rt_int";
1233 break;
1236 subfields = create_field (subfields, t,
1237 xasprintf (".fld[%lu].%s",
1238 (unsigned long) aindex,
1239 subname));
1240 subfields->opt = nodot;
1241 if (t == note_union_tp)
1242 subfields->opt =
1243 create_string_option (subfields->opt, "desc",
1244 "NOTE_KIND (&%0)");
1245 if (t == symbol_union_tp)
1246 subfields->opt =
1247 create_string_option (subfields->opt, "desc",
1248 "CONSTANT_POOL_ADDRESS_P (&%0)");
1251 if (i == SYMBOL_REF)
1253 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1254 holds. */
1255 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1256 subfields
1257 = create_optional_field (subfields, field_tp, "block_sym",
1258 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1261 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1262 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1263 NULL);
1265 ftag = xstrdup (rtx_name[i]);
1266 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1267 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1268 flds = create_field (flds, substruct, "");
1269 flds->opt = create_string_option (nodot, "tag", ftag);
1271 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1272 nodot);
1275 /* Handle `special("tree_exp")'. This is a special case for
1276 field `operands' of struct tree_exp, which although it claims to contain
1277 pointers to trees, actually sometimes contains pointers to RTL too.
1278 Passed T, the old type of the field, and OPT its options. Returns
1279 a new type for the field. */
1281 static type_p
1282 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1284 pair_p flds;
1285 options_p nodot;
1287 if (t->kind != TYPE_ARRAY)
1289 error_at_line (&lexer_line,
1290 "special `tree_exp' must be applied to an array");
1291 return &string_type;
1294 nodot = create_string_option (NULL, "dot", "");
1296 flds = create_field (NULL, t, "");
1297 flds->opt = create_string_option (nodot, "length",
1298 "TREE_OPERAND_LENGTH ((tree) &%0)");
1299 flds->opt = create_string_option (flds->opt, "default", "");
1301 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1302 nodot);
1305 /* Perform any special processing on a type T, about to become the type
1306 of a field. Return the appropriate type for the field.
1307 At present:
1308 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1309 - Similarly for arrays of pointer-to-char;
1310 - Converts structures for which a parameter is provided to
1311 TYPE_PARAM_STRUCT;
1312 - Handles "special" options.
1315 type_p
1316 adjust_field_type (type_p t, options_p opt)
1318 int length_p = 0;
1319 const int pointer_p = t->kind == TYPE_POINTER;
1320 type_p params[NUM_PARAM];
1321 int params_p = 0;
1322 int i;
1324 for (i = 0; i < NUM_PARAM; i++)
1325 params[i] = NULL;
1327 for (; opt; opt = opt->next)
1328 if (strcmp (opt->name, "length") == 0)
1330 if (length_p)
1331 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1332 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1334 error_at_line (&lexer_line,
1335 "option `%s' may not be applied to "
1336 "arrays of atomic types", opt->name);
1338 length_p = 1;
1340 else if ((strcmp (opt->name, "param_is") == 0
1341 || (strncmp (opt->name, "param", 5) == 0
1342 && ISDIGIT (opt->name[5])
1343 && strcmp (opt->name + 6, "_is") == 0))
1344 && opt->kind == OPTION_TYPE)
1346 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1348 if (!union_or_struct_p (t)
1349 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1351 error_at_line (&lexer_line,
1352 "option `%s' may only be applied to structures or structure pointers",
1353 opt->name);
1354 return t;
1357 params_p = 1;
1358 if (params[num] != NULL)
1359 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1360 if (!ISDIGIT (opt->name[5]))
1361 params[num] = create_pointer (opt->info.type);
1362 else
1363 params[num] = opt->info.type;
1365 else if (strcmp (opt->name, "special") == 0
1366 && opt->kind == OPTION_STRING)
1368 const char *special_name = opt->info.string;
1369 if (strcmp (special_name, "tree_exp") == 0)
1370 t = adjust_field_tree_exp (t, opt);
1371 else if (strcmp (special_name, "rtx_def") == 0)
1372 t = adjust_field_rtx_def (t, opt);
1373 else
1374 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1377 if (params_p)
1379 type_p realt;
1381 if (pointer_p)
1382 t = t->u.p;
1383 realt = find_param_structure (t, params);
1384 t = pointer_p ? create_pointer (realt) : realt;
1387 if (!length_p
1388 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1389 return &string_type;
1390 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1391 && t->u.a.p->u.p->kind == TYPE_SCALAR
1392 && t->u.a.p->u.p->u.scalar_is_char)
1393 return create_array (&string_type, t->u.a.len);
1395 return t;
1399 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *);
1400 static void set_gc_used (pair_p);
1402 /* Handle OPT for set_gc_used_type. */
1404 static void
1405 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1406 int *pass_param, int *length, int *skip,
1407 type_p *nested_ptr)
1409 options_p o;
1410 for (o = opt; o; o = o->next)
1411 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1412 && o->kind == OPTION_TYPE)
1413 set_gc_used_type (o->info.type,
1414 GC_POINTED_TO, NULL);
1415 else if (strcmp (o->name, "maybe_undef") == 0)
1416 *maybe_undef = 1;
1417 else if (strcmp (o->name, "use_params") == 0)
1418 *pass_param = 1;
1419 else if (strcmp (o->name, "length") == 0)
1420 *length = 1;
1421 else if (strcmp (o->name, "skip") == 0)
1422 *skip = 1;
1423 else if (strcmp (o->name, "nested_ptr") == 0
1424 && o->kind == OPTION_NESTED)
1425 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1429 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1430 static void
1431 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM])
1433 if (t->gc_used >= level)
1434 return;
1436 t->gc_used = level;
1438 switch (t->kind)
1440 case TYPE_STRUCT:
1441 case TYPE_UNION:
1442 case TYPE_USER_STRUCT:
1444 pair_p f;
1445 int dummy;
1446 type_p dummy2;
1448 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1449 &dummy2);
1451 for (f = t->u.s.fields; f; f = f->next)
1453 int maybe_undef = 0;
1454 int pass_param = 0;
1455 int length = 0;
1456 int skip = 0;
1457 type_p nested_ptr = NULL;
1458 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1459 &length, &skip, &nested_ptr);
1461 if (nested_ptr && f->type->kind == TYPE_POINTER)
1462 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1463 pass_param ? param : NULL);
1464 else if (length && f->type->kind == TYPE_POINTER)
1465 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1466 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1467 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1468 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1469 set_gc_used_type (find_param_structure (f->type->u.p, param),
1470 GC_POINTED_TO, NULL);
1471 else if (skip)
1472 ; /* target type is not used through this field */
1473 else
1474 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL);
1476 break;
1479 case TYPE_POINTER:
1480 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1481 break;
1483 case TYPE_ARRAY:
1484 set_gc_used_type (t->u.a.p, GC_USED, param);
1485 break;
1487 case TYPE_LANG_STRUCT:
1488 for (t = t->u.s.lang_struct; t; t = t->next)
1489 set_gc_used_type (t, level, param);
1490 break;
1492 case TYPE_PARAM_STRUCT:
1494 int i;
1495 for (i = 0; i < NUM_PARAM; i++)
1496 if (t->u.param_struct.param[i] != 0)
1497 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1499 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1500 level = GC_POINTED_TO;
1501 else
1502 level = GC_USED;
1503 t->u.param_struct.stru->gc_used = GC_UNUSED;
1504 set_gc_used_type (t->u.param_struct.stru, level,
1505 t->u.param_struct.param);
1506 break;
1508 default:
1509 break;
1513 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1515 static void
1516 set_gc_used (pair_p variables)
1518 int nbvars = 0;
1519 pair_p p;
1520 for (p = variables; p; p = p->next)
1522 set_gc_used_type (p->type, GC_USED, NULL);
1523 nbvars++;
1525 if (verbosity_level >= 2)
1526 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1529 /* File mapping routines. For each input file, there is one output .c file
1530 (but some output files have many input files), and there is one .h file
1531 for the whole build. */
1533 /* Output file handling. */
1535 /* Create and return an outf_p for a new file for NAME, to be called
1536 ONAME. */
1538 static outf_p
1539 create_file (const char *name, const char *oname)
1541 static const char *const hdr[] = {
1542 " Copyright (C) 2004, 2007, 2009, 2012 Free Software Foundation, Inc.\n",
1543 "\n",
1544 "This file is part of GCC.\n",
1545 "\n",
1546 "GCC is free software; you can redistribute it and/or modify it under\n",
1547 "the terms of the GNU General Public License as published by the Free\n",
1548 "Software Foundation; either version 3, or (at your option) any later\n",
1549 "version.\n",
1550 "\n",
1551 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1552 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1553 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1554 "for more details.\n",
1555 "\n",
1556 "You should have received a copy of the GNU General Public License\n",
1557 "along with GCC; see the file COPYING3. If not see\n",
1558 "<http://www.gnu.org/licenses/>. */\n",
1559 "\n",
1560 "/* This file is machine generated. Do not edit. */\n"
1562 outf_p f;
1563 size_t i;
1565 gcc_assert (name != NULL);
1566 gcc_assert (oname != NULL);
1567 f = XCNEW (struct outf);
1568 f->next = output_files;
1569 f->name = oname;
1570 output_files = f;
1572 oprintf (f, "/* Type information for %s.\n", name);
1573 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1574 oprintf (f, "%s", hdr[i]);
1575 return f;
1578 /* Print, like fprintf, to O.
1579 N.B. You might think this could be implemented more efficiently
1580 with vsnprintf(). Unfortunately, there are C libraries that
1581 provide that function but without the C99 semantics for its return
1582 value, making it impossible to know how much space is required. */
1583 void
1584 oprintf (outf_p o, const char *format, ...)
1586 char *s;
1587 size_t slength;
1588 va_list ap;
1590 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1591 in that case. */
1592 if (!o)
1593 return;
1595 va_start (ap, format);
1596 slength = vasprintf (&s, format, ap);
1597 if (s == NULL || (int) slength < 0)
1598 fatal ("out of memory");
1599 va_end (ap);
1601 if (o->bufused + slength > o->buflength)
1603 size_t new_len = o->buflength;
1604 if (new_len == 0)
1605 new_len = 1024;
1608 new_len *= 2;
1610 while (o->bufused + slength >= new_len);
1611 o->buf = XRESIZEVEC (char, o->buf, new_len);
1612 o->buflength = new_len;
1614 memcpy (o->buf + o->bufused, s, slength);
1615 o->bufused += slength;
1616 free (s);
1619 /* Open the global header file and the language-specific header files. */
1621 static void
1622 open_base_files (void)
1624 size_t i;
1626 if (nb_plugin_files > 0 && plugin_files)
1627 return;
1629 header_file = create_file ("GCC", "gtype-desc.h");
1631 base_files = XNEWVEC (outf_p, num_lang_dirs);
1633 for (i = 0; i < num_lang_dirs; i++)
1634 base_files[i] = create_file (lang_dir_names[i],
1635 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1637 /* gtype-desc.c is a little special, so we create it here. */
1639 /* The order of files here matters very much. */
1640 static const char *const ifiles[] = {
1641 "config.h", "system.h", "coretypes.h", "tm.h",
1642 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1643 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1644 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1645 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1646 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1647 "except.h", "output.h", "gimple.h", "cfgloop.h",
1648 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1649 "ipa-inline.h", "dwarf2out.h", NULL
1651 const char *const *ifp;
1652 outf_p gtype_desc_c;
1654 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1655 for (ifp = ifiles; *ifp; ifp++)
1656 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1658 /* Make sure we handle "cfun" specially. */
1659 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1660 oprintf (gtype_desc_c, "#undef cfun\n");
1664 /* For INPF an input file, return the real basename of INPF, with all
1665 the directory components skipped. */
1667 static const char *
1668 get_file_realbasename (const input_file *inpf)
1670 return lbasename (get_input_file_name (inpf));
1673 /* For INPF a filename, return the relative path to INPF from
1674 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1676 const char *
1677 get_file_srcdir_relative_path (const input_file *inpf)
1679 const char *f = get_input_file_name (inpf);
1680 if (strlen (f) > srcdir_len
1681 && IS_DIR_SEPARATOR (f[srcdir_len])
1682 && strncmp (f, srcdir, srcdir_len) == 0)
1683 return f + srcdir_len + 1;
1684 else
1685 return NULL;
1688 /* For INPF an input_file, return the relative path to INPF from
1689 $(srcdir) if the latter is a prefix in INPF, or the real basename
1690 of INPF otherwise. */
1692 static const char *
1693 get_file_basename (const input_file *inpf)
1695 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1697 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1700 /* For F a filename, return the lang_dir_names relative index of the language
1701 directory that is a prefix in F, if any, -1 otherwise. */
1703 static int
1704 get_prefix_langdir_index (const char *f)
1706 size_t f_len = strlen (f);
1707 size_t lang_index;
1709 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1711 const char *langdir = lang_dir_names[lang_index];
1712 size_t langdir_len = strlen (langdir);
1714 if (f_len > langdir_len
1715 && IS_DIR_SEPARATOR (f[langdir_len])
1716 && memcmp (f, langdir, langdir_len) == 0)
1717 return lang_index;
1720 return -1;
1723 /* For INPF an input file, return the name of language directory where
1724 F is located, if any, NULL otherwise. */
1726 static const char *
1727 get_file_langdir (const input_file *inpf)
1729 /* Get the relative path to INPF from $(srcdir) and find the
1730 language by comparing the prefix with language directory names.
1731 If INPF is not even srcdir relative, no point in looking
1732 further. */
1734 int lang_index;
1735 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1736 const char *r;
1738 if (!srcdir_relative_path)
1739 return NULL;
1741 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1742 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1743 r = "c-family";
1744 else if (lang_index >= 0)
1745 r = lang_dir_names[lang_index];
1746 else
1747 r = NULL;
1749 return r;
1752 /* The gt- output file name for INPF. */
1754 static const char *
1755 get_file_gtfilename (const input_file *inpf)
1757 /* Cook up an initial version of the gt- file name from the file real
1758 basename and the language name, if any. */
1760 const char *basename = get_file_realbasename (inpf);
1761 const char *langdir = get_file_langdir (inpf);
1763 char *result =
1764 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1765 : xasprintf ("gt-%s", basename));
1767 /* Then replace all non alphanumerics characters by '-' and change the
1768 extension to ".h". We expect the input filename extension was at least
1769 one character long. */
1771 char *s = result;
1773 for (; *s != '.'; s++)
1774 if (!ISALNUM (*s) && *s != '-')
1775 *s = '-';
1777 memcpy (s, ".h", sizeof (".h"));
1779 return result;
1782 /* Each input_file has its associated output file outf_p. The
1783 association is computed by the function
1784 get_output_file_with_visibility. The associated file is cached
1785 inside input_file in its inpoutf field, so is really computed only
1786 once. Associated output file paths (i.e. output_name-s) are
1787 computed by a rule based regexp machinery, using the files_rules
1788 array of struct file_rule_st. A for_name is also computed, giving
1789 the source file name for which the output_file is generated; it is
1790 often the last component of the input_file path. */
1794 Regexpr machinery to compute the output_name and for_name-s of each
1795 input_file. We have a sequence of file rules which gives the POSIX
1796 extended regular expression to match an input file path, and two
1797 transformed strings for the corresponding output_name and the
1798 corresponding for_name. The transformed string contain dollars: $0
1799 is replaced by the entire match, $1 is replaced by the substring
1800 matching the first parenthesis in the regexp, etc. And $$ is replaced
1801 by a single verbatim dollar. The rule order is important. The
1802 general case is last, and the particular cases should come before.
1803 An action routine can, when needed, update the out_name & for_name
1804 and/or return the appropriate output file. It is invoked only when a
1805 rule is triggered. When a rule is triggered, the output_name and
1806 for_name are computed using their transform string in while $$, $0,
1807 $1, ... are suitably replaced. If there is an action, it is called.
1808 In some few cases, the action can directly return the outf_p, but
1809 usually it just updates the output_name and for_name so should free
1810 them before replacing them. The get_output_file_with_visibility
1811 function creates an outf_p only once per each output_name, so it
1812 scans the output_files list for previously seen output file names.
1815 /* Signature of actions in file rules. */
1816 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1819 struct file_rule_st {
1820 const char* frul_srcexpr; /* Source string for regexp. */
1821 int frul_rflags; /* Flags passed to regcomp, usually
1822 * REG_EXTENDED. */
1823 regex_t* frul_re; /* Compiled regular expression
1824 obtained by regcomp. */
1825 const char* frul_tr_out; /* Transformation string for making
1826 * the output_name, with $1 ... $9 for
1827 * subpatterns and $0 for the whole
1828 * matched filename. */
1829 const char* frul_tr_for; /* Tranformation string for making the
1830 for_name. */
1831 frul_actionrout_t* frul_action; /* The action, if non null, is
1832 * called once the rule matches, on
1833 * the transformed out_name &
1834 * for_name. It could change them
1835 * and/or give the output file. */
1838 /* File rule action handling *.h files. */
1839 static outf_p header_dot_h_frul (input_file*, char**, char**);
1841 /* File rule action handling *.c files. */
1842 static outf_p source_dot_c_frul (input_file*, char**, char**);
1844 #define NULL_REGEX (regex_t*)0
1846 /* The prefix in our regexp-s matching the directory. */
1847 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1849 #define NULL_FRULACT (frul_actionrout_t*)0
1851 /* The array of our rules governing file name generation. Rules order
1852 matters, so change with extreme care! */
1854 struct file_rule_st files_rules[] = {
1855 /* The general rule assumes that files in subdirectories belong to a
1856 particular front-end, and files not in subdirectories are shared.
1857 The following rules deal with exceptions - files that are in
1858 subdirectories and yet are shared, and files that are top-level,
1859 but are not shared. */
1861 /* the c-family/ source directory is special. */
1862 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1863 REG_EXTENDED, NULL_REGEX,
1864 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1866 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1867 REG_EXTENDED, NULL_REGEX,
1868 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1870 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1871 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1872 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1874 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1875 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1877 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1878 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1879 REG_EXTENDED, NULL_REGEX,
1880 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1882 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1883 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1884 REG_EXTENDED, NULL_REGEX,
1885 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1887 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1888 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1889 REG_EXTENDED, NULL_REGEX,
1890 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1892 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1893 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1894 REG_EXTENDED, NULL_REGEX,
1895 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1897 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1898 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1899 REG_EXTENDED, NULL_REGEX,
1900 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1902 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1903 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1904 REG_EXTENDED, NULL_REGEX,
1905 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
1907 /* General cases. For header *.h and source *.c files, we need
1908 * special actions to handle the language. */
1910 /* Source *.c files are using get_file_gtfilename to compute their
1911 output_name and get_file_basename to compute their for_name
1912 through the source_dot_c_frul action. */
1913 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
1914 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
1915 /* Common header files get "gtype-desc.c" as their output_name,
1916 * while language specific header files are handled specially. So
1917 * we need the header_dot_h_frul action. */
1918 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
1919 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
1921 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
1922 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
1924 /* Mandatory null last entry signaling end of rules. */
1925 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
1928 /* Special file rules action for handling *.h header files. It gives
1929 "gtype-desc.c" for common headers and corresponding output
1930 files for language-specific header files. */
1931 static outf_p
1932 header_dot_h_frul (input_file* inpf, char**poutname,
1933 char**pforname ATTRIBUTE_UNUSED)
1935 const char *basename = 0;
1936 int lang_index = 0;
1937 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
1938 (void*) inpf, get_input_file_name (inpf),
1939 *poutname, *pforname);
1940 basename = get_file_basename (inpf);
1941 lang_index = get_prefix_langdir_index (basename);
1942 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
1944 if (lang_index >= 0)
1946 /* The header is language specific. Given output_name &
1947 for_name remains unchanged. The base_files array gives the
1948 outf_p. */
1949 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
1950 (void*) base_files[lang_index],
1951 (base_files[lang_index])->name);
1952 return base_files[lang_index];
1954 else
1956 /* The header is common to all front-end languages. So
1957 output_name is "gtype-desc.c" file. The calling function
1958 get_output_file_with_visibility will find its outf_p. */
1959 free (*poutname);
1960 *poutname = xstrdup ("gtype-desc.c");
1961 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
1962 get_input_file_name (inpf));
1963 return NULL;
1968 /* Special file rules action for handling *.c source files using
1969 * get_file_gtfilename to compute their output_name and
1970 * get_file_basename to compute their for_name. The output_name is
1971 * gt-<LANG>-<BASE>.h for language specific source files, and
1972 * gt-<BASE>.h for common source files. */
1973 static outf_p
1974 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
1976 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
1977 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
1978 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
1979 (void*) inpf, get_input_file_name (inpf),
1980 *poutname, *pforname);
1981 DBGPRINTF ("newoutname %s", newoutname);
1982 DBGPRINTF ("newbasename %s", newbasename);
1983 free (*poutname);
1984 free (*pforname);
1985 *poutname = newoutname;
1986 *pforname = newbasename;
1987 return NULL;
1990 /* Utility function for get_output_file_with_visibility which returns
1991 * a malloc-ed substituted string using TRS on matching of the FILNAM
1992 * file name, using the PMATCH array. */
1993 static char*
1994 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
1995 const char *trs)
1997 struct obstack str_obstack;
1998 char *str = NULL;
1999 char *rawstr = NULL;
2000 const char *pt = NULL;
2001 DBGPRINTF ("filnam %s", filnam);
2002 obstack_init (&str_obstack);
2003 for (pt = trs; *pt; pt++) {
2004 char c = *pt;
2005 if (c == '$')
2007 if (pt[1] == '$')
2009 /* A double dollar $$ is substituted by a single verbatim
2010 dollar, but who really uses dollar signs in file
2011 paths? */
2012 obstack_1grow (&str_obstack, '$');
2014 else if (ISDIGIT (pt[1]))
2016 /* Handle $0 $1 ... $9 by appropriate substitution. */
2017 int dolnum = pt[1] - '0';
2018 int so = pmatch[dolnum].rm_so;
2019 int eo = pmatch[dolnum].rm_eo;
2020 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2021 if (so>=0 && eo>=so)
2022 obstack_grow (&str_obstack, filnam + so, eo - so);
2024 else
2026 /* This can happen only when files_rules is buggy! */
2027 gcc_unreachable();
2029 /* Always skip the character after the dollar. */
2030 pt++;
2032 else
2033 obstack_1grow (&str_obstack, c);
2035 obstack_1grow (&str_obstack, '\0');
2036 rawstr = XOBFINISH (&str_obstack, char *);
2037 str = xstrdup (rawstr);
2038 obstack_free (&str_obstack, NULL);
2039 DBGPRINTF ("matched replacement %s", str);
2040 rawstr = NULL;
2041 return str;
2045 /* An output file, suitable for definitions, that can see declarations
2046 made in INPF and is linked into every language that uses INPF.
2047 Since the result is cached inside INPF, that argument cannot be
2048 declared constant, but is "almost" constant. */
2050 outf_p
2051 get_output_file_with_visibility (input_file *inpf)
2053 outf_p r;
2054 char *for_name = NULL;
2055 char *output_name = NULL;
2056 const char* inpfname;
2058 /* This can happen when we need a file with visibility on a
2059 structure that we've never seen. We have to just hope that it's
2060 globally visible. */
2061 if (inpf == NULL)
2062 inpf = system_h_file;
2064 /* The result is cached in INPF, so return it if already known. */
2065 if (inpf->inpoutf)
2066 return inpf->inpoutf;
2068 /* In plugin mode, return NULL unless the input_file is one of the
2069 plugin_files. */
2070 if (plugin_files)
2072 size_t i;
2073 for (i = 0; i < nb_plugin_files; i++)
2074 if (inpf == plugin_files[i])
2076 inpf->inpoutf = plugin_output;
2077 return plugin_output;
2080 return NULL;
2083 inpfname = get_input_file_name (inpf);
2085 /* Try each rule in sequence in files_rules until one is triggered. */
2087 int rulix = 0;
2088 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2089 (void*) inpf, inpfname);
2091 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2093 DBGPRINTF ("rulix#%d srcexpr %s",
2094 rulix, files_rules[rulix].frul_srcexpr);
2096 if (!files_rules[rulix].frul_re)
2098 /* Compile the regexpr lazily. */
2099 int err = 0;
2100 files_rules[rulix].frul_re = XCNEW (regex_t);
2101 err = regcomp (files_rules[rulix].frul_re,
2102 files_rules[rulix].frul_srcexpr,
2103 files_rules[rulix].frul_rflags);
2104 if (err)
2106 /* The regular expression compilation fails only when
2107 file_rules is buggy. */
2108 gcc_unreachable ();
2112 output_name = NULL;
2113 for_name = NULL;
2115 /* Match the regexpr and trigger the rule if matched. */
2117 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2118 $3, ... $9. */
2119 regmatch_t pmatch[10];
2120 memset (pmatch, 0, sizeof (pmatch));
2121 if (!regexec (files_rules[rulix].frul_re,
2122 inpfname, 10, pmatch, 0))
2124 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2125 (void*) inpf, inpfname, rulix,
2126 files_rules[rulix].frul_srcexpr);
2127 for_name =
2128 matching_file_name_substitute (inpfname, pmatch,
2129 files_rules[rulix].frul_tr_for);
2130 DBGPRINTF ("for_name %s", for_name);
2131 output_name =
2132 matching_file_name_substitute (inpfname, pmatch,
2133 files_rules[rulix].frul_tr_out);
2134 DBGPRINTF ("output_name %s", output_name);
2135 if (files_rules[rulix].frul_action)
2137 /* Invoke our action routine. */
2138 outf_p of = NULL;
2139 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2140 rulix, output_name, for_name);
2141 of =
2142 (files_rules[rulix].frul_action) (inpf,
2143 &output_name, &for_name);
2144 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2145 rulix, (void*)of, output_name, for_name);
2146 /* If the action routine returned something, give it back
2147 immediately and cache it in inpf. */
2148 if (of)
2150 inpf->inpoutf = of;
2151 return of;
2154 /* The rule matched, and had no action, or that action did
2155 not return any output file but could have changed the
2156 output_name or for_name. We break out of the loop on the
2157 files_rules. */
2158 break;
2160 else
2162 /* The regexpr did not match. */
2163 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2164 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2165 continue;
2170 if (!output_name || !for_name)
2172 /* This is impossible, and could only happen if the files_rules is
2173 incomplete or buggy. */
2174 gcc_unreachable ();
2177 /* Look through to see if we've ever seen this output filename
2178 before. If found, cache the result in inpf. */
2179 for (r = output_files; r; r = r->next)
2180 if (filename_cmp (r->name, output_name) == 0)
2182 inpf->inpoutf = r;
2183 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2184 output_name, for_name);
2185 return r;
2188 /* If not found, create it, and cache it in inpf. */
2189 r = create_file (for_name, output_name);
2191 gcc_assert (r && r->name);
2192 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2193 output_name, for_name);
2194 inpf->inpoutf = r;
2195 return r;
2200 /* The name of an output file, suitable for definitions, that can see
2201 declarations made in INPF and is linked into every language that
2202 uses INPF. */
2204 const char *
2205 get_output_file_name (input_file* inpf)
2207 outf_p o = get_output_file_with_visibility (inpf);
2208 if (o)
2209 return o->name;
2210 return NULL;
2213 /* Check if existing file is equal to the in memory buffer. */
2215 static bool
2216 is_file_equal (outf_p of)
2218 FILE *newfile = fopen (of->name, "r");
2219 size_t i;
2220 bool equal;
2221 if (newfile == NULL)
2222 return false;
2224 equal = true;
2225 for (i = 0; i < of->bufused; i++)
2227 int ch;
2228 ch = fgetc (newfile);
2229 if (ch == EOF || ch != (unsigned char) of->buf[i])
2231 equal = false;
2232 break;
2235 fclose (newfile);
2236 return equal;
2239 /* Copy the output to its final destination,
2240 but don't unnecessarily change modification times. */
2242 static void
2243 close_output_files (void)
2245 int nbwrittenfiles = 0;
2246 outf_p of;
2248 for (of = output_files; of; of = of->next)
2250 if (!is_file_equal (of))
2252 FILE *newfile = NULL;
2253 char *backupname = NULL;
2254 /* Back up the old version of the output file gt-FOO.c as
2255 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2256 if (backup_dir)
2258 backupname = concat (backup_dir, "/",
2259 lbasename (of->name), "~", NULL);
2260 if (!access (of->name, F_OK) && rename (of->name, backupname))
2261 fatal ("failed to back up %s as %s: %s",
2262 of->name, backupname, xstrerror (errno));
2265 newfile = fopen (of->name, "w");
2266 if (newfile == NULL)
2267 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2268 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2269 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2270 if (fclose (newfile) != 0)
2271 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2272 nbwrittenfiles++;
2273 if (verbosity_level >= 2 && backupname)
2274 printf ("%s wrote #%-3d %s backed-up in %s\n",
2275 progname, nbwrittenfiles, of->name, backupname);
2276 else if (verbosity_level >= 1)
2277 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2278 free (backupname);
2280 else
2282 /* output file remains unchanged. */
2283 if (verbosity_level >= 2)
2284 printf ("%s keep %s\n", progname, of->name);
2286 free (of->buf);
2287 of->buf = NULL;
2288 of->bufused = of->buflength = 0;
2290 if (verbosity_level >= 1)
2291 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2294 struct flist
2296 struct flist *next;
2297 int started_p;
2298 const input_file* file;
2299 outf_p f;
2302 struct walk_type_data;
2304 /* For scalars and strings, given the item in 'val'.
2305 For structures, given a pointer to the item in 'val'.
2306 For misc. pointers, given the item in 'val'.
2308 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2309 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2311 /* Parameters for write_types. */
2313 struct write_types_data
2315 const char *prefix;
2316 const char *param_prefix;
2317 const char *subfield_marker_routine;
2318 const char *marker_routine;
2319 const char *reorder_note_routine;
2320 const char *comment;
2321 int skip_hooks; /* skip hook generation if non zero */
2324 static void output_escaped_param (struct walk_type_data *d,
2325 const char *, const char *);
2326 static void output_mangled_typename (outf_p, const_type_p);
2327 static void walk_type (type_p t, struct walk_type_data *d);
2328 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2329 const struct write_types_data *wtd);
2330 static void write_types_process_field
2331 (type_p f, const struct walk_type_data *d);
2332 static void write_types (outf_p output_header,
2333 type_p structures,
2334 type_p param_structs,
2335 const struct write_types_data *wtd);
2336 static void write_types_local_process_field
2337 (type_p f, const struct walk_type_data *d);
2338 static void write_local_func_for_structure
2339 (const_type_p orig_s, type_p s, type_p *param);
2340 static void write_local (outf_p output_header,
2341 type_p structures, type_p param_structs);
2342 static void write_enum_defn (type_p structures, type_p param_structs);
2343 static int contains_scalar_p (type_p t);
2344 static void put_mangled_filename (outf_p, const input_file *);
2345 static void finish_root_table (struct flist *flp, const char *pfx,
2346 const char *tname, const char *lastname,
2347 const char *name);
2348 static void write_root (outf_p, pair_p, type_p, const char *, int,
2349 struct fileloc *, const char *, bool);
2350 static void write_array (outf_p f, pair_p v,
2351 const struct write_types_data *wtd);
2352 static void write_roots (pair_p, bool);
2354 /* Parameters for walk_type. */
2356 struct walk_type_data
2358 process_field_fn process_field;
2359 const void *cookie;
2360 outf_p of;
2361 options_p opt;
2362 const char *val;
2363 const char *prev_val[4];
2364 int indent;
2365 int counter;
2366 const struct fileloc *line;
2367 lang_bitmap bitmap;
2368 type_p *param;
2369 int used_length;
2370 type_p orig_s;
2371 const char *reorder_fn;
2372 bool needs_cast_p;
2373 bool fn_wants_lvalue;
2374 bool in_record_p;
2375 int loopcounter;
2376 bool in_ptr_field;
2377 bool have_this_obj;
2381 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2382 pre-processor identifier to use in a #define directive. This replaces
2383 special characters used in C++ identifiers like '>', '<' and ':' with
2384 '_'.
2386 If no C++ special characters are found in TYPE_NAME, return
2387 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2388 characters replaced with '_'. In this case, the caller is
2389 responsible for freeing the allocated string. */
2391 static const char *
2392 filter_type_name (const char *type_name)
2394 if (strchr (type_name, '<') || strchr (type_name, ':'))
2396 size_t i;
2397 char *s = xstrdup (type_name);
2398 for (i = 0; i < strlen (s); i++)
2399 if (s[i] == '<' || s[i] == '>' || s[i] == ':')
2400 s[i] = '_';
2401 return s;
2403 else
2404 return type_name;
2408 /* Print a mangled name representing T to OF. */
2410 static void
2411 output_mangled_typename (outf_p of, const_type_p t)
2413 if (t == NULL)
2414 oprintf (of, "Z");
2415 else
2416 switch (t->kind)
2418 case TYPE_NONE:
2419 gcc_unreachable ();
2420 break;
2421 case TYPE_POINTER:
2422 oprintf (of, "P");
2423 output_mangled_typename (of, t->u.p);
2424 break;
2425 case TYPE_SCALAR:
2426 oprintf (of, "I");
2427 break;
2428 case TYPE_STRING:
2429 oprintf (of, "S");
2430 break;
2431 case TYPE_STRUCT:
2432 case TYPE_UNION:
2433 case TYPE_LANG_STRUCT:
2434 case TYPE_USER_STRUCT:
2436 const char *id_for_tag = filter_type_name (t->u.s.tag);
2437 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2438 id_for_tag);
2439 if (id_for_tag != t->u.s.tag)
2440 free (CONST_CAST(char *, id_for_tag));
2442 break;
2443 case TYPE_PARAM_STRUCT:
2445 int i;
2446 for (i = 0; i < NUM_PARAM; i++)
2447 if (t->u.param_struct.param[i] != NULL)
2448 output_mangled_typename (of, t->u.param_struct.param[i]);
2449 output_mangled_typename (of, t->u.param_struct.stru);
2451 break;
2452 case TYPE_ARRAY:
2453 gcc_unreachable ();
2457 /* Print PARAM to D->OF processing escapes. D->VAL references the
2458 current object, D->PREV_VAL the object containing the current
2459 object, ONAME is the name of the option and D->LINE is used to
2460 print error messages. */
2462 static void
2463 output_escaped_param (struct walk_type_data *d, const char *param,
2464 const char *oname)
2466 const char *p;
2468 for (p = param; *p; p++)
2469 if (*p != '%')
2470 oprintf (d->of, "%c", *p);
2471 else
2472 switch (*++p)
2474 case 'h':
2475 oprintf (d->of, "(%s)", d->prev_val[2]);
2476 break;
2477 case '0':
2478 oprintf (d->of, "(%s)", d->prev_val[0]);
2479 break;
2480 case '1':
2481 oprintf (d->of, "(%s)", d->prev_val[1]);
2482 break;
2483 case 'a':
2485 const char *pp = d->val + strlen (d->val);
2486 while (pp[-1] == ']')
2487 while (*pp != '[')
2488 pp--;
2489 oprintf (d->of, "%s", pp);
2491 break;
2492 default:
2493 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2494 oname, '%', *p);
2499 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2500 which is of type T. Write code to D->OF to constrain execution (at
2501 the point that D->PROCESS_FIELD is called) to the appropriate
2502 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2503 pointers to those objects. D->PREV_VAL lists the objects
2504 containing the current object, D->OPT is a list of options to
2505 apply, D->INDENT is the current indentation level, D->LINE is used
2506 to print error messages, D->BITMAP indicates which languages to
2507 print the structure for, and D->PARAM is the current parameter
2508 (from an enclosing param_is option). */
2510 static void
2511 walk_type (type_p t, struct walk_type_data *d)
2513 const char *length = NULL;
2514 const char *desc = NULL;
2515 int maybe_undef_p = 0;
2516 int use_param_num = -1;
2517 int use_params_p = 0;
2518 int atomic_p = 0;
2519 options_p oo;
2520 const struct nested_ptr_data *nested_ptr_d = NULL;
2522 d->needs_cast_p = false;
2523 for (oo = d->opt; oo; oo = oo->next)
2524 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2525 length = oo->info.string;
2526 else if (strcmp (oo->name, "maybe_undef") == 0)
2527 maybe_undef_p = 1;
2528 else if (strncmp (oo->name, "use_param", 9) == 0
2529 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2530 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2531 else if (strcmp (oo->name, "use_params") == 0)
2532 use_params_p = 1;
2533 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2534 desc = oo->info.string;
2535 else if (strcmp (oo->name, "mark_hook") == 0)
2537 else if (strcmp (oo->name, "nested_ptr") == 0
2538 && oo->kind == OPTION_NESTED)
2539 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2540 else if (strcmp (oo->name, "dot") == 0)
2542 else if (strcmp (oo->name, "tag") == 0)
2544 else if (strcmp (oo->name, "special") == 0)
2546 else if (strcmp (oo->name, "skip") == 0)
2548 else if (strcmp (oo->name, "atomic") == 0)
2549 atomic_p = 1;
2550 else if (strcmp (oo->name, "default") == 0)
2552 else if (strcmp (oo->name, "param_is") == 0)
2554 else if (strncmp (oo->name, "param", 5) == 0
2555 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2557 else if (strcmp (oo->name, "chain_next") == 0)
2559 else if (strcmp (oo->name, "chain_prev") == 0)
2561 else if (strcmp (oo->name, "chain_circular") == 0)
2563 else if (strcmp (oo->name, "reorder") == 0)
2565 else if (strcmp (oo->name, "variable_size") == 0)
2567 else
2568 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2570 if (d->used_length)
2571 length = NULL;
2573 if (use_params_p)
2575 int pointer_p = t->kind == TYPE_POINTER;
2577 if (pointer_p)
2578 t = t->u.p;
2579 if (!union_or_struct_p (t))
2580 error_at_line (d->line, "`use_params' option on unimplemented type");
2581 else
2582 t = find_param_structure (t, d->param);
2583 if (pointer_p)
2584 t = create_pointer (t);
2587 if (use_param_num != -1)
2589 if (d->param != NULL && d->param[use_param_num] != NULL)
2591 type_p nt = d->param[use_param_num];
2593 if (t->kind == TYPE_ARRAY)
2594 nt = create_array (nt, t->u.a.len);
2595 else if (length != NULL && t->kind == TYPE_POINTER)
2596 nt = create_pointer (nt);
2597 d->needs_cast_p = (t->kind != TYPE_POINTER
2598 && (nt->kind == TYPE_POINTER
2599 || nt->kind == TYPE_STRING));
2600 t = nt;
2602 else
2603 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2606 if (maybe_undef_p
2607 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2609 error_at_line (d->line,
2610 "field `%s' has invalid option `maybe_undef_p'\n",
2611 d->val);
2612 return;
2615 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2617 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2618 return;
2621 switch (t->kind)
2623 case TYPE_SCALAR:
2624 case TYPE_STRING:
2625 d->process_field (t, d);
2626 break;
2628 case TYPE_POINTER:
2630 d->in_ptr_field = true;
2631 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2633 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2634 break;
2637 /* If a pointer type is marked as "atomic", we process the
2638 field itself, but we don't walk the data that they point to.
2640 There are two main cases where we walk types: to mark
2641 pointers that are reachable, and to relocate pointers when
2642 writing a PCH file. In both cases, an atomic pointer is
2643 itself marked or relocated, but the memory that it points
2644 to is left untouched. In the case of PCH, that memory will
2645 be read/written unchanged to the PCH file. */
2646 if (atomic_p)
2648 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2649 d->indent += 2;
2650 d->process_field (t, d);
2651 d->indent -= 2;
2652 oprintf (d->of, "%*s}\n", d->indent, "");
2653 break;
2656 if (!length)
2658 if (!union_or_struct_p (t->u.p)
2659 && t->u.p->kind != TYPE_PARAM_STRUCT)
2661 error_at_line (d->line,
2662 "field `%s' is pointer to unimplemented type",
2663 d->val);
2664 break;
2667 if (nested_ptr_d)
2669 const char *oldprevval2 = d->prev_val[2];
2671 if (!union_or_struct_p (nested_ptr_d->type))
2673 error_at_line (d->line,
2674 "field `%s' has invalid "
2675 "option `nested_ptr'\n", d->val);
2676 return;
2679 d->prev_val[2] = d->val;
2680 oprintf (d->of, "%*s{\n", d->indent, "");
2681 d->indent += 2;
2682 d->val = xasprintf ("x%d", d->counter++);
2683 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2684 (nested_ptr_d->type->kind == TYPE_UNION
2685 ? "union" : "struct"),
2686 nested_ptr_d->type->u.s.tag,
2687 d->fn_wants_lvalue ? "" : "const ", d->val);
2688 oprintf (d->of, "%*s", d->indent + 2, "");
2689 output_escaped_param (d, nested_ptr_d->convert_from,
2690 "nested_ptr");
2691 oprintf (d->of, ";\n");
2693 d->process_field (nested_ptr_d->type, d);
2695 if (d->fn_wants_lvalue)
2697 oprintf (d->of, "%*s%s = ", d->indent, "",
2698 d->prev_val[2]);
2699 d->prev_val[2] = d->val;
2700 output_escaped_param (d, nested_ptr_d->convert_to,
2701 "nested_ptr");
2702 oprintf (d->of, ";\n");
2705 d->indent -= 2;
2706 oprintf (d->of, "%*s}\n", d->indent, "");
2707 d->val = d->prev_val[2];
2708 d->prev_val[2] = oldprevval2;
2710 else
2711 d->process_field (t->u.p, d);
2713 else
2715 int loopcounter = d->loopcounter;
2716 const char *oldval = d->val;
2717 const char *oldprevval3 = d->prev_val[3];
2718 char *newval;
2720 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2721 d->indent += 2;
2722 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2723 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2724 "", loopcounter, loopcounter);
2725 if (!d->in_record_p)
2726 output_escaped_param (d, length, "length");
2727 else
2728 oprintf (d->of, "l%d", loopcounter);
2729 if (d->have_this_obj)
2730 /* Try to unswitch loops (see PR53880). */
2731 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2732 oprintf (d->of, "); i%d++) {\n", loopcounter);
2733 d->indent += 2;
2734 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2735 d->used_length = 1;
2736 d->prev_val[3] = oldval;
2737 walk_type (t->u.p, d);
2738 free (newval);
2739 d->val = oldval;
2740 d->prev_val[3] = oldprevval3;
2741 d->used_length = 0;
2742 d->indent -= 2;
2743 oprintf (d->of, "%*s}\n", d->indent, "");
2744 d->process_field (t, d);
2745 d->indent -= 2;
2746 oprintf (d->of, "%*s}\n", d->indent, "");
2748 d->in_ptr_field = false;
2750 break;
2752 case TYPE_ARRAY:
2754 int loopcounter;
2755 const char *oldval = d->val;
2756 char *newval;
2758 /* If it's an array of scalars, we optimize by not generating
2759 any code. */
2760 if (t->u.a.p->kind == TYPE_SCALAR)
2761 break;
2763 if (length)
2764 loopcounter = d->loopcounter;
2765 else
2766 loopcounter = d->counter++;
2768 /* When walking an array, compute the length and store it in a
2769 local variable before walking the array elements, instead of
2770 recomputing the length expression each time through the loop.
2771 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2772 where the length is stored in the first array element,
2773 because otherwise that operand can get overwritten on the
2774 first iteration. */
2775 oprintf (d->of, "%*s{\n", d->indent, "");
2776 d->indent += 2;
2777 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2778 if (!d->in_record_p || !length)
2780 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2781 d->indent, "", loopcounter);
2782 if (length)
2783 output_escaped_param (d, length, "length");
2784 else
2785 oprintf (d->of, "%s", t->u.a.len);
2786 oprintf (d->of, ");\n");
2789 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2790 d->indent, "",
2791 loopcounter, loopcounter, loopcounter, loopcounter);
2792 d->indent += 2;
2793 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2794 d->used_length = 1;
2795 walk_type (t->u.a.p, d);
2796 free (newval);
2797 d->used_length = 0;
2798 d->val = oldval;
2799 d->indent -= 2;
2800 oprintf (d->of, "%*s}\n", d->indent, "");
2801 d->indent -= 2;
2802 oprintf (d->of, "%*s}\n", d->indent, "");
2804 break;
2806 case TYPE_STRUCT:
2807 case TYPE_UNION:
2809 pair_p f;
2810 const char *oldval = d->val;
2811 const char *oldprevval1 = d->prev_val[1];
2812 const char *oldprevval2 = d->prev_val[2];
2813 const int union_p = t->kind == TYPE_UNION;
2814 int seen_default_p = 0;
2815 options_p o;
2816 int lengths_seen = 0;
2817 int endcounter;
2818 bool any_length_seen = false;
2820 if (!t->u.s.line.file)
2821 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2823 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2825 error_at_line (d->line,
2826 "structure `%s' defined for mismatching languages",
2827 t->u.s.tag);
2828 error_at_line (&t->u.s.line, "one structure defined here");
2831 /* Some things may also be defined in the structure's options. */
2832 for (o = t->u.s.opt; o; o = o->next)
2833 if (!desc && strcmp (o->name, "desc") == 0
2834 && o->kind == OPTION_STRING)
2835 desc = o->info.string;
2837 d->prev_val[2] = oldval;
2838 d->prev_val[1] = oldprevval2;
2839 if (union_p)
2841 if (desc == NULL)
2843 error_at_line (d->line,
2844 "missing `desc' option for union `%s'",
2845 t->u.s.tag);
2846 desc = "1";
2848 oprintf (d->of, "%*sswitch (", d->indent, "");
2849 output_escaped_param (d, desc, "desc");
2850 oprintf (d->of, ")\n");
2851 d->indent += 2;
2852 oprintf (d->of, "%*s{\n", d->indent, "");
2855 for (f = t->u.s.fields; f; f = f->next)
2857 options_p oo;
2858 int skip_p = 0;
2859 const char *fieldlength = NULL;
2861 d->reorder_fn = NULL;
2862 for (oo = f->opt; oo; oo = oo->next)
2863 if (strcmp (oo->name, "skip") == 0)
2864 skip_p = 1;
2865 else if (strcmp (oo->name, "length") == 0
2866 && oo->kind == OPTION_STRING)
2867 fieldlength = oo->info.string;
2869 if (skip_p)
2870 continue;
2871 if (fieldlength)
2873 lengths_seen++;
2874 d->counter++;
2875 if (!union_p)
2877 if (!any_length_seen)
2879 oprintf (d->of, "%*s{\n", d->indent, "");
2880 d->indent += 2;
2882 any_length_seen = true;
2884 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2885 d->indent, "", d->counter - 1);
2886 output_escaped_param (d, fieldlength, "length");
2887 oprintf (d->of, ");\n");
2891 endcounter = d->counter;
2893 for (f = t->u.s.fields; f; f = f->next)
2895 options_p oo;
2896 const char *dot = ".";
2897 const char *tagid = NULL;
2898 int skip_p = 0;
2899 int default_p = 0;
2900 int use_param_p = 0;
2901 const char *fieldlength = NULL;
2902 char *newval;
2904 d->reorder_fn = NULL;
2905 for (oo = f->opt; oo; oo = oo->next)
2906 if (strcmp (oo->name, "dot") == 0
2907 && oo->kind == OPTION_STRING)
2908 dot = oo->info.string;
2909 else if (strcmp (oo->name, "tag") == 0
2910 && oo->kind == OPTION_STRING)
2911 tagid = oo->info.string;
2912 else if (strcmp (oo->name, "skip") == 0)
2913 skip_p = 1;
2914 else if (strcmp (oo->name, "default") == 0)
2915 default_p = 1;
2916 else if (strcmp (oo->name, "reorder") == 0
2917 && oo->kind == OPTION_STRING)
2918 d->reorder_fn = oo->info.string;
2919 else if (strncmp (oo->name, "use_param", 9) == 0
2920 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2921 use_param_p = 1;
2922 else if (strcmp (oo->name, "length") == 0
2923 && oo->kind == OPTION_STRING)
2924 fieldlength = oo->info.string;
2926 if (skip_p)
2927 continue;
2929 if (union_p && tagid)
2931 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
2932 d->indent += 2;
2934 else if (union_p && default_p)
2936 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2937 d->indent += 2;
2938 seen_default_p = 1;
2940 else if (!union_p && (default_p || tagid))
2941 error_at_line (d->line,
2942 "can't use `%s' outside a union on field `%s'",
2943 default_p ? "default" : "tag", f->name);
2944 else if (union_p && !(default_p || tagid)
2945 && f->type->kind == TYPE_SCALAR)
2947 fprintf (stderr,
2948 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2949 get_input_file_name (d->line->file), d->line->line,
2950 f->name);
2951 continue;
2953 else if (union_p && !(default_p || tagid))
2954 error_at_line (d->line,
2955 "field `%s' is missing `tag' or `default' option",
2956 f->name);
2958 if (fieldlength)
2960 d->loopcounter = endcounter - lengths_seen--;
2963 d->line = &f->line;
2964 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
2965 d->opt = f->opt;
2966 d->used_length = false;
2967 d->in_record_p = !union_p;
2969 if (union_p && use_param_p && d->param == NULL)
2970 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
2971 else
2972 walk_type (f->type, d);
2974 d->in_record_p = false;
2976 free (newval);
2978 if (union_p)
2980 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2981 d->indent -= 2;
2984 d->reorder_fn = NULL;
2986 d->val = oldval;
2987 d->prev_val[1] = oldprevval1;
2988 d->prev_val[2] = oldprevval2;
2990 if (union_p && !seen_default_p)
2992 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2993 oprintf (d->of, "%*s break;\n", d->indent, "");
2995 if (union_p)
2997 oprintf (d->of, "%*s}\n", d->indent, "");
2998 d->indent -= 2;
3000 if (any_length_seen)
3002 d->indent -= 2;
3003 oprintf (d->of, "%*s}\n", d->indent, "");
3006 break;
3008 case TYPE_LANG_STRUCT:
3010 type_p nt;
3011 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3012 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3013 break;
3014 if (nt == NULL)
3015 error_at_line (d->line, "structure `%s' differs between languages",
3016 t->u.s.tag);
3017 else
3018 walk_type (nt, d);
3020 break;
3022 case TYPE_PARAM_STRUCT:
3024 type_p *oldparam = d->param;
3026 d->param = t->u.param_struct.param;
3027 walk_type (t->u.param_struct.stru, d);
3028 d->param = oldparam;
3030 break;
3032 case TYPE_USER_STRUCT:
3033 d->process_field (t, d);
3034 break;
3036 default:
3037 gcc_unreachable ();
3041 /* process_field routine for marking routines. */
3043 static void
3044 write_types_process_field (type_p f, const struct walk_type_data *d)
3046 const struct write_types_data *wtd;
3047 const char *cast = d->needs_cast_p ? "(void *)" : "";
3048 wtd = (const struct write_types_data *) d->cookie;
3050 switch (f->kind)
3052 case TYPE_NONE:
3053 gcc_unreachable ();
3054 case TYPE_POINTER:
3055 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3056 wtd->subfield_marker_routine, cast, d->val);
3057 if (wtd->param_prefix)
3059 if (f->u.p->kind == TYPE_SCALAR)
3060 /* The current type is a pointer to a scalar (so not
3061 considered like a pointer to instances of user defined
3062 types) and we are seeing it; it means we must be even
3063 more careful about the second argument of the
3064 SUBFIELD_MARKER_ROUTINE call. That argument must
3065 always be the instance of the type for which
3066 write_func_for_structure was called - this really is
3067 what the function SUBFIELD_MARKER_ROUTINE expects.
3068 That is, it must be an instance of the ORIG_S type
3069 parameter of write_func_for_structure. The convention
3070 is that that argument must be "x" in that case (as set
3071 by write_func_for_structure). The problem is, we can't
3072 count on d->prev_val[3] to be always set to "x" in that
3073 case. Sometimes walk_type can set it to something else
3074 (to e.g cooperate with write_array when called from
3075 write_roots). So let's set it to "x" here then. */
3076 oprintf (d->of, ", x");
3077 else
3078 oprintf (d->of, ", %s", d->prev_val[3]);
3079 if (d->orig_s)
3081 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3082 output_mangled_typename (d->of, d->orig_s);
3084 else
3085 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3087 if (f->u.p->kind == TYPE_PARAM_STRUCT
3088 && f->u.p->u.s.line.file != NULL)
3090 oprintf (d->of, ", gt_e_");
3091 output_mangled_typename (d->of, f);
3093 else if (union_or_struct_p (f) && f->u.p->u.s.line.file != NULL)
3095 oprintf (d->of, ", gt_ggc_e_");
3096 output_mangled_typename (d->of, f);
3098 else
3099 oprintf (d->of, ", gt_types_enum_last");
3101 oprintf (d->of, ");\n");
3102 if (d->reorder_fn && wtd->reorder_note_routine)
3103 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3104 wtd->reorder_note_routine, cast, d->val,
3105 d->prev_val[3], d->reorder_fn);
3106 break;
3108 case TYPE_STRING:
3109 case TYPE_STRUCT:
3110 case TYPE_UNION:
3111 case TYPE_LANG_STRUCT:
3112 case TYPE_PARAM_STRUCT:
3113 case TYPE_USER_STRUCT:
3114 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3116 /* If F is a user-defined type and the field is not a
3117 pointer to the type, then we should not generate the
3118 standard pointer-marking code. All we need to do is call
3119 the user-provided marking function to process the fields
3120 of F. */
3121 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3122 d->val);
3124 else
3126 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3127 output_mangled_typename (d->of, f);
3128 oprintf (d->of, " (%s%s);\n", cast, d->val);
3129 if (d->reorder_fn && wtd->reorder_note_routine)
3130 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3131 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3132 d->reorder_fn);
3134 break;
3136 case TYPE_SCALAR:
3137 break;
3139 case TYPE_ARRAY:
3140 gcc_unreachable ();
3144 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
3146 static void
3147 output_type_enum (outf_p of, type_p s)
3149 if (s->kind == TYPE_PARAM_STRUCT && s->u.param_struct.line.file != NULL)
3151 oprintf (of, ", gt_e_");
3152 output_mangled_typename (of, s);
3154 else if (union_or_struct_p (s) && s->u.s.line.file != NULL)
3156 oprintf (of, ", gt_ggc_e_");
3157 output_mangled_typename (of, s);
3159 else
3160 oprintf (of, ", gt_types_enum_last");
3163 /* Return an output file that is suitable for definitions which can
3164 reference struct S */
3166 static outf_p
3167 get_output_file_for_structure (const_type_p s, type_p *param)
3169 const input_file *fn;
3170 int i;
3172 gcc_assert (union_or_struct_p (s));
3173 fn = s->u.s.line.file;
3175 /* This is a hack, and not the good kind either. */
3176 for (i = NUM_PARAM - 1; i >= 0; i--)
3177 if (param && param[i] && param[i]->kind == TYPE_POINTER
3178 && union_or_struct_p (param[i]->u.p))
3179 fn = param[i]->u.p->u.s.line.file;
3181 /* The call to get_output_file_with_visibility may update fn by
3182 caching its result inside, so we need the CONST_CAST. */
3183 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3187 /* Returns the specifier keyword for a string or union type S, empty string
3188 otherwise. */
3190 static const char *
3191 get_type_specifier (const type_p s)
3193 if (s->kind == TYPE_STRUCT)
3194 return "struct ";
3195 else if (s->kind == TYPE_LANG_STRUCT)
3196 return get_type_specifier (s->u.s.lang_struct);
3197 else if (s->kind == TYPE_UNION)
3198 return "union ";
3199 return "";
3203 /* Emits a declaration for type TY (assumed to be a union or a
3204 structure) on stream OUT. */
3206 static void
3207 write_type_decl (outf_p out, type_p ty)
3209 if (union_or_struct_p (ty))
3210 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3211 else if (ty->kind == TYPE_SCALAR)
3213 if (ty->u.scalar_is_char)
3214 oprintf (out, "const char");
3215 else
3216 oprintf (out, "void");
3218 else if (ty->kind == TYPE_POINTER)
3220 write_type_decl (out, ty->u.p);
3221 oprintf (out, " *");
3223 else if (ty->kind == TYPE_ARRAY)
3225 write_type_decl (out, ty->u.a.p);
3226 oprintf (out, " *");
3228 else if (ty->kind == TYPE_STRING)
3230 oprintf (out, "const char *");
3232 else
3233 gcc_unreachable ();
3237 /* Write on OF the name of the marker function for structure S. PREFIX
3238 is the prefix to use (to distinguish ggc from pch markers). */
3240 static void
3241 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3243 if (union_or_struct_p (s))
3245 const char *id_for_tag = filter_type_name (s->u.s.tag);
3246 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3247 if (id_for_tag != s->u.s.tag)
3248 free (CONST_CAST(char *, id_for_tag));
3250 else if (s->kind == TYPE_PARAM_STRUCT)
3252 oprintf (of, "gt_%s_", prefix);
3253 output_mangled_typename (of, s);
3255 else
3256 gcc_unreachable ();
3260 /* Write on OF a user-callable routine to act as an entry point for
3261 the marking routine for S, generated by write_func_for_structure.
3262 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3264 static void
3265 write_user_func_for_structure_ptr (outf_p of, type_p s, const char *prefix)
3267 /* Parameterized structures are not supported in user markers. There
3268 is no way for the marker function to know which specific type
3269 to use to generate the call to the void * entry point. For
3270 instance, a marker for struct htab may need to call different
3271 routines to mark the fields, depending on the paramN_is attributes.
3273 A user-defined marker that accepts 'struct htab' as its argument
3274 would not know which variant to call. Generating several entry
3275 points accepting 'struct htab' would cause multiply-defined
3276 errors during compilation. */
3277 gcc_assert (union_or_struct_p (s));
3279 type_p alias_of = NULL;
3280 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3281 if (strcmp (opt->name, "ptr_alias") == 0)
3283 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3284 we do not generate marking code for ORIG_S here. Instead, a
3285 forwarder #define in gtype-desc.h will cause every call to its
3286 marker to call the target of this alias.
3288 However, we still want to create a user entry code for the
3289 aliased type. So, if ALIAS_OF is set, we only generate the
3290 user-callable marker function. */
3291 alias_of = opt->info.type;
3292 break;
3295 oprintf (of, "\nvoid\n");
3296 oprintf (of, "gt_%sx (", prefix);
3297 write_type_decl (of, s);
3298 oprintf (of, " *& x)\n");
3299 oprintf (of, "{\n");
3300 oprintf (of, " if (x)\n ");
3301 write_marker_function_name (of, alias_of ? alias_of : s, prefix);
3302 oprintf (of, " ((void *) x);\n");
3303 oprintf (of, "}\n");
3307 /* Write a function to mark all the fields of type S on OF. PREFIX
3308 and D are as in write_user_marking_functions. */
3310 static void
3311 write_user_func_for_structure_body (type_p s, const char *prefix,
3312 struct walk_type_data *d)
3314 oprintf (d->of, "\nvoid\n");
3315 oprintf (d->of, "gt_%sx (", prefix);
3316 write_type_decl (d->of, s);
3317 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3318 oprintf (d->of, "{\n");
3319 oprintf (d->of, " ");
3320 write_type_decl (d->of, s);
3321 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3322 d->val = "(*x)";
3323 d->indent = 2;
3324 walk_type (s, d);
3325 oprintf (d->of, "}\n");
3329 /* Emit the user-callable functions needed to mark all the types used
3330 by the user structure S. PREFIX is the prefix to use to
3331 distinguish ggc and pch markers. D contains data needed to pass to
3332 walk_type when traversing the fields of a type.
3334 For every type T referenced by S, two routines are generated: one
3335 that takes 'T *', marks the pointer and calls the second routine,
3336 which just marks the fields of T. */
3338 static void
3339 write_user_marking_functions (type_p s, const char *prefix,
3340 struct walk_type_data *d)
3342 gcc_assert (s->kind == TYPE_USER_STRUCT);
3344 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3346 type_p fld_type = fld->type;
3347 if (fld_type->kind == TYPE_POINTER)
3349 type_p pointed_to_type = fld_type->u.p;
3350 if (union_or_struct_p (pointed_to_type))
3351 write_user_func_for_structure_ptr (d->of, pointed_to_type, prefix);
3353 else if (union_or_struct_p (fld_type))
3354 write_user_func_for_structure_body (fld_type, prefix, d);
3359 /* For S, a structure that's part of ORIG_S, and using parameters
3360 PARAM, write out a routine that:
3361 - Takes a parameter, a void * but actually of type *S
3362 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3363 field of S or its substructures and (in some cases) things
3364 that are pointed to by S. */
3366 static void
3367 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3368 const struct write_types_data *wtd)
3370 const char *chain_next = NULL;
3371 const char *chain_prev = NULL;
3372 const char *chain_circular = NULL;
3373 const char *mark_hook_name = NULL;
3374 options_p opt;
3375 struct walk_type_data d;
3377 memset (&d, 0, sizeof (d));
3378 d.of = get_output_file_for_structure (s, param);
3379 for (opt = s->u.s.opt; opt; opt = opt->next)
3380 if (strcmp (opt->name, "chain_next") == 0
3381 && opt->kind == OPTION_STRING)
3382 chain_next = opt->info.string;
3383 else if (strcmp (opt->name, "chain_prev") == 0
3384 && opt->kind == OPTION_STRING)
3385 chain_prev = opt->info.string;
3386 else if (strcmp (opt->name, "chain_circular") == 0
3387 && opt->kind == OPTION_STRING)
3388 chain_circular = opt->info.string;
3389 else if (strcmp (opt->name, "mark_hook") == 0
3390 && opt->kind == OPTION_STRING)
3391 mark_hook_name = opt->info.string;
3392 if (chain_prev != NULL && chain_next == NULL)
3393 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3394 if (chain_circular != NULL && chain_next != NULL)
3395 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3396 if (chain_circular != NULL)
3397 chain_next = chain_circular;
3399 d.process_field = write_types_process_field;
3400 d.cookie = wtd;
3401 d.orig_s = orig_s;
3402 d.opt = s->u.s.opt;
3403 d.line = &s->u.s.line;
3404 d.bitmap = s->u.s.bitmap;
3405 d.param = param;
3406 d.prev_val[0] = "*x";
3407 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3408 d.prev_val[3] = "x";
3409 d.val = "(*x)";
3410 d.have_this_obj = false;
3412 oprintf (d.of, "\n");
3413 oprintf (d.of, "void\n");
3414 write_marker_function_name (d.of, orig_s, wtd->prefix);
3415 oprintf (d.of, " (void *x_p)\n");
3416 oprintf (d.of, "{\n ");
3417 write_type_decl (d.of, s);
3418 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3419 write_type_decl (d.of, s);
3420 oprintf (d.of, " *)x_p;\n");
3421 if (chain_next != NULL)
3423 oprintf (d.of, " ");
3424 write_type_decl (d.of, s);
3425 oprintf (d.of, " * xlimit = x;\n");
3427 if (chain_next == NULL)
3429 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3430 if (wtd->param_prefix)
3432 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3433 output_mangled_typename (d.of, orig_s);
3434 output_type_enum (d.of, orig_s);
3436 oprintf (d.of, "))\n");
3438 else
3440 if (chain_circular != NULL)
3441 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3442 else
3443 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3444 if (wtd->param_prefix)
3446 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3447 output_mangled_typename (d.of, orig_s);
3448 output_type_enum (d.of, orig_s);
3450 oprintf (d.of, "))\n");
3451 if (chain_circular != NULL)
3452 oprintf (d.of, " return;\n do\n");
3453 if (mark_hook_name && !wtd->skip_hooks)
3455 oprintf (d.of, " {\n");
3456 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3458 oprintf (d.of, " xlimit = (");
3459 d.prev_val[2] = "*xlimit";
3460 output_escaped_param (&d, chain_next, "chain_next");
3461 oprintf (d.of, ");\n");
3462 if (mark_hook_name && !wtd->skip_hooks)
3463 oprintf (d.of, " }\n");
3464 if (chain_prev != NULL)
3466 oprintf (d.of, " if (x != xlimit)\n");
3467 oprintf (d.of, " for (;;)\n");
3468 oprintf (d.of, " {\n");
3469 oprintf (d.of, " %s %s * const xprev = (",
3470 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3472 d.prev_val[2] = "*x";
3473 output_escaped_param (&d, chain_prev, "chain_prev");
3474 oprintf (d.of, ");\n");
3475 oprintf (d.of, " if (xprev == NULL) break;\n");
3476 oprintf (d.of, " x = xprev;\n");
3477 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3478 if (wtd->param_prefix)
3480 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3481 output_mangled_typename (d.of, orig_s);
3482 output_type_enum (d.of, orig_s);
3484 oprintf (d.of, ");\n");
3485 oprintf (d.of, " }\n");
3487 if (chain_circular != NULL)
3489 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3490 if (wtd->param_prefix)
3492 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3493 output_mangled_typename (d.of, orig_s);
3494 output_type_enum (d.of, orig_s);
3496 oprintf (d.of, "));\n");
3497 if (mark_hook_name && !wtd->skip_hooks)
3498 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3499 oprintf (d.of, " do\n");
3501 else
3502 oprintf (d.of, " while (x != xlimit)\n");
3504 oprintf (d.of, " {\n");
3505 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3507 oprintf (d.of, " %s (x);\n", mark_hook_name);
3510 d.prev_val[2] = "*x";
3511 d.indent = 6;
3512 if (orig_s->kind != TYPE_USER_STRUCT)
3513 walk_type (s, &d);
3514 else
3516 /* User structures have no fields to walk. Simply generate a call
3517 to the user-provided structure marker. */
3518 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3521 if (chain_next != NULL)
3523 oprintf (d.of, " x = (");
3524 output_escaped_param (&d, chain_next, "chain_next");
3525 oprintf (d.of, ");\n");
3528 oprintf (d.of, " }\n");
3529 if (chain_circular != NULL)
3530 oprintf (d.of, " while (x != xlimit);\n");
3531 oprintf (d.of, "}\n");
3533 if (orig_s->kind == TYPE_USER_STRUCT)
3534 write_user_marking_functions (orig_s, wtd->prefix, &d);
3538 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3540 static void
3541 write_types (outf_p output_header, type_p structures, type_p param_structs,
3542 const struct write_types_data *wtd)
3544 int nbfun = 0; /* Count the emitted functions. */
3545 type_p s;
3547 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3549 /* We first emit the macros and the declarations. Functions' code is
3550 emitted afterwards. This is needed in plugin mode. */
3551 oprintf (output_header, "/* Macros and declarations. */\n");
3552 for (s = structures; s; s = s->next)
3553 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3555 options_p opt;
3557 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3558 continue;
3560 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3562 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3563 output_mangled_typename (output_header, s);
3564 oprintf (output_header, "(X) do { \\\n");
3565 oprintf (output_header,
3566 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3567 s_id_for_tag);
3568 oprintf (output_header, " } while (0)\n");
3570 for (opt = s->u.s.opt; opt; opt = opt->next)
3571 if (strcmp (opt->name, "ptr_alias") == 0
3572 && opt->kind == OPTION_TYPE)
3574 const_type_p const t = (const_type_p) opt->info.type;
3575 if (t->kind == TYPE_STRUCT
3576 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3578 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3579 oprintf (output_header,
3580 "#define gt_%sx_%s gt_%sx_%s\n",
3581 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3582 if (t_id_for_tag != t->u.s.tag)
3583 free (CONST_CAST(char *, t_id_for_tag));
3585 else
3586 error_at_line (&s->u.s.line,
3587 "structure alias is not a structure");
3588 break;
3590 if (opt)
3591 continue;
3593 /* Declare the marker procedure only once. */
3594 oprintf (output_header,
3595 "extern void gt_%sx_%s (void *);\n",
3596 wtd->prefix, s_id_for_tag);
3598 if (s_id_for_tag != s->u.s.tag)
3599 free (CONST_CAST(char *, s_id_for_tag));
3601 if (s->u.s.line.file == NULL)
3603 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3604 s->u.s.tag);
3605 continue;
3609 for (s = param_structs; s; s = s->next)
3610 if (s->gc_used == GC_POINTED_TO)
3612 type_p stru = s->u.param_struct.stru;
3614 /* Declare the marker procedure. */
3615 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3616 output_mangled_typename (output_header, s);
3617 oprintf (output_header, " (void *);\n");
3619 if (stru->u.s.line.file == NULL)
3621 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3622 stru->u.s.tag);
3623 continue;
3627 /* At last we emit the functions code. */
3628 oprintf (output_header, "\n/* functions code */\n");
3629 for (s = structures; s; s = s->next)
3630 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3632 options_p opt;
3634 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3635 continue;
3636 for (opt = s->u.s.opt; opt; opt = opt->next)
3637 if (strcmp (opt->name, "ptr_alias") == 0)
3638 break;
3639 if (opt)
3640 continue;
3642 if (s->kind == TYPE_LANG_STRUCT)
3644 type_p ss;
3645 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3647 nbfun++;
3648 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3649 nbfun, (void*) ss, ss->u.s.tag);
3650 write_func_for_structure (s, ss, NULL, wtd);
3653 else
3655 nbfun++;
3656 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3657 nbfun, (void*) s, s->u.s.tag);
3658 write_func_for_structure (s, s, NULL, wtd);
3661 else
3663 /* Structure s is not possibly pointed to, so can be ignored. */
3664 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3665 (void*)s, s->u.s.tag,
3666 (int) s->gc_used);
3669 for (s = param_structs; s; s = s->next)
3670 if (s->gc_used == GC_POINTED_TO)
3672 type_p *param = s->u.param_struct.param;
3673 type_p stru = s->u.param_struct.stru;
3674 if (stru->u.s.line.file == NULL)
3675 continue;
3676 if (stru->kind == TYPE_LANG_STRUCT)
3678 type_p ss;
3679 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3681 nbfun++;
3682 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3683 nbfun, (void*) ss, ss->u.s.tag);
3684 write_func_for_structure (s, ss, param, wtd);
3687 else
3689 nbfun++;
3690 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3691 nbfun, (void*) s,
3692 (void*) stru, stru->u.s.tag);
3693 write_func_for_structure (s, stru, param, wtd);
3696 else
3698 /* Param structure s is not pointed to, so should be ignored. */
3699 DBGPRINTF ("ignored s @ %p", (void*)s);
3701 if (verbosity_level >= 2)
3702 printf ("%s emitted %d routines for %s\n",
3703 progname, nbfun, wtd->comment);
3706 static const struct write_types_data ggc_wtd = {
3707 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3708 "GC marker procedures. ",
3709 FALSE
3712 static const struct write_types_data pch_wtd = {
3713 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3714 "gt_pch_note_reorder",
3715 "PCH type-walking procedures. ",
3716 TRUE
3719 /* Write out the local pointer-walking routines. */
3721 /* process_field routine for local pointer-walking for user-callable
3722 routines. The difference between this and
3723 write_types_local_process_field is that, in this case, we do not
3724 need to check whether the given pointer matches the address of the
3725 parent structure. This check was already generated by the call
3726 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3727 this code. */
3729 static void
3730 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3732 switch (f->kind)
3734 case TYPE_POINTER:
3735 case TYPE_STRUCT:
3736 case TYPE_UNION:
3737 case TYPE_LANG_STRUCT:
3738 case TYPE_PARAM_STRUCT:
3739 case TYPE_STRING:
3740 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3741 break;
3743 case TYPE_USER_STRUCT:
3744 if (d->in_ptr_field)
3745 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3746 else
3747 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3748 d->indent, "", d->val);
3749 break;
3751 case TYPE_SCALAR:
3752 break;
3754 default:
3755 gcc_unreachable ();
3760 /* Write a function to PCH walk all the fields of type S on OF.
3761 D contains data needed by walk_type to recurse into the fields of S. */
3763 static void
3764 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3766 oprintf (d->of, "\nvoid\n");
3767 oprintf (d->of, "gt_pch_nx (");
3768 write_type_decl (d->of, s);
3769 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3770 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3771 "\tATTRIBUTE_UNUSED void *cookie)\n");
3772 oprintf (d->of, "{\n");
3773 d->val = "(*x)";
3774 d->indent = 2;
3775 d->process_field = write_types_local_user_process_field;
3776 walk_type (s, d);
3777 oprintf (d->of, "}\n");
3781 /* Emit the user-callable functions needed to mark all the types used
3782 by the user structure S. PREFIX is the prefix to use to
3783 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3784 chain_next option defined. D contains data needed to pass to
3785 walk_type when traversing the fields of a type.
3787 For every type T referenced by S, two routines are generated: one
3788 that takes 'T *', marks the pointer and calls the second routine,
3789 which just marks the fields of T. */
3791 static void
3792 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3794 gcc_assert (s->kind == TYPE_USER_STRUCT);
3796 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3798 type_p fld_type = fld->type;
3799 if (union_or_struct_p (fld_type))
3800 write_pch_user_walking_for_structure_body (fld_type, d);
3805 /* process_field routine for local pointer-walking. */
3807 static void
3808 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3810 gcc_assert (d->have_this_obj);
3811 switch (f->kind)
3813 case TYPE_POINTER:
3814 case TYPE_STRUCT:
3815 case TYPE_UNION:
3816 case TYPE_LANG_STRUCT:
3817 case TYPE_PARAM_STRUCT:
3818 case TYPE_STRING:
3819 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3820 d->prev_val[3]);
3821 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3822 break;
3824 case TYPE_USER_STRUCT:
3825 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3826 d->prev_val[3]);
3827 if (d->in_ptr_field)
3828 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3829 else
3830 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3831 d->indent, "", d->val);
3832 break;
3834 case TYPE_SCALAR:
3835 break;
3837 default:
3838 gcc_unreachable ();
3843 /* For S, a structure that's part of ORIG_S, and using parameters
3844 PARAM, write out a routine that:
3845 - Is of type gt_note_pointers
3846 - Calls PROCESS_FIELD on each field of S or its substructures.
3849 static void
3850 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3852 struct walk_type_data d;
3854 memset (&d, 0, sizeof (d));
3855 d.of = get_output_file_for_structure (s, param);
3856 d.process_field = write_types_local_process_field;
3857 d.opt = s->u.s.opt;
3858 d.line = &s->u.s.line;
3859 d.bitmap = s->u.s.bitmap;
3860 d.param = param;
3861 d.prev_val[0] = d.prev_val[2] = "*x";
3862 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3863 d.prev_val[3] = "x";
3864 d.val = "(*x)";
3865 d.fn_wants_lvalue = true;
3867 oprintf (d.of, "\n");
3868 oprintf (d.of, "void\n");
3869 oprintf (d.of, "gt_pch_p_");
3870 output_mangled_typename (d.of, orig_s);
3871 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3872 "\tvoid *x_p,\n"
3873 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3874 "\tATTRIBUTE_UNUSED void *cookie)\n");
3875 oprintf (d.of, "{\n");
3876 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3877 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3878 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3879 d.indent = 2;
3880 d.have_this_obj = true;
3882 if (s->kind != TYPE_USER_STRUCT)
3883 walk_type (s, &d);
3884 else
3886 /* User structures have no fields to walk. Simply generate a
3887 call to the user-provided PCH walker. */
3888 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3889 d.prev_val[3]);
3890 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3891 d.indent, "", d.val);
3894 oprintf (d.of, "}\n");
3896 /* Write user-callable entry points for the PCH walking routines. */
3897 if (orig_s->kind == TYPE_USER_STRUCT)
3898 write_pch_user_walking_functions (s, &d);
3901 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3903 static void
3904 write_local (outf_p output_header, type_p structures, type_p param_structs)
3906 type_p s;
3908 if (!output_header)
3909 return;
3911 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3912 for (s = structures; s; s = s->next)
3913 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3915 options_p opt;
3917 if (s->u.s.line.file == NULL)
3918 continue;
3919 for (opt = s->u.s.opt; opt; opt = opt->next)
3920 if (strcmp (opt->name, "ptr_alias") == 0
3921 && opt->kind == OPTION_TYPE)
3923 const_type_p const t = (const_type_p) opt->info.type;
3924 if (t->kind == TYPE_STRUCT
3925 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3927 oprintf (output_header, "#define gt_pch_p_");
3928 output_mangled_typename (output_header, s);
3929 oprintf (output_header, " gt_pch_p_");
3930 output_mangled_typename (output_header, t);
3931 oprintf (output_header, "\n");
3933 else
3934 error_at_line (&s->u.s.line,
3935 "structure alias is not a structure");
3936 break;
3938 if (opt)
3939 continue;
3941 /* Declare the marker procedure only once. */
3942 oprintf (output_header, "extern void gt_pch_p_");
3943 output_mangled_typename (output_header, s);
3944 oprintf (output_header,
3945 "\n (void *, void *, gt_pointer_operator, void *);\n");
3947 if (s->kind == TYPE_LANG_STRUCT)
3949 type_p ss;
3950 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3951 write_local_func_for_structure (s, ss, NULL);
3953 else
3954 write_local_func_for_structure (s, s, NULL);
3957 for (s = param_structs; s; s = s->next)
3958 if (s->gc_used == GC_POINTED_TO)
3960 type_p *param = s->u.param_struct.param;
3961 type_p stru = s->u.param_struct.stru;
3963 /* Declare the marker procedure. */
3964 oprintf (output_header, "extern void gt_pch_p_");
3965 output_mangled_typename (output_header, s);
3966 oprintf (output_header,
3967 "\n (void *, void *, gt_pointer_operator, void *);\n");
3969 if (stru->u.s.line.file == NULL)
3971 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3972 stru->u.s.tag);
3973 continue;
3976 if (stru->kind == TYPE_LANG_STRUCT)
3978 type_p ss;
3979 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3980 write_local_func_for_structure (s, ss, param);
3982 else
3983 write_local_func_for_structure (s, stru, param);
3987 /* Nonzero if S is a type for which typed GC allocators should be output. */
3989 #define USED_BY_TYPED_GC_P(s) \
3990 ((s->kind == TYPE_POINTER \
3991 && (s->u.p->gc_used == GC_POINTED_TO \
3992 || s->u.p->gc_used == GC_USED)) \
3993 || (union_or_struct_p (s) \
3994 && ((s)->gc_used == GC_POINTED_TO \
3995 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
3996 && s->u.s.line.file != NULL) \
3997 || ((s)->gc_used == GC_USED \
3998 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
4001 /* Write out the 'enum' definition for gt_types_enum. */
4003 static void
4004 write_enum_defn (type_p structures, type_p param_structs)
4006 type_p s;
4007 int nbstruct = 0;
4008 int nbparamstruct = 0;
4010 if (!header_file)
4011 return;
4012 oprintf (header_file, "\n/* Enumeration of types known. */\n");
4013 oprintf (header_file, "enum gt_types_enum {\n");
4014 for (s = structures; s; s = s->next)
4015 if (USED_BY_TYPED_GC_P (s))
4017 nbstruct++;
4018 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
4019 (void*) s, nbstruct);
4020 if (union_or_struct_p (s))
4021 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
4022 (void*) s, nbstruct, s->u.s.tag);
4023 oprintf (header_file, " gt_ggc_e_");
4024 output_mangled_typename (header_file, s);
4025 oprintf (header_file, ",\n");
4027 for (s = param_structs; s; s = s->next)
4028 if (s->gc_used == GC_POINTED_TO)
4030 nbparamstruct++;
4031 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
4032 (void*) s, nbparamstruct);
4033 oprintf (header_file, " gt_e_");
4034 output_mangled_typename (header_file, s);
4035 oprintf (header_file, ",\n");
4037 oprintf (header_file, " gt_types_enum_last\n");
4038 oprintf (header_file, "};\n");
4039 if (verbosity_level >= 2)
4040 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
4041 progname, nbstruct, nbparamstruct);
4045 /* Might T contain any non-pointer elements? */
4047 static int
4048 contains_scalar_p (type_p t)
4050 switch (t->kind)
4052 case TYPE_STRING:
4053 case TYPE_POINTER:
4054 return 0;
4055 case TYPE_ARRAY:
4056 return contains_scalar_p (t->u.a.p);
4057 default:
4058 /* Could also check for structures that have no non-pointer
4059 fields, but there aren't enough of those to worry about. */
4060 return 1;
4064 /* Mangle INPF and print it to F. */
4066 static void
4067 put_mangled_filename (outf_p f, const input_file *inpf)
4069 /* The call to get_output_file_name may indirectly update fn since
4070 get_output_file_with_visibility caches its result inside, so we
4071 need the CONST_CAST. */
4072 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4073 if (!f || !name)
4074 return;
4075 for (; *name != 0; name++)
4076 if (ISALNUM (*name))
4077 oprintf (f, "%c", *name);
4078 else
4079 oprintf (f, "%c", '_');
4082 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4083 LASTNAME, and NAME are all strings to insert in various places in
4084 the resulting code. */
4086 static void
4087 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4088 const char *tname, const char *name)
4090 struct flist *fli2;
4092 for (fli2 = flp; fli2; fli2 = fli2->next)
4093 if (fli2->started_p)
4095 oprintf (fli2->f, " %s\n", lastname);
4096 oprintf (fli2->f, "};\n\n");
4099 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4100 if (fli2->started_p)
4102 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4103 int fnum;
4105 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4106 if (bitmap & 1)
4108 oprintf (base_files[fnum],
4109 "extern const struct %s gt_%s_", tname, pfx);
4110 put_mangled_filename (base_files[fnum], fli2->file);
4111 oprintf (base_files[fnum], "[];\n");
4116 size_t fnum;
4117 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4118 oprintf (base_files[fnum],
4119 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4123 for (fli2 = flp; fli2; fli2 = fli2->next)
4124 if (fli2->started_p)
4126 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4127 int fnum;
4129 fli2->started_p = 0;
4131 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4132 if (bitmap & 1)
4134 oprintf (base_files[fnum], " gt_%s_", pfx);
4135 put_mangled_filename (base_files[fnum], fli2->file);
4136 oprintf (base_files[fnum], ",\n");
4141 size_t fnum;
4142 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4144 oprintf (base_files[fnum], " NULL\n");
4145 oprintf (base_files[fnum], "};\n");
4150 /* Write the first three fields (pointer, count and stride) for
4151 root NAME to F. V and LINE are as for write_root.
4153 Return true if the entry could be written; return false on error. */
4155 static bool
4156 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4158 type_p ap;
4160 if (!v)
4162 error_at_line (line, "`%s' is too complex to be a root", name);
4163 return false;
4166 oprintf (f, " {\n");
4167 oprintf (f, " &%s,\n", name);
4168 oprintf (f, " 1");
4170 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4171 if (ap->u.a.len[0])
4172 oprintf (f, " * (%s)", ap->u.a.len);
4173 else if (ap == v->type)
4174 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4175 oprintf (f, ",\n");
4176 oprintf (f, " sizeof (%s", v->name);
4177 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4178 oprintf (f, "[0]");
4179 oprintf (f, "),\n");
4180 return true;
4183 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4184 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4185 of the caller. */
4187 static void
4188 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4189 int has_length, struct fileloc *line, const char *if_marked,
4190 bool emit_pch, type_p field_type, const char *field_name)
4192 struct pair newv;
4193 /* If the field reference is relative to V, rather than to some
4194 subcomponent of V, we can mark any subarrays with a single stride.
4195 We're effectively treating the field as a global variable in its
4196 own right. */
4197 if (v && type == v->type)
4199 newv = *v;
4200 newv.type = field_type;
4201 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4202 v = &newv;
4204 /* Otherwise, any arrays nested in the structure are too complex to
4205 handle. */
4206 else if (field_type->kind == TYPE_ARRAY)
4207 v = NULL;
4208 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4209 has_length, line, if_marked, emit_pch);
4212 /* Write out to F the table entry and any marker routines needed to
4213 mark NAME as TYPE. V can be one of three values:
4215 - null, if NAME is too complex to represent using a single
4216 count and stride. In this case, it is an error for NAME to
4217 contain any gc-ed data.
4219 - the outermost array that contains NAME, if NAME is part of an array.
4221 - the C variable that contains NAME, if NAME is not part of an array.
4223 LINE is the line of the C source that declares the root variable.
4224 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4225 is nonzero iff we are building the root table for hash table caches. */
4227 static void
4228 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4229 struct fileloc *line, const char *if_marked, bool emit_pch)
4231 switch (type->kind)
4233 case TYPE_STRUCT:
4235 pair_p fld;
4236 for (fld = type->u.s.fields; fld; fld = fld->next)
4238 int skip_p = 0;
4239 const char *desc = NULL;
4240 options_p o;
4242 for (o = fld->opt; o; o = o->next)
4243 if (strcmp (o->name, "skip") == 0)
4244 skip_p = 1;
4245 else if (strcmp (o->name, "desc") == 0
4246 && o->kind == OPTION_STRING)
4247 desc = o->info.string;
4248 else if (strcmp (o->name, "param_is") == 0)
4250 else
4251 error_at_line (line,
4252 "field `%s' of global `%s' has unknown option `%s'",
4253 fld->name, name, o->name);
4255 if (skip_p)
4256 continue;
4257 else if (desc && fld->type->kind == TYPE_UNION)
4259 pair_p validf = NULL;
4260 pair_p ufld;
4262 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4264 const char *tag = NULL;
4265 options_p oo;
4266 for (oo = ufld->opt; oo; oo = oo->next)
4267 if (strcmp (oo->name, "tag") == 0
4268 && oo->kind == OPTION_STRING)
4269 tag = oo->info.string;
4270 if (tag == NULL || strcmp (tag, desc) != 0)
4271 continue;
4272 if (validf != NULL)
4273 error_at_line (line,
4274 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4275 name, fld->name, validf->name,
4276 name, fld->name, ufld->name, tag);
4277 validf = ufld;
4279 if (validf != NULL)
4280 write_field_root (f, v, type, name, 0, line, if_marked,
4281 emit_pch, validf->type,
4282 ACONCAT ((fld->name, ".",
4283 validf->name, NULL)));
4285 else if (desc)
4286 error_at_line (line,
4287 "global `%s.%s' has `desc' option but is not union",
4288 name, fld->name);
4289 else
4290 write_field_root (f, v, type, name, 0, line, if_marked,
4291 emit_pch, fld->type, fld->name);
4294 break;
4296 case TYPE_ARRAY:
4298 char *newname;
4299 newname = xasprintf ("%s[0]", name);
4300 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4301 emit_pch);
4302 free (newname);
4304 break;
4306 case TYPE_USER_STRUCT:
4307 write_root (f, v, type->u.a.p, name, has_length, line, if_marked,
4308 emit_pch);
4309 break;
4311 case TYPE_POINTER:
4313 type_p tp;
4315 if (!start_root_entry (f, v, name, line))
4316 return;
4318 tp = type->u.p;
4320 if (!has_length && union_or_struct_p (tp))
4322 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4323 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4324 if (emit_pch)
4325 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4326 else
4327 oprintf (f, " NULL");
4328 if (id_for_tag != tp->u.s.tag)
4329 free (CONST_CAST(char *, id_for_tag));
4331 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4333 oprintf (f, " &gt_ggc_m_");
4334 output_mangled_typename (f, tp);
4335 if (emit_pch)
4337 oprintf (f, ",\n &gt_pch_n_");
4338 output_mangled_typename (f, tp);
4340 else
4341 oprintf (f, ",\n NULL");
4343 else if (has_length
4344 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4346 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4347 if (emit_pch)
4348 oprintf (f, " &gt_pch_na_%s", name);
4349 else
4350 oprintf (f, " NULL");
4352 else
4354 error_at_line (line,
4355 "global `%s' is pointer to unimplemented type",
4356 name);
4358 if (if_marked)
4359 oprintf (f, ",\n &%s", if_marked);
4360 oprintf (f, "\n },\n");
4362 break;
4364 case TYPE_STRING:
4366 if (!start_root_entry (f, v, name, line))
4367 return;
4369 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4370 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4371 oprintf (f, " },\n");
4373 break;
4375 case TYPE_SCALAR:
4376 break;
4378 default:
4379 error_at_line (line, "global `%s' is unimplemented type", name);
4383 /* This generates a routine to walk an array. */
4385 static void
4386 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4388 struct walk_type_data d;
4389 char *prevval3;
4391 memset (&d, 0, sizeof (d));
4392 d.of = f;
4393 d.cookie = wtd;
4394 d.indent = 2;
4395 d.line = &v->line;
4396 d.opt = v->opt;
4397 d.bitmap = get_lang_bitmap (v->line.file);
4398 d.param = NULL;
4400 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4402 if (wtd->param_prefix)
4404 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4405 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4406 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4407 wtd->param_prefix, v->name);
4408 oprintf (d.of,
4409 " ATTRIBUTE_UNUSED void *x_p,\n"
4410 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4411 " ATTRIBUTE_UNUSED void * cookie)\n");
4412 oprintf (d.of, "{\n");
4413 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4414 d.process_field = write_types_local_process_field;
4415 d.have_this_obj = true;
4416 walk_type (v->type, &d);
4417 oprintf (f, "}\n\n");
4420 d.opt = v->opt;
4421 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4422 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4423 wtd->prefix, v->name);
4424 oprintf (f, "{\n");
4425 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4426 d.process_field = write_types_process_field;
4427 d.have_this_obj = false;
4428 walk_type (v->type, &d);
4429 free (prevval3);
4430 oprintf (f, "}\n\n");
4433 /* Output a table describing the locations and types of VARIABLES. */
4435 static void
4436 write_roots (pair_p variables, bool emit_pch)
4438 pair_p v;
4439 struct flist *flp = NULL;
4441 for (v = variables; v; v = v->next)
4443 outf_p f =
4444 get_output_file_with_visibility (CONST_CAST (input_file*,
4445 v->line.file));
4446 struct flist *fli;
4447 const char *length = NULL;
4448 int deletable_p = 0;
4449 options_p o;
4450 for (o = v->opt; o; o = o->next)
4451 if (strcmp (o->name, "length") == 0
4452 && o->kind == OPTION_STRING)
4453 length = o->info.string;
4454 else if (strcmp (o->name, "deletable") == 0)
4455 deletable_p = 1;
4456 else if (strcmp (o->name, "param_is") == 0)
4458 else if (strncmp (o->name, "param", 5) == 0
4459 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4461 else if (strcmp (o->name, "if_marked") == 0)
4463 else
4464 error_at_line (&v->line,
4465 "global `%s' has unknown option `%s'",
4466 v->name, o->name);
4468 for (fli = flp; fli; fli = fli->next)
4469 if (fli->f == f && f)
4470 break;
4471 if (fli == NULL)
4473 fli = XNEW (struct flist);
4474 fli->f = f;
4475 fli->next = flp;
4476 fli->started_p = 0;
4477 fli->file = v->line.file;
4478 gcc_assert (fli->file);
4479 flp = fli;
4481 oprintf (f, "\n/* GC roots. */\n\n");
4484 if (!deletable_p
4485 && length
4486 && v->type->kind == TYPE_POINTER
4487 && (v->type->u.p->kind == TYPE_POINTER
4488 || v->type->u.p->kind == TYPE_STRUCT))
4490 write_array (f, v, &ggc_wtd);
4491 write_array (f, v, &pch_wtd);
4495 for (v = variables; v; v = v->next)
4497 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4498 v->line.file));
4499 struct flist *fli;
4500 int skip_p = 0;
4501 int length_p = 0;
4502 options_p o;
4504 for (o = v->opt; o; o = o->next)
4505 if (strcmp (o->name, "length") == 0)
4506 length_p = 1;
4507 else if (strcmp (o->name, "deletable") == 0
4508 || strcmp (o->name, "if_marked") == 0)
4509 skip_p = 1;
4511 if (skip_p)
4512 continue;
4514 for (fli = flp; fli; fli = fli->next)
4515 if (fli->f == f)
4516 break;
4517 if (!fli->started_p)
4519 fli->started_p = 1;
4521 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4522 put_mangled_filename (f, v->line.file);
4523 oprintf (f, "[] = {\n");
4526 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4529 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4530 "gt_ggc_rtab");
4532 for (v = variables; v; v = v->next)
4534 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4535 v->line.file));
4536 struct flist *fli;
4537 int skip_p = 1;
4538 options_p o;
4540 for (o = v->opt; o; o = o->next)
4541 if (strcmp (o->name, "deletable") == 0)
4542 skip_p = 0;
4543 else if (strcmp (o->name, "if_marked") == 0)
4544 skip_p = 1;
4546 if (skip_p)
4547 continue;
4549 for (fli = flp; fli; fli = fli->next)
4550 if (fli->f == f)
4551 break;
4552 if (!fli->started_p)
4554 fli->started_p = 1;
4556 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4557 put_mangled_filename (f, v->line.file);
4558 oprintf (f, "[] = {\n");
4561 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4562 v->name, v->name);
4565 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4566 "gt_ggc_deletable_rtab");
4568 for (v = variables; v; v = v->next)
4570 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4571 v->line.file));
4572 struct flist *fli;
4573 const char *if_marked = NULL;
4574 int length_p = 0;
4575 options_p o;
4577 for (o = v->opt; o; o = o->next)
4578 if (strcmp (o->name, "length") == 0)
4579 length_p = 1;
4580 else if (strcmp (o->name, "if_marked") == 0
4581 && o->kind == OPTION_STRING)
4582 if_marked = o->info.string;
4583 if (if_marked == NULL)
4584 continue;
4585 if (v->type->kind != TYPE_POINTER
4586 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4587 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4588 TYPE_STRUCT))
4590 error_at_line (&v->line,
4591 "if_marked option used but not hash table");
4592 continue;
4595 for (fli = flp; fli; fli = fli->next)
4596 if (fli->f == f)
4597 break;
4598 if (!fli->started_p)
4600 fli->started_p = 1;
4602 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4603 put_mangled_filename (f, v->line.file);
4604 oprintf (f, "[] = {\n");
4607 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4608 v->name, length_p, &v->line, if_marked, emit_pch);
4611 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4612 "gt_ggc_cache_rtab");
4614 if (!emit_pch)
4615 return;
4617 for (v = variables; v; v = v->next)
4619 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4620 v->line.file));
4621 struct flist *fli;
4622 int length_p = 0;
4623 int if_marked_p = 0;
4624 options_p o;
4626 for (o = v->opt; o; o = o->next)
4627 if (strcmp (o->name, "length") == 0)
4628 length_p = 1;
4629 else if (strcmp (o->name, "if_marked") == 0)
4630 if_marked_p = 1;
4632 if (!if_marked_p)
4633 continue;
4635 for (fli = flp; fli; fli = fli->next)
4636 if (fli->f == f)
4637 break;
4638 if (!fli->started_p)
4640 fli->started_p = 1;
4642 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4643 put_mangled_filename (f, v->line.file);
4644 oprintf (f, "[] = {\n");
4647 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4650 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4651 "gt_pch_cache_rtab");
4653 for (v = variables; v; v = v->next)
4655 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4656 v->line.file));
4657 struct flist *fli;
4658 int skip_p = 0;
4659 options_p o;
4661 for (o = v->opt; o; o = o->next)
4662 if (strcmp (o->name, "deletable") == 0
4663 || strcmp (o->name, "if_marked") == 0)
4664 skip_p = 1;
4666 if (skip_p)
4667 continue;
4669 if (!contains_scalar_p (v->type))
4670 continue;
4672 for (fli = flp; fli; fli = fli->next)
4673 if (fli->f == f)
4674 break;
4675 if (!fli->started_p)
4677 fli->started_p = 1;
4679 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4680 put_mangled_filename (f, v->line.file);
4681 oprintf (f, "[] = {\n");
4684 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4685 v->name, v->name);
4688 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4689 "gt_pch_scalar_rtab");
4692 /* TRUE if type S has the GTY variable_size annotation. */
4694 static bool
4695 variable_size_p (const type_p s)
4697 options_p o;
4698 for (o = s->u.s.opt; o; o = o->next)
4699 if (strcmp (o->name, "variable_size") == 0)
4700 return true;
4701 return false;
4704 enum alloc_quantity
4705 { single, vector };
4706 enum alloc_zone
4707 { any_zone, specific_zone };
4709 /* Writes one typed allocator definition into output F for type
4710 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4711 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4712 is true, the allocator will have an extra parameter specifying
4713 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4714 vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
4715 the allocator will be zone-specific. */
4717 static void
4718 write_typed_alloc_def (outf_p f,
4719 bool variable_size, const char *type_specifier,
4720 const char *type_name, const char *allocator_type,
4721 enum alloc_quantity quantity, enum alloc_zone zone)
4723 bool two_args = variable_size && (quantity == vector);
4724 bool third_arg = ((zone == specific_zone)
4725 && (variable_size || (quantity == vector)));
4726 gcc_assert (f != NULL);
4727 const char *type_name_as_id = filter_type_name (type_name);
4728 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name_as_id);
4729 oprintf (f, "(%s%s%s%s%s) ",
4730 (variable_size ? "SIZE" : ""),
4731 (two_args ? ", " : ""),
4732 (quantity == vector) ? "n" : "",
4733 (third_arg ? ", " : ""), (zone == specific_zone) ? "z" : "");
4734 oprintf (f, "((%s%s *)", type_specifier, type_name);
4735 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4736 if (zone == specific_zone)
4737 oprintf (f, "z, ");
4738 if (variable_size)
4739 oprintf (f, "SIZE");
4740 else
4741 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4742 if (quantity == vector)
4743 oprintf (f, ", n");
4744 oprintf (f, " MEM_STAT_INFO)))\n");
4745 if (type_name_as_id != type_name)
4746 free (CONST_CAST(char *, type_name_as_id));
4749 /* Writes a typed allocator definition into output F for a struct or
4750 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4752 static void
4753 write_typed_struct_alloc_def (outf_p f,
4754 const type_p s, const char *allocator_type,
4755 enum alloc_quantity quantity,
4756 enum alloc_zone zone)
4758 gcc_assert (union_or_struct_p (s));
4759 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4760 s->u.s.tag, allocator_type, quantity, zone);
4763 /* Writes a typed allocator definition into output F for a typedef P,
4764 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4766 static void
4767 write_typed_typedef_alloc_def (outf_p f,
4768 const pair_p p, const char *allocator_type,
4769 enum alloc_quantity quantity,
4770 enum alloc_zone zone)
4772 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4773 allocator_type, quantity, zone);
4776 /* Writes typed allocator definitions into output F for the types in
4777 STRUCTURES and TYPEDEFS that are used by GC. */
4779 static void
4780 write_typed_alloc_defns (outf_p f,
4781 const type_p structures, const pair_p typedefs)
4783 type_p s;
4784 pair_p p;
4786 gcc_assert (f != NULL);
4787 oprintf (f,
4788 "\n/* Allocators for known structs and unions. */\n\n");
4789 for (s = structures; s; s = s->next)
4791 if (!USED_BY_TYPED_GC_P (s))
4792 continue;
4793 gcc_assert (union_or_struct_p (s));
4794 /* In plugin mode onput output ggc_alloc macro definitions
4795 relevant to plugin input files. */
4796 if (nb_plugin_files > 0
4797 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
4798 continue;
4799 write_typed_struct_alloc_def (f, s, "", single, any_zone);
4800 write_typed_struct_alloc_def (f, s, "cleared_", single, any_zone);
4801 write_typed_struct_alloc_def (f, s, "vec_", vector, any_zone);
4802 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector, any_zone);
4803 write_typed_struct_alloc_def (f, s, "zone_", single, specific_zone);
4804 write_typed_struct_alloc_def (f, s, "zone_cleared_", single,
4805 specific_zone);
4806 write_typed_struct_alloc_def (f, s, "zone_vec_", vector, specific_zone);
4807 write_typed_struct_alloc_def (f, s, "zone_cleared_vec_", vector,
4808 specific_zone);
4811 oprintf (f, "\n/* Allocators for known typedefs. */\n");
4812 for (p = typedefs; p; p = p->next)
4814 s = p->type;
4815 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4816 continue;
4817 /* In plugin mode onput output ggc_alloc macro definitions
4818 relevant to plugin input files. */
4819 if (nb_plugin_files > 0)
4821 struct fileloc* filoc = type_fileloc(s);
4822 if (!filoc || !filoc->file->inpisplugin)
4823 continue;
4825 write_typed_typedef_alloc_def (f, p, "", single, any_zone);
4826 write_typed_typedef_alloc_def (f, p, "cleared_", single, any_zone);
4827 write_typed_typedef_alloc_def (f, p, "vec_", vector, any_zone);
4828 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector, any_zone);
4829 write_typed_typedef_alloc_def (f, p, "zone_", single, specific_zone);
4830 write_typed_typedef_alloc_def (f, p, "zone_cleared_", single,
4831 specific_zone);
4832 write_typed_typedef_alloc_def (f, p, "zone_cleared_vec_", vector,
4833 specific_zone);
4837 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4838 guaranteee for somewhat increased readability. If name conflicts do happen,
4839 this funcion will have to be adjusted to be more like
4840 output_mangled_typename. */
4842 static void
4843 output_typename (outf_p of, const_type_p t)
4845 switch (t->kind)
4847 case TYPE_STRING:
4848 oprintf (of, "str");
4849 break;
4850 case TYPE_SCALAR:
4851 oprintf (of, "scalar");
4852 break;
4853 case TYPE_POINTER:
4854 output_typename (of, t->u.p);
4855 break;
4856 case TYPE_STRUCT:
4857 case TYPE_USER_STRUCT:
4858 case TYPE_UNION:
4859 case TYPE_LANG_STRUCT:
4860 oprintf (of, "%s", t->u.s.tag);
4861 break;
4862 case TYPE_PARAM_STRUCT:
4864 int i;
4865 for (i = 0; i < NUM_PARAM; i++)
4866 if (t->u.param_struct.param[i] != NULL)
4868 output_typename (of, t->u.param_struct.param[i]);
4869 oprintf (of, "_");
4871 output_typename (of, t->u.param_struct.stru);
4872 break;
4874 default:
4875 gcc_unreachable ();
4879 /* Writes a typed GC allocator for type S that is suitable as a callback for
4880 the splay tree implementation in libiberty. */
4882 static void
4883 write_splay_tree_allocator_def (const_type_p s)
4885 outf_p of = get_output_file_with_visibility (NULL);
4886 oprintf (of, "void * ggc_alloc_splay_tree_");
4887 output_typename (of, s);
4888 oprintf (of, " (int sz, void * nl)\n");
4889 oprintf (of, "{\n");
4890 oprintf (of, " return ggc_splay_alloc (");
4891 oprintf (of, "gt_e_");
4892 output_mangled_typename (of, s);
4893 oprintf (of, ", sz, nl);\n");
4894 oprintf (of, "}\n\n");
4897 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4898 for the splay tree implementation in libiberty. */
4900 static void
4901 write_splay_tree_allocators (const_type_p param_structs)
4903 const_type_p s;
4905 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4906 for (s = param_structs; s; s = s->next)
4907 if (s->gc_used == GC_POINTED_TO)
4909 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4910 output_typename (header_file, s);
4911 oprintf (header_file, " (int, void *);\n");
4912 write_splay_tree_allocator_def (s);
4916 #define INDENT 2
4918 /* Dumps the value of typekind KIND. */
4920 static void
4921 dump_typekind (int indent, enum typekind kind)
4923 printf ("%*ckind = ", indent, ' ');
4924 switch (kind)
4926 case TYPE_SCALAR:
4927 printf ("TYPE_SCALAR");
4928 break;
4929 case TYPE_STRING:
4930 printf ("TYPE_STRING");
4931 break;
4932 case TYPE_STRUCT:
4933 printf ("TYPE_STRUCT");
4934 break;
4935 case TYPE_USER_STRUCT:
4936 printf ("TYPE_USER_STRUCT");
4937 break;
4938 case TYPE_UNION:
4939 printf ("TYPE_UNION");
4940 break;
4941 case TYPE_POINTER:
4942 printf ("TYPE_POINTER");
4943 break;
4944 case TYPE_ARRAY:
4945 printf ("TYPE_ARRAY");
4946 break;
4947 case TYPE_LANG_STRUCT:
4948 printf ("TYPE_LANG_STRUCT");
4949 break;
4950 case TYPE_PARAM_STRUCT:
4951 printf ("TYPE_PARAM_STRUCT");
4952 break;
4953 default:
4954 gcc_unreachable ();
4956 printf ("\n");
4959 /* Dumps the value of GC_USED flag. */
4961 static void
4962 dump_gc_used (int indent, enum gc_used_enum gc_used)
4964 printf ("%*cgc_used = ", indent, ' ');
4965 switch (gc_used)
4967 case GC_UNUSED:
4968 printf ("GC_UNUSED");
4969 break;
4970 case GC_USED:
4971 printf ("GC_USED");
4972 break;
4973 case GC_MAYBE_POINTED_TO:
4974 printf ("GC_MAYBE_POINTED_TO");
4975 break;
4976 case GC_POINTED_TO:
4977 printf ("GC_POINTED_TO");
4978 break;
4979 default:
4980 gcc_unreachable ();
4982 printf ("\n");
4985 /* Dumps the type options OPT. */
4987 static void
4988 dump_options (int indent, options_p opt)
4990 options_p o;
4991 printf ("%*coptions = ", indent, ' ');
4992 o = opt;
4993 while (o)
4995 switch (o->kind)
4997 case OPTION_STRING:
4998 printf ("%s:string %s ", o->name, o->info.string);
4999 break;
5000 case OPTION_TYPE:
5001 printf ("%s:type ", o->name);
5002 dump_type (indent+1, o->info.type);
5003 break;
5004 case OPTION_NESTED:
5005 printf ("%s:nested ", o->name);
5006 break;
5007 case OPTION_NONE:
5008 gcc_unreachable ();
5010 o = o->next;
5012 printf ("\n");
5015 /* Dumps the source file location in LINE. */
5017 static void
5018 dump_fileloc (int indent, struct fileloc line)
5020 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5021 get_input_file_name (line.file),
5022 line.line);
5025 /* Recursively dumps the struct, union, or a language-specific
5026 struct T. */
5028 static void
5029 dump_type_u_s (int indent, type_p t)
5031 pair_p fields;
5033 gcc_assert (union_or_struct_p (t));
5034 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5035 dump_fileloc (indent, t->u.s.line);
5036 printf ("%*cu.s.fields =\n", indent, ' ');
5037 fields = t->u.s.fields;
5038 while (fields)
5040 dump_pair (indent + INDENT, fields);
5041 fields = fields->next;
5043 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5044 dump_options (indent, t->u.s.opt);
5045 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5046 if (t->kind == TYPE_LANG_STRUCT)
5048 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5049 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5053 /* Recursively dumps the array T. */
5055 static void
5056 dump_type_u_a (int indent, type_p t)
5058 gcc_assert (t->kind == TYPE_ARRAY);
5059 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5060 dump_type_list (indent + INDENT, t->u.a.p);
5063 /* Recursively dumps the parameterized struct T. */
5065 static void
5066 dump_type_u_param_struct (int indent, type_p t)
5068 int i;
5069 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5070 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5071 dump_type_list (indent, t->u.param_struct.stru);
5072 dump_fileloc (indent, t->u.param_struct.line);
5073 for (i = 0; i < NUM_PARAM; i++)
5075 if (t->u.param_struct.param[i] == NULL)
5076 continue;
5077 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5078 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5082 /* Recursively dumps the type list T. */
5084 static void
5085 dump_type_list (int indent, type_p t)
5087 type_p p = t;
5088 while (p)
5090 dump_type (indent, p);
5091 p = p->next;
5095 static htab_t seen_types;
5097 /* Recursively dumps the type T if it was not dumped previously. */
5099 static void
5100 dump_type (int indent, type_p t)
5102 PTR *slot;
5104 if (seen_types == NULL)
5105 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5107 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5108 slot = htab_find_slot (seen_types, t, INSERT);
5109 if (*slot != NULL)
5111 printf ("already seen.\n");
5112 return;
5114 *slot = t;
5115 printf ("\n");
5117 dump_typekind (indent, t->kind);
5118 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5119 (void *) t->pointer_to);
5120 dump_gc_used (indent + INDENT, t->gc_used);
5121 switch (t->kind)
5123 case TYPE_SCALAR:
5124 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5125 t->u.scalar_is_char ? "true" : "false");
5126 break;
5127 case TYPE_STRING:
5128 break;
5129 case TYPE_STRUCT:
5130 case TYPE_UNION:
5131 case TYPE_LANG_STRUCT:
5132 case TYPE_USER_STRUCT:
5133 dump_type_u_s (indent + INDENT, t);
5134 break;
5135 case TYPE_POINTER:
5136 printf ("%*cp:\n", indent + INDENT, ' ');
5137 dump_type (indent + INDENT, t->u.p);
5138 break;
5139 case TYPE_ARRAY:
5140 dump_type_u_a (indent + INDENT, t);
5141 break;
5142 case TYPE_PARAM_STRUCT:
5143 dump_type_u_param_struct (indent + INDENT, t);
5144 break;
5145 default:
5146 gcc_unreachable ();
5148 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5151 /* Dumps the pair P. */
5153 static void
5154 dump_pair (int indent, pair_p p)
5156 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5157 dump_type (indent, p->type);
5158 dump_fileloc (indent, p->line);
5159 dump_options (indent, p->opt);
5160 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5163 /* Dumps the list of pairs PP. */
5165 static void
5166 dump_pair_list (const char *name, pair_p pp)
5168 pair_p p;
5169 printf ("%s:\n", name);
5170 for (p = pp; p != NULL; p = p->next)
5171 dump_pair (0, p);
5172 printf ("End of %s\n\n", name);
5175 /* Dumps the STRUCTURES. */
5177 static void
5178 dump_structures (const char *name, type_p structures)
5180 printf ("%s:\n", name);
5181 dump_type_list (0, structures);
5182 printf ("End of %s\n\n", name);
5185 /* Dumps the internal structures of gengtype. This is useful to debug
5186 gengtype itself, or to understand what it does, e.g. for plugin
5187 developers. */
5189 static void
5190 dump_everything (void)
5192 dump_pair_list ("typedefs", typedefs);
5193 dump_structures ("structures", structures);
5194 dump_structures ("param_structs", param_structs);
5195 dump_pair_list ("variables", variables);
5197 /* Allocated with the first call to dump_type. */
5198 htab_delete (seen_types);
5203 /* Option specification for getopt_long. */
5204 static const struct option gengtype_long_options[] = {
5205 {"help", no_argument, NULL, 'h'},
5206 {"version", no_argument, NULL, 'V'},
5207 {"verbose", no_argument, NULL, 'v'},
5208 {"dump", no_argument, NULL, 'd'},
5209 {"debug", no_argument, NULL, 'D'},
5210 {"plugin", required_argument, NULL, 'P'},
5211 {"srcdir", required_argument, NULL, 'S'},
5212 {"backupdir", required_argument, NULL, 'B'},
5213 {"inputs", required_argument, NULL, 'I'},
5214 {"read-state", required_argument, NULL, 'r'},
5215 {"write-state", required_argument, NULL, 'w'},
5216 /* Terminating NULL placeholder. */
5217 {NULL, no_argument, NULL, 0},
5221 static void
5222 print_usage (void)
5224 printf ("Usage: %s\n", progname);
5225 printf ("\t -h | --help " " \t# Give this help.\n");
5226 printf ("\t -D | --debug "
5227 " \t# Give debug output to debug %s itself.\n", progname);
5228 printf ("\t -V | --version " " \t# Give version information.\n");
5229 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5230 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5231 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5232 " \t# Generate for plugin.\n");
5233 printf ("\t -S | --srcdir <GCC-directory> "
5234 " \t# Specify the GCC source directory.\n");
5235 printf ("\t -B | --backupdir <directory> "
5236 " \t# Specify the backup directory for updated files.\n");
5237 printf ("\t -I | --inputs <input-list> "
5238 " \t# Specify the file with source files list.\n");
5239 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5240 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5243 static void
5244 print_version (void)
5246 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5247 printf ("Report bugs: %s\n", bug_report_url);
5250 /* Parse the program options using getopt_long... */
5251 static void
5252 parse_program_options (int argc, char **argv)
5254 int opt = -1;
5255 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5256 gengtype_long_options, NULL)) >= 0)
5258 switch (opt)
5260 case 'h': /* --help */
5261 print_usage ();
5262 break;
5263 case 'V': /* --version */
5264 print_version ();
5265 break;
5266 case 'd': /* --dump */
5267 do_dump = 1;
5268 break;
5269 case 'D': /* --debug */
5270 do_debug = 1;
5271 break;
5272 case 'v': /* --verbose */
5273 verbosity_level++;
5274 break;
5275 case 'P': /* --plugin */
5276 if (optarg)
5277 plugin_output_filename = optarg;
5278 else
5279 fatal ("missing plugin output file name");
5280 break;
5281 case 'S': /* --srcdir */
5282 if (optarg)
5283 srcdir = optarg;
5284 else
5285 fatal ("missing source directory");
5286 srcdir_len = strlen (srcdir);
5287 break;
5288 case 'B': /* --backupdir */
5289 if (optarg)
5290 backup_dir = optarg;
5291 else
5292 fatal ("missing backup directory");
5293 break;
5294 case 'I': /* --inputs */
5295 if (optarg)
5296 inputlist = optarg;
5297 else
5298 fatal ("missing input list");
5299 break;
5300 case 'r': /* --read-state */
5301 if (optarg)
5302 read_state_filename = optarg;
5303 else
5304 fatal ("missing read state file");
5305 DBGPRINTF ("read state %s\n", optarg);
5306 break;
5307 case 'w': /* --write-state */
5308 DBGPRINTF ("write state %s\n", optarg);
5309 if (optarg)
5310 write_state_filename = optarg;
5311 else
5312 fatal ("missing write state file");
5313 break;
5314 default:
5315 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5316 print_usage ();
5317 fatal ("unexpected flag");
5320 if (plugin_output_filename)
5322 /* In plugin mode we require some input files. */
5323 int i = 0;
5324 if (optind >= argc)
5325 fatal ("no source files given in plugin mode");
5326 nb_plugin_files = argc - optind;
5327 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5328 for (i = 0; i < (int) nb_plugin_files; i++)
5330 char *name = argv[i + optind];
5331 plugin_files[i] = input_file_by_name (name);
5338 /******* Manage input files. ******/
5340 /* Hash table of unique input file names. */
5341 static htab_t input_file_htab;
5343 /* Find or allocate a new input_file by hash-consing it. */
5344 input_file*
5345 input_file_by_name (const char* name)
5347 PTR* slot;
5348 input_file* f = NULL;
5349 int namlen = 0;
5350 if (!name)
5351 return NULL;
5352 namlen = strlen (name);
5353 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5354 f->inpbitmap = 0;
5355 f->inpoutf = NULL;
5356 f->inpisplugin = false;
5357 strcpy (f->inpname, name);
5358 slot = htab_find_slot (input_file_htab, f, INSERT);
5359 gcc_assert (slot != NULL);
5360 if (*slot)
5362 /* Already known input file. */
5363 free (f);
5364 return (input_file*)(*slot);
5366 /* New input file. */
5367 *slot = f;
5368 return f;
5371 /* Hash table support routines for input_file-s. */
5372 static hashval_t
5373 htab_hash_inputfile (const void *p)
5375 const input_file *inpf = (const input_file *) p;
5376 gcc_assert (inpf);
5377 return htab_hash_string (get_input_file_name (inpf));
5380 static int
5381 htab_eq_inputfile (const void *x, const void *y)
5383 const input_file *inpfx = (const input_file *) x;
5384 const input_file *inpfy = (const input_file *) y;
5385 gcc_assert (inpfx != NULL && inpfy != NULL);
5386 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5391 main (int argc, char **argv)
5393 size_t i;
5394 static struct fileloc pos = { NULL, 0 };
5395 outf_p output_header;
5397 /* Mandatory common initializations. */
5398 progname = "gengtype"; /* For fatal and messages. */
5399 /* Create the hash-table used to hash-cons input files. */
5400 input_file_htab =
5401 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5402 /* Initialize our special input files. */
5403 this_file = input_file_by_name (__FILE__);
5404 system_h_file = input_file_by_name ("system.h");
5405 /* Set the scalar_is_char union number for predefined scalar types. */
5406 scalar_nonchar.u.scalar_is_char = FALSE;
5407 scalar_char.u.scalar_is_char = TRUE;
5409 parse_program_options (argc, argv);
5411 #if ENABLE_CHECKING
5412 if (do_debug)
5414 time_t now = (time_t) 0;
5415 time (&now);
5416 DBGPRINTF ("gengtype started pid %d at %s",
5417 (int) getpid (), ctime (&now));
5419 #endif /* ENABLE_CHECKING */
5421 /* Parse the input list and the input files. */
5422 DBGPRINTF ("inputlist %s", inputlist);
5423 if (read_state_filename)
5425 if (inputlist)
5426 fatal ("input list %s cannot be given with a read state file %s",
5427 inputlist, read_state_filename);
5428 read_state (read_state_filename);
5429 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5430 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5432 else if (inputlist)
5434 /* These types are set up with #define or else outside of where
5435 we can see them. We should initialize them before calling
5436 read_input_list. */
5437 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5438 Call;} while(0)
5439 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5440 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5441 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5442 POS_HERE (do_scalar_typedef ("double_int", &pos));
5443 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5444 POS_HERE (do_scalar_typedef ("uint8", &pos));
5445 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5446 POS_HERE (do_scalar_typedef ("jword", &pos));
5447 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5448 POS_HERE (do_scalar_typedef ("void", &pos));
5449 POS_HERE (do_typedef ("PTR",
5450 create_pointer (resolve_typedef ("void", &pos)),
5451 &pos));
5452 #undef POS_HERE
5453 read_input_list (inputlist);
5454 for (i = 0; i < num_gt_files; i++)
5456 parse_file (get_input_file_name (gt_files[i]));
5457 DBGPRINTF ("parsed file #%d %s",
5458 (int) i, get_input_file_name (gt_files[i]));
5460 if (verbosity_level >= 1)
5461 printf ("%s parsed %d files with %d GTY types\n",
5462 progname, (int) num_gt_files, type_count);
5464 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5465 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5468 else
5469 fatal ("either an input list or a read state file should be given");
5470 if (hit_error)
5471 return 1;
5474 if (plugin_output_filename)
5476 size_t ix = 0;
5477 /* In plugin mode, we should have read a state file, and have
5478 given at least one plugin file. */
5479 if (!read_state_filename)
5480 fatal ("No read state given in plugin mode for %s",
5481 plugin_output_filename);
5483 if (nb_plugin_files == 0 || !plugin_files)
5484 fatal ("No plugin files given in plugin mode for %s",
5485 plugin_output_filename);
5487 /* Parse our plugin files and augment the state. */
5488 for (ix = 0; ix < nb_plugin_files; ix++)
5490 input_file* pluginput = plugin_files [ix];
5491 pluginput->inpisplugin = true;
5492 parse_file (get_input_file_name (pluginput));
5494 if (hit_error)
5495 return 1;
5497 plugin_output = create_file ("GCC", plugin_output_filename);
5498 DBGPRINTF ("created plugin_output %p named %s",
5499 (void *) plugin_output, plugin_output->name);
5501 else
5502 { /* No plugin files, we are in normal mode. */
5503 if (!srcdir)
5504 fatal ("gengtype needs a source directory in normal mode");
5506 if (hit_error)
5507 return 1;
5509 gen_rtx_next ();
5511 /* The call to set_gc_used may indirectly call find_param_structure
5512 hence enlarge the param_structs list of types. */
5513 set_gc_used (variables);
5515 /* The state at this point is read from the state input file or by
5516 parsing source files and optionally augmented by parsing plugin
5517 source files. Write it now. */
5518 if (write_state_filename)
5520 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5521 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5523 if (hit_error)
5524 fatal ("didn't write state file %s after errors",
5525 write_state_filename);
5527 DBGPRINTF ("before write_state %s", write_state_filename);
5528 write_state (write_state_filename);
5530 if (do_dump)
5531 dump_everything ();
5533 /* After having written the state file we return immediately to
5534 avoid generating any output file. */
5535 if (hit_error)
5536 return 1;
5537 else
5538 return 0;
5542 open_base_files ();
5544 write_enum_defn (structures, param_structs);
5545 output_header = plugin_output ? plugin_output : header_file;
5546 write_typed_alloc_defns (output_header, structures, typedefs);
5547 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5548 structures);
5549 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5550 param_structs);
5552 write_types (output_header, structures, param_structs, &ggc_wtd);
5553 if (plugin_files == NULL)
5555 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5556 structures);
5557 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5558 param_structs);
5559 write_types (header_file, structures, param_structs, &pch_wtd);
5560 write_local (header_file, structures, param_structs);
5562 write_splay_tree_allocators (param_structs);
5563 write_roots (variables, plugin_files == NULL);
5564 write_rtx_next ();
5565 close_output_files ();
5567 if (do_dump)
5568 dump_everything ();
5570 /* Don't bother about free-ing any input or plugin file, etc. */
5572 if (hit_error)
5573 return 1;
5574 return 0;