In gcc/: 2011-03-21 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / gengtype.c
blob7127a2a9e95e8f4d42fae32a2ecd3fdabb66593e
1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "bconfig.h"
22 #include "system.h"
23 #include "errors.h" /* for fatal */
24 #include "getopt.h"
25 #include "double-int.h"
26 #include "version.h" /* for version_string & pkgversion_string. */
27 #include "hashtab.h"
28 #include "xregex.h"
29 #include "obstack.h"
30 #include "gengtype.h"
32 /* Data types, macros, etc. used only in this file. */
35 /* The list of output files. */
36 outf_p output_files;
38 /* The output header file that is included into pretty much every
39 source file. */
40 outf_p header_file;
43 /* The name of the file containing the list of input files. */
44 static char *inputlist;
46 /* The plugin input files and their number; in that case only
47 a single file is produced. */
48 static input_file **plugin_files;
49 static size_t nb_plugin_files;
51 /* The generated plugin output file and name. */
52 static outf_p plugin_output;
53 static char *plugin_output_filename;
55 /* Our source directory and its length. */
56 const char *srcdir;
57 size_t srcdir_len;
59 /* Variables used for reading and writing the state. */
60 const char *read_state_filename;
61 const char *write_state_filename;
63 /* Variables to help debugging. */
64 int do_dump;
65 int do_debug;
67 /* Level for verbose messages. */
68 int verbosity_level;
70 /* We have a type count and use it to set the state_number of newly
71 allocated types to some unique negative number. */
72 static int type_count;
74 /* The backup directory should be in the same file system as the
75 generated files, otherwise the rename(2) system call would fail.
76 If NULL, no backup is made when overwriting a generated file. */
77 static const char* backup_dir; /* (-B) program option. */
80 static outf_p create_file (const char *, const char *);
82 static const char *get_file_basename (const input_file *);
83 static const char *get_file_realbasename (const input_file *);
85 static int get_prefix_langdir_index (const char *);
86 static const char *get_file_langdir (const input_file *);
89 /* Nonzero iff an error has occurred. */
90 bool hit_error = false;
92 static void gen_rtx_next (void);
93 static void write_rtx_next (void);
94 static void open_base_files (void);
95 static void close_output_files (void);
97 /* Report an error at POS, printing MSG. */
99 void
100 error_at_line (const struct fileloc *pos, const char *msg, ...)
102 va_list ap;
104 gcc_assert (pos != NULL && pos->file != NULL);
105 va_start (ap, msg);
107 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
108 vfprintf (stderr, msg, ap);
109 fputc ('\n', stderr);
110 hit_error = true;
112 va_end (ap);
115 /* asprintf, but produces fatal message on out-of-memory. */
116 char *
117 xasprintf (const char *format, ...)
119 int n;
120 char *result;
121 va_list ap;
123 va_start (ap, format);
124 n = vasprintf (&result, format, ap);
125 if (result == NULL || n < 0)
126 fatal ("out of memory");
127 va_end (ap);
129 return result;
132 /* Input file handling. */
134 /* Table of all input files. */
135 const input_file **gt_files;
136 size_t num_gt_files;
138 /* A number of places use the name of this "gengtype.c" file for a
139 location for things that we can't rely on the source to define.
140 Make sure we can still use pointer comparison on filenames. */
141 input_file* this_file;
142 /* The "system.h" file is likewise specially useful. */
143 input_file* system_h_file;
145 /* Vector of per-language directories. */
146 const char **lang_dir_names;
147 size_t num_lang_dirs;
149 /* An array of output files suitable for definitions. There is one
150 BASE_FILES entry for each language. */
151 static outf_p *base_files;
155 #if ENABLE_CHECKING
156 /* Utility debugging function, printing the various type counts within
157 a list of types. Called thru the DBGPRINT_COUNT_TYPE macro. */
158 void
159 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
161 int nb_types = 0, nb_scalar = 0, nb_string = 0;
162 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
163 int nb_lang_struct = 0, nb_param_struct = 0;
164 type_p p = NULL;
165 for (p = t; p; p = p->next)
167 nb_types++;
168 switch (p->kind)
170 case TYPE_SCALAR:
171 nb_scalar++;
172 break;
173 case TYPE_STRING:
174 nb_string++;
175 break;
176 case TYPE_STRUCT:
177 nb_struct++;
178 break;
179 case TYPE_UNION:
180 nb_union++;
181 break;
182 case TYPE_POINTER:
183 nb_pointer++;
184 break;
185 case TYPE_ARRAY:
186 nb_array++;
187 break;
188 case TYPE_LANG_STRUCT:
189 nb_lang_struct++;
190 break;
191 case TYPE_PARAM_STRUCT:
192 nb_param_struct++;
193 break;
194 default:
195 gcc_unreachable ();
198 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
199 lbasename (fil), lin, msg, nb_types);
200 if (nb_scalar > 0 || nb_string > 0)
201 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
202 if (nb_struct > 0 || nb_union > 0)
203 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
204 if (nb_pointer > 0 || nb_array > 0)
205 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
206 if (nb_lang_struct > 0 || nb_param_struct > 0)
207 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
208 nb_lang_struct, nb_param_struct);
209 fprintf (stderr, "\n");
211 #endif /* ENABLE_CHECKING */
213 /* Scan the input file, LIST, and determine how much space we need to
214 store strings in. Also, count the number of language directories
215 and files. The numbers returned are overestimates as they does not
216 consider repeated files. */
217 static size_t
218 measure_input_list (FILE *list)
220 size_t n = 0;
221 int c;
222 bool atbol = true;
223 num_lang_dirs = 0;
224 num_gt_files = plugin_files ? nb_plugin_files : 0;
225 while ((c = getc (list)) != EOF)
227 n++;
228 if (atbol)
230 if (c == '[')
231 num_lang_dirs++;
232 else
234 /* Add space for a lang_bitmap before the input file name. */
235 n += sizeof (lang_bitmap);
236 num_gt_files++;
238 atbol = false;
241 if (c == '\n')
242 atbol = true;
245 rewind (list);
246 return n;
249 /* Read one input line from LIST to HEREP (which is updated). A
250 pointer to the string is returned via LINEP. If it was a language
251 subdirectory in square brackets, strip off the square brackets and
252 return true. Otherwise, leave space before the string for a
253 lang_bitmap, and return false. At EOF, returns false, does not
254 touch *HEREP, and sets *LINEP to NULL. POS is used for
255 diagnostics. */
256 static bool
257 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
259 char *here = *herep;
260 char *line;
261 int c = getc (list);
263 /* Read over whitespace. */
264 while (c == '\n' || c == ' ')
265 c = getc (list);
267 if (c == EOF)
269 *linep = 0;
270 return false;
272 else if (c == '[')
274 /* No space for a lang_bitmap is necessary. Discard the '['. */
275 c = getc (list);
276 line = here;
277 while (c != ']' && c != '\n' && c != EOF)
279 *here++ = c;
280 c = getc (list);
282 *here++ = '\0';
284 if (c == ']')
286 c = getc (list); /* eat what should be a newline */
287 if (c != '\n' && c != EOF)
288 error_at_line (pos, "junk on line after language tag [%s]", line);
290 else
291 error_at_line (pos, "missing close bracket for language tag [%s",
292 line);
294 *herep = here;
295 *linep = line;
296 return true;
298 else
300 /* Leave space for a lang_bitmap. */
301 memset (here, 0, sizeof (lang_bitmap));
302 here += sizeof (lang_bitmap);
303 line = here;
306 *here++ = c;
307 c = getc (list);
309 while (c != EOF && c != '\n');
310 *here++ = '\0';
311 *herep = here;
312 *linep = line;
313 return false;
317 /* Read the list of input files from LIST and compute all of the
318 relevant tables. There is one file per line of the list. At
319 first, all the files on the list are language-generic, but
320 eventually a line will appear which is the name of a language
321 subdirectory in square brackets, like this: [cp]. All subsequent
322 files are specific to that language, until another language
323 subdirectory tag appears. Files can appear more than once, if
324 they apply to more than one language. */
325 static void
326 read_input_list (const char *listname)
328 FILE *list = fopen (listname, "r");
329 if (!list)
330 fatal ("cannot open %s: %s", listname, xstrerror (errno));
331 else
333 struct fileloc epos;
334 size_t bufsz = measure_input_list (list);
335 char *buf = XNEWVEC (char, bufsz);
336 char *here = buf;
337 char *committed = buf;
338 char *limit = buf + bufsz;
339 char *line;
340 bool is_language;
341 size_t langno = 0;
342 size_t nfiles = 0;
343 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
345 epos.file = input_file_by_name (listname);
346 epos.line = 0;
348 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
349 gt_files = XNEWVEC (const input_file *, num_gt_files);
351 for (;;)
353 next_line:
354 epos.line++;
355 committed = here;
356 is_language = read_input_line (list, &here, &line, &epos);
357 gcc_assert (here <= limit);
358 if (line == 0)
359 break;
360 else if (is_language)
362 size_t i;
363 gcc_assert (langno <= num_lang_dirs);
364 for (i = 0; i < langno; i++)
365 if (strcmp (lang_dir_names[i], line) == 0)
367 error_at_line (&epos, "duplicate language tag [%s]",
368 line);
369 curlangs = 1 << i;
370 here = committed;
371 goto next_line;
374 curlangs = 1 << langno;
375 lang_dir_names[langno++] = line;
377 else
379 size_t i;
380 input_file *inpf = input_file_by_name (line);
381 gcc_assert (nfiles <= num_gt_files);
382 for (i = 0; i < nfiles; i++)
383 /* Since the input_file-s are uniquely hash-consed, we
384 can just compare pointers! */
385 if (gt_files[i] == inpf)
387 /* Throw away the string we just read, and add the
388 current language to the existing string's bitmap. */
389 lang_bitmap bmap = get_lang_bitmap (inpf);
390 if (bmap & curlangs)
391 error_at_line (&epos,
392 "file %s specified more than once "
393 "for language %s", line,
394 langno ==
395 0 ? "(all)" : lang_dir_names[langno -
396 1]);
398 bmap |= curlangs;
399 set_lang_bitmap (inpf, bmap);
400 here = committed;
401 goto next_line;
404 set_lang_bitmap (inpf, curlangs);
405 gt_files[nfiles++] = inpf;
408 /* Update the global counts now that we know accurately how many
409 things there are. (We do not bother resizing the arrays down.) */
410 num_lang_dirs = langno;
411 /* Add the plugin files if provided. */
412 if (plugin_files)
414 size_t i;
415 for (i = 0; i < nb_plugin_files; i++)
416 gt_files[nfiles++] = plugin_files[i];
418 num_gt_files = nfiles;
421 /* Sanity check: any file that resides in a language subdirectory
422 (e.g. 'cp') ought to belong to the corresponding language.
423 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
424 (Can you even do that? Should you be allowed to?) */
426 size_t f;
427 for (f = 0; f < num_gt_files; f++)
429 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
430 const char *basename = get_file_basename (gt_files[f]);
431 const char *slashpos = strchr (basename, '/');
433 if (slashpos)
435 size_t l;
436 for (l = 0; l < num_lang_dirs; l++)
437 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
438 && memcmp (basename, lang_dir_names[l],
439 strlen (lang_dir_names[l])) == 0)
441 if (!(bitmap & (1 << l)))
442 error ("%s is in language directory '%s' but is not "
443 "tagged for that language",
444 basename, lang_dir_names[l]);
445 break;
451 if (ferror (list))
452 fatal ("error reading %s: %s", listname, xstrerror (errno));
454 fclose (list);
459 /* The one and only TYPE_STRING. */
461 struct type string_type = {
462 TYPE_STRING, 0, 0, 0, GC_USED, {0}
465 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
466 set early in main. */
468 struct type scalar_nonchar = {
469 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
472 struct type scalar_char = {
473 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
476 /* Lists of various things. */
478 pair_p typedefs;
479 type_p structures;
480 type_p param_structs;
481 pair_p variables;
483 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
484 static type_p adjust_field_tree_exp (type_p t, options_p opt);
485 static type_p adjust_field_rtx_def (type_p t, options_p opt);
487 /* Define S as a typedef to T at POS. */
489 void
490 do_typedef (const char *s, type_p t, struct fileloc *pos)
492 pair_p p;
494 /* temporary kludge - gengtype doesn't handle conditionals or
495 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
496 is coming from this file (main() sets them up with safe dummy
497 definitions). */
498 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
499 return;
501 for (p = typedefs; p != NULL; p = p->next)
502 if (strcmp (p->name, s) == 0)
504 if (p->type != t)
506 error_at_line (pos, "type `%s' previously defined", s);
507 error_at_line (&p->line, "previously defined here");
509 return;
512 p = XNEW (struct pair);
513 p->next = typedefs;
514 p->name = s;
515 p->type = t;
516 p->line = *pos;
517 p->opt = NULL;
518 typedefs = p;
521 /* Define S as a typename of a scalar. Cannot be used to define
522 typedefs of 'char'. Note: is also used for pointer-to-function
523 typedefs (which are therefore not treated as pointers). */
525 void
526 do_scalar_typedef (const char *s, struct fileloc *pos)
528 do_typedef (s, &scalar_nonchar, pos);
531 /* Return the type previously defined for S. Use POS to report errors. */
533 type_p
534 resolve_typedef (const char *s, struct fileloc *pos)
536 pair_p p;
537 for (p = typedefs; p != NULL; p = p->next)
538 if (strcmp (p->name, s) == 0)
539 return p->type;
540 error_at_line (pos, "unidentified type `%s'", s);
541 return &scalar_nonchar; /* treat as "int" */
544 /* Create and return a new structure with tag NAME (or a union iff
545 ISUNION is nonzero), at POS with fields FIELDS and options O. */
547 type_p
548 new_structure (const char *name, int isunion, struct fileloc *pos,
549 pair_p fields, options_p o)
551 type_p si;
552 type_p s = NULL;
553 lang_bitmap bitmap = get_lang_bitmap (pos->file);
555 for (si = structures; si != NULL; si = si->next)
556 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
558 type_p ls = NULL;
559 if (si->kind == TYPE_LANG_STRUCT)
561 ls = si;
563 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
564 if (si->u.s.bitmap == bitmap)
565 s = si;
567 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
569 ls = si;
570 type_count++;
571 si = XCNEW (struct type);
572 memcpy (si, ls, sizeof (struct type));
573 ls->kind = TYPE_LANG_STRUCT;
574 ls->u.s.lang_struct = si;
575 ls->u.s.fields = NULL;
576 si->next = NULL;
577 si->state_number = -type_count;
578 si->pointer_to = NULL;
579 si->u.s.lang_struct = ls;
581 else
582 s = si;
584 if (ls != NULL && s == NULL)
586 type_count++;
587 s = XCNEW (struct type);
588 s->state_number = -type_count;
589 s->next = ls->u.s.lang_struct;
590 ls->u.s.lang_struct = s;
591 s->u.s.lang_struct = ls;
593 break;
596 if (s == NULL)
598 type_count++;
599 s = XCNEW (struct type);
600 s->state_number = -type_count;
601 s->next = structures;
602 structures = s;
605 if (s->u.s.line.file != NULL
606 || (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap)))
608 error_at_line (pos, "duplicate definition of '%s %s'",
609 isunion ? "union" : "struct", s->u.s.tag);
610 error_at_line (&s->u.s.line, "previous definition here");
613 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
614 s->u.s.tag = name;
615 s->u.s.line = *pos;
616 s->u.s.fields = fields;
617 s->u.s.opt = o;
618 s->u.s.bitmap = bitmap;
619 if (s->u.s.lang_struct)
620 s->u.s.lang_struct->u.s.bitmap |= bitmap;
622 return s;
625 /* Return the previously-defined structure with tag NAME (or a union
626 iff ISUNION is nonzero), or a new empty structure or union if none
627 was defined previously. */
629 type_p
630 find_structure (const char *name, int isunion)
632 type_p s;
634 for (s = structures; s != NULL; s = s->next)
635 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
636 return s;
638 type_count++;
639 s = XCNEW (struct type);
640 s->next = structures;
641 s->state_number = -type_count;
642 structures = s;
643 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
644 s->u.s.tag = name;
645 structures = s;
646 return s;
649 /* Return the previously-defined parameterized structure for structure
650 T and parameters PARAM, or a new parameterized empty structure or
651 union if none was defined previously. */
653 static type_p
654 find_param_structure (type_p t, type_p param[NUM_PARAM])
656 type_p res;
658 for (res = param_structs; res; res = res->next)
659 if (res->u.param_struct.stru == t
660 && memcmp (res->u.param_struct.param, param,
661 sizeof (type_p) * NUM_PARAM) == 0)
662 break;
663 if (res == NULL)
665 type_count++;
666 res = XCNEW (struct type);
667 res->kind = TYPE_PARAM_STRUCT;
668 res->next = param_structs;
669 res->state_number = -type_count;
670 param_structs = res;
671 res->u.param_struct.stru = t;
672 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
674 return res;
677 /* Return a scalar type with name NAME. */
679 type_p
680 create_scalar_type (const char *name)
682 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
683 return &scalar_char;
684 else
685 return &scalar_nonchar;
688 /* Return a pointer to T. */
690 type_p
691 create_pointer (type_p t)
693 if (!t->pointer_to)
695 type_p r = XCNEW (struct type);
696 type_count++;
697 r->state_number = -type_count;
698 r->kind = TYPE_POINTER;
699 r->u.p = t;
700 t->pointer_to = r;
702 return t->pointer_to;
705 /* Return an array of length LEN. */
707 type_p
708 create_array (type_p t, const char *len)
710 type_p v;
712 type_count++;
713 v = XCNEW (struct type);
714 v->kind = TYPE_ARRAY;
715 v->state_number = -type_count;
716 v->u.a.p = t;
717 v->u.a.len = len;
718 return v;
721 /* Return a string options structure with name NAME and info INFO.
722 NEXT is the next option in the chain. */
723 options_p
724 create_string_option (options_p next, const char *name, const char *info)
726 options_p o = XNEW (struct options);
727 o->kind = OPTION_STRING;
728 o->next = next;
729 o->name = name;
730 o->info.string = info;
731 return o;
734 /* Create a type options structure with name NAME and info INFO. NEXT
735 is the next option in the chain. */
736 options_p
737 create_type_option (options_p next, const char* name, type_p info)
739 options_p o = XNEW (struct options);
740 o->next = next;
741 o->name = name;
742 o->kind = OPTION_TYPE;
743 o->info.type = info;
744 return o;
747 /* Create a nested pointer options structure with name NAME and info
748 INFO. NEXT is the next option in the chain. */
749 options_p
750 create_nested_option (options_p next, const char* name,
751 struct nested_ptr_data* info)
753 options_p o;
754 o = XNEW (struct options);
755 o->next = next;
756 o->name = name;
757 o->kind = OPTION_NESTED;
758 o->info.nested = info;
759 return o;
762 /* Return an options structure for a "nested_ptr" option. */
763 options_p
764 create_nested_ptr_option (options_p next, type_p t,
765 const char *to, const char *from)
767 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
769 d->type = adjust_field_type (t, 0);
770 d->convert_to = to;
771 d->convert_from = from;
772 return create_nested_option (next, "nested_ptr", d);
775 /* Add a variable named S of type T with options O defined at POS,
776 to `variables'. */
777 void
778 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
780 pair_p n;
781 n = XNEW (struct pair);
782 n->name = s;
783 n->type = t;
784 n->line = *pos;
785 n->opt = o;
786 n->next = variables;
787 variables = n;
790 /* Most-general structure field creator. */
791 static pair_p
792 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
793 const input_file *inpf, int line)
795 pair_p field;
797 field = XNEW (struct pair);
798 field->next = next;
799 field->type = type;
800 field->name = name;
801 field->opt = opt;
802 field->line.file = inpf;
803 field->line.line = line;
804 return field;
807 /* Create a field that came from the source code we are scanning,
808 i.e. we have a 'struct fileloc', and possibly options; also,
809 adjust_field_type should be called. */
810 pair_p
811 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
812 struct fileloc *pos)
814 return create_field_all (next, adjust_field_type (type, opt),
815 name, opt, pos->file, pos->line);
818 /* Create a fake field with the given type and name. NEXT is the next
819 field in the chain. */
820 #define create_field(next,type,name) \
821 create_field_all(next,type,name, 0, this_file, __LINE__)
823 /* Like create_field, but the field is only valid when condition COND
824 is true. */
826 static pair_p
827 create_optional_field_ (pair_p next, type_p type, const char *name,
828 const char *cond, int line)
830 static int id = 1;
831 pair_p union_fields;
832 type_p union_type;
834 /* Create a fake union type with a single nameless field of type TYPE.
835 The field has a tag of "1". This allows us to make the presence
836 of a field of type TYPE depend on some boolean "desc" being true. */
837 union_fields = create_field (NULL, type, "");
838 union_fields->opt =
839 create_string_option (union_fields->opt, "dot", "");
840 union_fields->opt =
841 create_string_option (union_fields->opt, "tag", "1");
842 union_type =
843 new_structure (xasprintf ("%s_%d", "fake_union", id++), 1,
844 &lexer_line, union_fields, NULL);
846 /* Create the field and give it the new fake union type. Add a "desc"
847 tag that specifies the condition under which the field is valid. */
848 return create_field_all (next, union_type, name,
849 create_string_option (0, "desc", cond),
850 this_file, line);
853 #define create_optional_field(next,type,name,cond) \
854 create_optional_field_(next,type,name,cond,__LINE__)
856 /* Reverse a linked list of 'struct pair's in place. */
857 pair_p
858 nreverse_pairs (pair_p list)
860 pair_p prev = 0, p, next;
861 for (p = list; p; p = next)
863 next = p->next;
864 p->next = prev;
865 prev = p;
867 return prev;
871 /* We don't care how long a CONST_DOUBLE is. */
872 #define CONST_DOUBLE_FORMAT "ww"
873 /* We don't want to see codes that are only for generator files. */
874 #undef GENERATOR_FILE
876 enum rtx_code
878 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
879 #include "rtl.def"
880 #undef DEF_RTL_EXPR
881 NUM_RTX_CODE
884 static const char *const rtx_name[NUM_RTX_CODE] = {
885 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
886 #include "rtl.def"
887 #undef DEF_RTL_EXPR
890 static const char *const rtx_format[NUM_RTX_CODE] = {
891 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
892 #include "rtl.def"
893 #undef DEF_RTL_EXPR
896 static int rtx_next_new[NUM_RTX_CODE];
898 /* We also need codes and names for insn notes (not register notes).
899 Note that we do *not* bias the note values here. */
900 enum insn_note
902 #define DEF_INSN_NOTE(NAME) NAME,
903 #include "insn-notes.def"
904 #undef DEF_INSN_NOTE
906 NOTE_INSN_MAX
909 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
910 default field for line number notes. */
911 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
912 #define DEF_INSN_NOTE(NAME) #NAME,
913 #include "insn-notes.def"
914 #undef DEF_INSN_NOTE
917 #undef CONST_DOUBLE_FORMAT
918 #define GENERATOR_FILE
920 /* Generate the contents of the rtx_next array. This really doesn't belong
921 in gengtype at all, but it's needed for adjust_field_rtx_def. */
923 static void
924 gen_rtx_next (void)
926 int i;
927 for (i = 0; i < NUM_RTX_CODE; i++)
929 int k;
931 rtx_next_new[i] = -1;
932 if (strncmp (rtx_format[i], "iuu", 3) == 0)
933 rtx_next_new[i] = 2;
934 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
935 rtx_next_new[i] = 1;
936 else
937 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
938 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
939 rtx_next_new[i] = k;
943 /* Write out the contents of the rtx_next array. */
944 static void
945 write_rtx_next (void)
947 outf_p f = get_output_file_with_visibility (NULL);
948 int i;
949 if (!f)
950 return;
952 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
953 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
954 for (i = 0; i < NUM_RTX_CODE; i++)
955 if (rtx_next_new[i] == -1)
956 oprintf (f, " 0,\n");
957 else
958 oprintf (f,
959 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
960 oprintf (f, "};\n");
963 /* Handle `special("rtx_def")'. This is a special case for field
964 `fld' of struct rtx_def, which is an array of unions whose values
965 are based in a complex way on the type of RTL. */
967 static type_p
968 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
970 pair_p flds = NULL;
971 options_p nodot;
972 int i;
973 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
974 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
976 if (t->kind != TYPE_UNION)
978 error_at_line (&lexer_line,
979 "special `rtx_def' must be applied to a union");
980 return &string_type;
983 nodot = create_string_option (NULL, "dot", "");
985 rtx_tp = create_pointer (find_structure ("rtx_def", 0));
986 rtvec_tp = create_pointer (find_structure ("rtvec_def", 0));
987 tree_tp = create_pointer (find_structure ("tree_node", 1));
988 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", 0));
989 reg_attrs_tp =
990 create_pointer (find_structure ("reg_attrs", 0));
991 basic_block_tp =
992 create_pointer (find_structure ("basic_block_def", 0));
993 constant_tp =
994 create_pointer (find_structure ("constant_descriptor_rtx", 0));
995 scalar_tp = &scalar_nonchar; /* rtunion int */
998 pair_p note_flds = NULL;
999 int c;
1001 for (c = 0; c <= NOTE_INSN_MAX; c++)
1003 switch (c)
1005 case NOTE_INSN_MAX:
1006 case NOTE_INSN_DELETED_LABEL:
1007 note_flds = create_field (note_flds, &string_type, "rt_str");
1008 break;
1010 case NOTE_INSN_BLOCK_BEG:
1011 case NOTE_INSN_BLOCK_END:
1012 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1013 break;
1015 case NOTE_INSN_VAR_LOCATION:
1016 case NOTE_INSN_CALL_ARG_LOCATION:
1017 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1018 break;
1020 default:
1021 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1022 break;
1024 /* NOTE_INSN_MAX is used as the default field for line
1025 number notes. */
1026 if (c == NOTE_INSN_MAX)
1027 note_flds->opt =
1028 create_string_option (nodot, "default", "");
1029 else
1030 note_flds->opt =
1031 create_string_option (nodot, "tag", note_insn_name[c]);
1033 note_union_tp = new_structure ("rtx_def_note_subunion", 1,
1034 &lexer_line, note_flds, NULL);
1036 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1038 pair_p sym_flds;
1039 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1040 sym_flds->opt = create_string_option (nodot, "default", "");
1041 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1042 sym_flds->opt = create_string_option (nodot, "tag", "1");
1043 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", 1,
1044 &lexer_line, sym_flds, NULL);
1046 for (i = 0; i < NUM_RTX_CODE; i++)
1048 pair_p subfields = NULL;
1049 size_t aindex, nmindex;
1050 const char *sname;
1051 type_p substruct;
1052 char *ftag;
1054 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1056 type_p t;
1057 const char *subname;
1059 switch (rtx_format[i][aindex])
1061 case '*':
1062 case 'i':
1063 case 'n':
1064 case 'w':
1065 t = scalar_tp;
1066 subname = "rt_int";
1067 break;
1069 case '0':
1070 if (i == MEM && aindex == 1)
1071 t = mem_attrs_tp, subname = "rt_mem";
1072 else if (i == JUMP_INSN && aindex == 8)
1073 t = rtx_tp, subname = "rt_rtx";
1074 else if (i == CODE_LABEL && aindex == 5)
1075 t = scalar_tp, subname = "rt_int";
1076 else if (i == CODE_LABEL && aindex == 4)
1077 t = rtx_tp, subname = "rt_rtx";
1078 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1079 t = rtx_tp, subname = "rt_rtx";
1080 else if (i == NOTE && aindex == 4)
1081 t = note_union_tp, subname = "";
1082 else if (i == NOTE && aindex == 5)
1083 t = scalar_tp, subname = "rt_int";
1084 else if (i == NOTE && aindex >= 7)
1085 t = scalar_tp, subname = "rt_int";
1086 else if (i == ADDR_DIFF_VEC && aindex == 4)
1087 t = scalar_tp, subname = "rt_int";
1088 else if (i == VALUE && aindex == 0)
1089 t = scalar_tp, subname = "rt_int";
1090 else if (i == DEBUG_EXPR && aindex == 0)
1091 t = tree_tp, subname = "rt_tree";
1092 else if (i == REG && aindex == 1)
1093 t = scalar_tp, subname = "rt_int";
1094 else if (i == REG && aindex == 2)
1095 t = reg_attrs_tp, subname = "rt_reg";
1096 else if (i == SCRATCH && aindex == 0)
1097 t = scalar_tp, subname = "rt_int";
1098 else if (i == SYMBOL_REF && aindex == 1)
1099 t = scalar_tp, subname = "rt_int";
1100 else if (i == SYMBOL_REF && aindex == 2)
1101 t = symbol_union_tp, subname = "";
1102 else if (i == BARRIER && aindex >= 3)
1103 t = scalar_tp, subname = "rt_int";
1104 else if (i == ENTRY_VALUE && aindex == 0)
1105 t = rtx_tp, subname = "rt_rtx";
1106 else
1108 error_at_line
1109 (&lexer_line,
1110 "rtx type `%s' has `0' in position %lu, can't handle",
1111 rtx_name[i], (unsigned long) aindex);
1112 t = &string_type;
1113 subname = "rt_int";
1115 break;
1117 case 's':
1118 case 'S':
1119 case 'T':
1120 t = &string_type;
1121 subname = "rt_str";
1122 break;
1124 case 'e':
1125 case 'u':
1126 t = rtx_tp;
1127 subname = "rt_rtx";
1128 break;
1130 case 'E':
1131 case 'V':
1132 t = rtvec_tp;
1133 subname = "rt_rtvec";
1134 break;
1136 case 't':
1137 t = tree_tp;
1138 subname = "rt_tree";
1139 break;
1141 case 'B':
1142 t = basic_block_tp;
1143 subname = "rt_bb";
1144 break;
1146 default:
1147 error_at_line
1148 (&lexer_line,
1149 "rtx type `%s' has `%c' in position %lu, can't handle",
1150 rtx_name[i], rtx_format[i][aindex],
1151 (unsigned long) aindex);
1152 t = &string_type;
1153 subname = "rt_int";
1154 break;
1157 subfields = create_field (subfields, t,
1158 xasprintf (".fld[%lu].%s",
1159 (unsigned long) aindex,
1160 subname));
1161 subfields->opt = nodot;
1162 if (t == note_union_tp)
1163 subfields->opt =
1164 create_string_option (subfields->opt, "desc",
1165 "NOTE_KIND (&%0)");
1166 if (t == symbol_union_tp)
1167 subfields->opt =
1168 create_string_option (subfields->opt, "desc",
1169 "CONSTANT_POOL_ADDRESS_P (&%0)");
1172 if (i == SYMBOL_REF)
1174 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1175 holds. */
1176 type_p field_tp = find_structure ("block_symbol", 0);
1177 subfields
1178 = create_optional_field (subfields, field_tp, "block_sym",
1179 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1182 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1183 substruct = new_structure (sname, 0, &lexer_line, subfields, NULL);
1185 ftag = xstrdup (rtx_name[i]);
1186 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1187 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1188 flds = create_field (flds, substruct, "");
1189 flds->opt = create_string_option (nodot, "tag", ftag);
1191 return new_structure ("rtx_def_subunion", 1, &lexer_line, flds, nodot);
1194 /* Handle `special("tree_exp")'. This is a special case for
1195 field `operands' of struct tree_exp, which although it claims to contain
1196 pointers to trees, actually sometimes contains pointers to RTL too.
1197 Passed T, the old type of the field, and OPT its options. Returns
1198 a new type for the field. */
1200 static type_p
1201 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1203 pair_p flds;
1204 options_p nodot;
1206 if (t->kind != TYPE_ARRAY)
1208 error_at_line (&lexer_line,
1209 "special `tree_exp' must be applied to an array");
1210 return &string_type;
1213 nodot = create_string_option (NULL, "dot", "");
1215 flds = create_field (NULL, t, "");
1216 flds->opt = create_string_option (nodot, "length",
1217 "TREE_OPERAND_LENGTH ((tree) &%0)");
1218 flds->opt = create_string_option (flds->opt, "default", "");
1220 return new_structure ("tree_exp_subunion", 1, &lexer_line, flds, nodot);
1223 /* Perform any special processing on a type T, about to become the type
1224 of a field. Return the appropriate type for the field.
1225 At present:
1226 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1227 - Similarly for arrays of pointer-to-char;
1228 - Converts structures for which a parameter is provided to
1229 TYPE_PARAM_STRUCT;
1230 - Handles "special" options.
1233 type_p
1234 adjust_field_type (type_p t, options_p opt)
1236 int length_p = 0;
1237 const int pointer_p = t->kind == TYPE_POINTER;
1238 type_p params[NUM_PARAM];
1239 int params_p = 0;
1240 int i;
1242 for (i = 0; i < NUM_PARAM; i++)
1243 params[i] = NULL;
1245 for (; opt; opt = opt->next)
1246 if (strcmp (opt->name, "length") == 0)
1247 length_p = 1;
1248 else if ((strcmp (opt->name, "param_is") == 0
1249 || (strncmp (opt->name, "param", 5) == 0
1250 && ISDIGIT (opt->name[5])
1251 && strcmp (opt->name + 6, "_is") == 0))
1252 && opt->kind == OPTION_TYPE)
1254 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1256 if (!UNION_OR_STRUCT_P (t)
1257 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
1259 error_at_line (&lexer_line,
1260 "option `%s' may only be applied to structures or structure pointers",
1261 opt->name);
1262 return t;
1265 params_p = 1;
1266 if (params[num] != NULL)
1267 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1268 if (!ISDIGIT (opt->name[5]))
1269 params[num] = create_pointer (opt->info.type);
1270 else
1271 params[num] = opt->info.type;
1273 else if (strcmp (opt->name, "special") == 0
1274 && opt->kind == OPTION_STRING)
1276 const char *special_name = opt->info.string;
1277 if (strcmp (special_name, "tree_exp") == 0)
1278 t = adjust_field_tree_exp (t, opt);
1279 else if (strcmp (special_name, "rtx_def") == 0)
1280 t = adjust_field_rtx_def (t, opt);
1281 else
1282 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1285 if (params_p)
1287 type_p realt;
1289 if (pointer_p)
1290 t = t->u.p;
1291 realt = find_param_structure (t, params);
1292 t = pointer_p ? create_pointer (realt) : realt;
1295 if (!length_p
1296 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1297 return &string_type;
1298 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1299 && t->u.a.p->u.p->kind == TYPE_SCALAR
1300 && t->u.a.p->u.p->u.scalar_is_char)
1301 return create_array (&string_type, t->u.a.len);
1303 return t;
1307 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *);
1308 static void set_gc_used (pair_p);
1310 /* Handle OPT for set_gc_used_type. */
1312 static void
1313 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1314 int *pass_param, int *length, int *skip,
1315 type_p *nested_ptr)
1317 options_p o;
1318 for (o = opt; o; o = o->next)
1319 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1320 && o->kind == OPTION_TYPE)
1321 set_gc_used_type (o->info.type,
1322 GC_POINTED_TO, NULL);
1323 else if (strcmp (o->name, "maybe_undef") == 0)
1324 *maybe_undef = 1;
1325 else if (strcmp (o->name, "use_params") == 0)
1326 *pass_param = 1;
1327 else if (strcmp (o->name, "length") == 0)
1328 *length = 1;
1329 else if (strcmp (o->name, "skip") == 0)
1330 *skip = 1;
1331 else if (strcmp (o->name, "nested_ptr") == 0
1332 && o->kind == OPTION_NESTED)
1333 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1337 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1338 static void
1339 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM])
1341 if (t->gc_used >= level)
1342 return;
1344 t->gc_used = level;
1346 switch (t->kind)
1348 case TYPE_STRUCT:
1349 case TYPE_UNION:
1351 pair_p f;
1352 int dummy;
1353 type_p dummy2;
1355 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1356 &dummy2);
1358 for (f = t->u.s.fields; f; f = f->next)
1360 int maybe_undef = 0;
1361 int pass_param = 0;
1362 int length = 0;
1363 int skip = 0;
1364 type_p nested_ptr = NULL;
1365 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1366 &length, &skip, &nested_ptr);
1368 if (nested_ptr && f->type->kind == TYPE_POINTER)
1369 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1370 pass_param ? param : NULL);
1371 else if (length && f->type->kind == TYPE_POINTER)
1372 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1373 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1374 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1375 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1376 set_gc_used_type (find_param_structure (f->type->u.p, param),
1377 GC_POINTED_TO, NULL);
1378 else if (skip)
1379 ; /* target type is not used through this field */
1380 else
1381 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL);
1383 break;
1386 case TYPE_POINTER:
1387 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1388 break;
1390 case TYPE_ARRAY:
1391 set_gc_used_type (t->u.a.p, GC_USED, param);
1392 break;
1394 case TYPE_LANG_STRUCT:
1395 for (t = t->u.s.lang_struct; t; t = t->next)
1396 set_gc_used_type (t, level, param);
1397 break;
1399 case TYPE_PARAM_STRUCT:
1401 int i;
1402 for (i = 0; i < NUM_PARAM; i++)
1403 if (t->u.param_struct.param[i] != 0)
1404 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1406 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1407 level = GC_POINTED_TO;
1408 else
1409 level = GC_USED;
1410 t->u.param_struct.stru->gc_used = GC_UNUSED;
1411 set_gc_used_type (t->u.param_struct.stru, level,
1412 t->u.param_struct.param);
1413 break;
1415 default:
1416 break;
1420 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1422 static void
1423 set_gc_used (pair_p variables)
1425 int nbvars = 0;
1426 pair_p p;
1427 for (p = variables; p; p = p->next)
1429 set_gc_used_type (p->type, GC_USED, NULL);
1430 nbvars++;
1432 if (verbosity_level >= 2)
1433 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1436 /* File mapping routines. For each input file, there is one output .c file
1437 (but some output files have many input files), and there is one .h file
1438 for the whole build. */
1440 /* Output file handling. */
1442 /* Create and return an outf_p for a new file for NAME, to be called
1443 ONAME. */
1445 static outf_p
1446 create_file (const char *name, const char *oname)
1448 static const char *const hdr[] = {
1449 " Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc.\n",
1450 "\n",
1451 "This file is part of GCC.\n",
1452 "\n",
1453 "GCC is free software; you can redistribute it and/or modify it under\n",
1454 "the terms of the GNU General Public License as published by the Free\n",
1455 "Software Foundation; either version 3, or (at your option) any later\n",
1456 "version.\n",
1457 "\n",
1458 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1459 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1460 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1461 "for more details.\n",
1462 "\n",
1463 "You should have received a copy of the GNU General Public License\n",
1464 "along with GCC; see the file COPYING3. If not see\n",
1465 "<http://www.gnu.org/licenses/>. */\n",
1466 "\n",
1467 "/* This file is machine generated. Do not edit. */\n"
1469 outf_p f;
1470 size_t i;
1472 gcc_assert (name != NULL);
1473 gcc_assert (oname != NULL);
1474 f = XCNEW (struct outf);
1475 f->next = output_files;
1476 f->name = oname;
1477 output_files = f;
1479 oprintf (f, "/* Type information for %s.\n", name);
1480 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1481 oprintf (f, "%s", hdr[i]);
1482 return f;
1485 /* Print, like fprintf, to O.
1486 N.B. You might think this could be implemented more efficiently
1487 with vsnprintf(). Unfortunately, there are C libraries that
1488 provide that function but without the C99 semantics for its return
1489 value, making it impossible to know how much space is required. */
1490 void
1491 oprintf (outf_p o, const char *format, ...)
1493 char *s;
1494 size_t slength;
1495 va_list ap;
1497 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1498 in that case. */
1499 if (!o)
1500 return;
1502 va_start (ap, format);
1503 slength = vasprintf (&s, format, ap);
1504 if (s == NULL || (int) slength < 0)
1505 fatal ("out of memory");
1506 va_end (ap);
1508 if (o->bufused + slength > o->buflength)
1510 size_t new_len = o->buflength;
1511 if (new_len == 0)
1512 new_len = 1024;
1515 new_len *= 2;
1517 while (o->bufused + slength >= new_len);
1518 o->buf = XRESIZEVEC (char, o->buf, new_len);
1519 o->buflength = new_len;
1521 memcpy (o->buf + o->bufused, s, slength);
1522 o->bufused += slength;
1523 free (s);
1526 /* Open the global header file and the language-specific header files. */
1528 static void
1529 open_base_files (void)
1531 size_t i;
1533 if (nb_plugin_files > 0 && plugin_files)
1534 return;
1536 header_file = create_file ("GCC", "gtype-desc.h");
1538 base_files = XNEWVEC (outf_p, num_lang_dirs);
1540 for (i = 0; i < num_lang_dirs; i++)
1541 base_files[i] = create_file (lang_dir_names[i],
1542 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1544 /* gtype-desc.c is a little special, so we create it here. */
1546 /* The order of files here matters very much. */
1547 static const char *const ifiles[] = {
1548 "config.h", "system.h", "coretypes.h", "tm.h",
1549 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1550 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1551 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1552 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1553 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1554 "cfglayout.h", "except.h", "output.h", "gimple.h", "cfgloop.h",
1555 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h", NULL
1557 const char *const *ifp;
1558 outf_p gtype_desc_c;
1560 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1561 for (ifp = ifiles; *ifp; ifp++)
1562 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1564 /* Make sure we handle "cfun" specially. */
1565 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1566 oprintf (gtype_desc_c, "#undef cfun\n");
1570 /* For INPF an input file, return the real basename of INPF, with all
1571 the directory components skipped. */
1573 static const char *
1574 get_file_realbasename (const input_file *inpf)
1576 const char *f = get_input_file_name (inpf);
1577 const char *lastslash = strrchr (f, '/');
1579 return (lastslash != NULL) ? lastslash + 1 : f;
1582 /* For INPF a filename, return the relative path to INPF from
1583 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1585 const char *
1586 get_file_srcdir_relative_path (const input_file *inpf)
1588 const char *f = get_input_file_name (inpf);
1589 if (strlen (f) > srcdir_len
1590 && IS_DIR_SEPARATOR (f[srcdir_len])
1591 && strncmp (f, srcdir, srcdir_len) == 0)
1592 return f + srcdir_len + 1;
1593 else
1594 return NULL;
1597 /* For INPF an input_file, return the relative path to INPF from
1598 $(srcdir) if the latter is a prefix in INPF, or the real basename
1599 of INPF otherwise. */
1601 static const char *
1602 get_file_basename (const input_file *inpf)
1604 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1606 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1609 /* For F a filename, return the lang_dir_names relative index of the language
1610 directory that is a prefix in F, if any, -1 otherwise. */
1612 static int
1613 get_prefix_langdir_index (const char *f)
1615 size_t f_len = strlen (f);
1616 size_t lang_index;
1618 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1620 const char *langdir = lang_dir_names[lang_index];
1621 size_t langdir_len = strlen (langdir);
1623 if (f_len > langdir_len
1624 && IS_DIR_SEPARATOR (f[langdir_len])
1625 && memcmp (f, langdir, langdir_len) == 0)
1626 return lang_index;
1629 return -1;
1632 /* For INPF an input file, return the name of language directory where
1633 F is located, if any, NULL otherwise. */
1635 static const char *
1636 get_file_langdir (const input_file *inpf)
1638 /* Get the relative path to INPF from $(srcdir) and find the
1639 language by comparing the prefix with language directory names.
1640 If INPF is not even srcdir relative, no point in looking
1641 further. */
1643 int lang_index;
1644 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1645 const char *r;
1647 if (!srcdir_relative_path)
1648 return NULL;
1650 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1651 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1652 r = "c-family";
1653 else if (lang_index >= 0)
1654 r = lang_dir_names[lang_index];
1655 else
1656 r = NULL;
1658 return r;
1661 /* The gt- output file name for INPF. */
1663 static const char *
1664 get_file_gtfilename (const input_file *inpf)
1666 /* Cook up an initial version of the gt- file name from the file real
1667 basename and the language name, if any. */
1669 const char *basename = get_file_realbasename (inpf);
1670 const char *langdir = get_file_langdir (inpf);
1672 char *result =
1673 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1674 : xasprintf ("gt-%s", basename));
1676 /* Then replace all non alphanumerics characters by '-' and change the
1677 extension to ".h". We expect the input filename extension was at least
1678 one character long. */
1680 char *s = result;
1682 for (; *s != '.'; s++)
1683 if (!ISALNUM (*s) && *s != '-')
1684 *s = '-';
1686 memcpy (s, ".h", sizeof (".h"));
1688 return result;
1691 /* Each input_file has its associated output file outf_p. The
1692 association is computed by the function
1693 get_output_file_with_visibility. The associated file is cached
1694 inside input_file in its inpoutf field, so is really computed only
1695 once. Associated output file paths (i.e. output_name-s) are
1696 computed by a rule based regexp machinery, using the files_rules
1697 array of struct file_rule_st. A for_name is also computed, giving
1698 the source file name for which the output_file is generated; it is
1699 often the last component of the input_file path. */
1703 Regexpr machinery to compute the output_name and for_name-s of each
1704 input_file. We have a sequence of file rules which gives the POSIX
1705 extended regular expression to match an input file path, and two
1706 transformed strings for the corresponding output_name and the
1707 corresponding for_name. The transformed string contain dollars: $0
1708 is replaced by the entire match, $1 is replaced by the substring
1709 matching the first parenthesis in the regexp, etc. And $$ is replaced
1710 by a single verbatim dollar. The rule order is important. The
1711 general case is last, and the particular cases should come before.
1712 An action routine can, when needed, update the out_name & for_name
1713 and/or return the appropriate output file. It is invoked only when a
1714 rule is triggered. When a rule is triggered, the output_name and
1715 for_name are computed using their transform string in while $$, $0,
1716 $1, ... are suitably replaced. If there is an action, it is called.
1717 In some few cases, the action can directly return the outf_p, but
1718 usually it just updates the output_name and for_name so should free
1719 them before replacing them. The get_output_file_with_visibility
1720 function creates an outf_p only once per each output_name, so it
1721 scans the output_files list for previously seen output file names.
1724 /* Signature of actions in file rules. */
1725 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1728 struct file_rule_st {
1729 const char* frul_srcexpr; /* Source string for regexp. */
1730 int frul_rflags; /* Flags passed to regcomp, usually
1731 * REG_EXTENDED. */
1732 regex_t* frul_re; /* Compiled regular expression
1733 obtained by regcomp. */
1734 const char* frul_tr_out; /* Transformation string for making
1735 * the output_name, with $1 ... $9 for
1736 * subpatterns and $0 for the whole
1737 * matched filename. */
1738 const char* frul_tr_for; /* Tranformation string for making the
1739 for_name. */
1740 frul_actionrout_t* frul_action; /* The action, if non null, is
1741 * called once the rule matches, on
1742 * the transformed out_name &
1743 * for_name. It could change them
1744 * and/or give the output file. */
1747 /* File rule action handling *.h files. */
1748 static outf_p header_dot_h_frul (input_file*, char**, char**);
1750 /* File rule action handling *.c files. */
1751 static outf_p source_dot_c_frul (input_file*, char**, char**);
1753 #define NULL_REGEX (regex_t*)0
1755 /* The prefix in our regexp-s matching the directory. */
1756 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1758 #define NULL_FRULACT (frul_actionrout_t*)0
1760 /* The array of our rules governing file name generation. Rules order
1761 matters, so change with extreme care! */
1763 struct file_rule_st files_rules[] = {
1764 /* The general rule assumes that files in subdirectories belong to a
1765 particular front-end, and files not in subdirectories are shared.
1766 The following rules deal with exceptions - files that are in
1767 subdirectories and yet are shared, and files that are top-level,
1768 but are not shared. */
1770 /* the c-family/ source directory is special. */
1771 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1772 REG_EXTENDED, NULL_REGEX,
1773 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1775 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1776 REG_EXTENDED, NULL_REGEX,
1777 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1779 /* Both c-lang.h & c-tree.h gives gt-c-decl.h for c-decl.c ! */
1780 { DIR_PREFIX_REGEX "c-lang\\.h$",
1781 REG_EXTENDED, NULL_REGEX, "gt-c-decl.h", "c-decl.c", NULL_FRULACT},
1783 { DIR_PREFIX_REGEX "c-tree\\.h$",
1784 REG_EXTENDED, NULL_REGEX, "gt-c-decl.h", "c-decl.c", NULL_FRULACT},
1786 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1787 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1788 REG_EXTENDED, NULL_REGEX,
1789 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1791 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1792 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1793 REG_EXTENDED, NULL_REGEX,
1794 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1796 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1797 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1798 REG_EXTENDED, NULL_REGEX,
1799 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1801 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1802 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1803 REG_EXTENDED, NULL_REGEX,
1804 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1806 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1807 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1808 REG_EXTENDED, NULL_REGEX,
1809 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1811 /* General cases. For header *.h and source *.c files, we need
1812 * special actions to handle the language. */
1814 /* Source *.c files are using get_file_gtfilename to compute their
1815 output_name and get_file_basename to compute their for_name
1816 thru the source_dot_c_frul action. */
1817 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
1818 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
1819 /* Common header files get "gtype-desc.c" as their output_name,
1820 * while language specific header files are handled specially. So
1821 * we need the header_dot_h_frul action. */
1822 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
1823 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
1825 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
1826 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
1828 /* Mandatory null last entry signaling end of rules. */
1829 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
1832 /* Special file rules action for handling *.h header files. It gives
1833 "gtype-desc.c" for common headers and corresponding output
1834 files for language-specific header files. */
1835 static outf_p
1836 header_dot_h_frul (input_file* inpf, char**poutname,
1837 char**pforname ATTRIBUTE_UNUSED)
1839 const char *basename = 0;
1840 int lang_index = 0;
1841 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
1842 (void*) inpf, get_input_file_name (inpf),
1843 *poutname, *pforname);
1844 basename = get_file_basename (inpf);
1845 lang_index = get_prefix_langdir_index (basename);
1846 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
1848 if (lang_index >= 0)
1850 /* The header is language specific. Given output_name &
1851 for_name remains unchanged. The base_files array gives the
1852 outf_p. */
1853 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
1854 (void*) base_files[lang_index],
1855 (base_files[lang_index])->name);
1856 return base_files[lang_index];
1858 else
1860 /* The header is common to all front-end languages. So
1861 output_name is "gtype-desc.c" file. The calling function
1862 get_output_file_with_visibility will find its outf_p. */
1863 free (*poutname);
1864 *poutname = xstrdup ("gtype-desc.c");
1865 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
1866 get_input_file_name (inpf));
1867 return NULL;
1872 /* Special file rules action for handling *.c source files using
1873 * get_file_gtfilename to compute their output_name and
1874 * get_file_basename to compute their for_name. The output_name is
1875 * gt-<LANG>-<BASE>.h for language specific source files, and
1876 * gt-<BASE>.h for common source files. */
1877 static outf_p
1878 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
1880 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
1881 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
1882 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
1883 (void*) inpf, get_input_file_name (inpf),
1884 *poutname, *pforname);
1885 DBGPRINTF ("newoutname %s", newoutname);
1886 DBGPRINTF ("newbasename %s", newbasename);
1887 free (*poutname);
1888 free (*pforname);
1889 *poutname = newoutname;
1890 *pforname = newbasename;
1891 return NULL;
1894 /* Utility function for get_output_file_with_visibility which returns
1895 * a malloc-ed substituted string using TRS on matching of the FILNAM
1896 * file name, using the PMATCH array. */
1897 static char*
1898 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
1899 const char *trs)
1901 struct obstack str_obstack;
1902 char *str = NULL;
1903 char *rawstr = NULL;
1904 const char *pt = NULL;
1905 DBGPRINTF ("filnam %s", filnam);
1906 obstack_init (&str_obstack);
1907 for (pt = trs; *pt; pt++) {
1908 char c = *pt;
1909 if (c == '$')
1911 if (pt[1] == '$')
1913 /* A double dollar $$ is substituted by a single verbatim
1914 dollar, but who really uses dollar signs in file
1915 paths? */
1916 obstack_1grow (&str_obstack, '$');
1918 else if (ISDIGIT (pt[1]))
1920 /* Handle $0 $1 ... $9 by appropriate substitution. */
1921 int dolnum = pt[1] - '0';
1922 int so = pmatch[dolnum].rm_so;
1923 int eo = pmatch[dolnum].rm_eo;
1924 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
1925 if (so>=0 && eo>=so)
1926 obstack_grow (&str_obstack, filnam + so, eo - so);
1928 else
1930 /* This can happen only when files_rules is buggy! */
1931 gcc_unreachable();
1933 /* Always skip the character after the dollar. */
1934 pt++;
1936 else
1937 obstack_1grow (&str_obstack, c);
1939 obstack_1grow (&str_obstack, '\0');
1940 rawstr = XOBFINISH (&str_obstack, char *);
1941 str = xstrdup (rawstr);
1942 obstack_free (&str_obstack, rawstr);
1943 DBGPRINTF ("matched replacement %s", str);
1944 rawstr = NULL;
1945 return str;
1949 /* An output file, suitable for definitions, that can see declarations
1950 made in INPF and is linked into every language that uses INPF.
1951 Since the the result is cached inside INPF, that argument cannot be
1952 declared constant, but is "almost" constant. */
1954 outf_p
1955 get_output_file_with_visibility (input_file *inpf)
1957 outf_p r;
1958 char *for_name = NULL;
1959 char *output_name = NULL;
1960 const char* inpfname;
1962 /* This can happen when we need a file with visibility on a
1963 structure that we've never seen. We have to just hope that it's
1964 globally visible. */
1965 if (inpf == NULL)
1966 inpf = system_h_file;
1968 /* The result is cached in INPF, so return it if already known. */
1969 if (inpf->inpoutf)
1970 return inpf->inpoutf;
1972 /* In plugin mode, return NULL unless the input_file is one of the
1973 plugin_files. */
1974 if (plugin_files)
1976 size_t i;
1977 for (i = 0; i < nb_plugin_files; i++)
1978 if (inpf == plugin_files[i])
1980 inpf->inpoutf = plugin_output;
1981 return plugin_output;
1984 return NULL;
1987 inpfname = get_input_file_name (inpf);
1989 /* Try each rule in sequence in files_rules until one is triggered. */
1991 int rulix = 0;
1992 DBGPRINTF ("passing input file @ %p named %s thru the files_rules",
1993 (void*) inpf, inpfname);
1995 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
1997 DBGPRINTF ("rulix#%d srcexpr %s",
1998 rulix, files_rules[rulix].frul_srcexpr);
2000 if (!files_rules[rulix].frul_re)
2002 /* Compile the regexpr lazily. */
2003 int err = 0;
2004 files_rules[rulix].frul_re = XCNEW (regex_t);
2005 err = regcomp (files_rules[rulix].frul_re,
2006 files_rules[rulix].frul_srcexpr,
2007 files_rules[rulix].frul_rflags);
2008 if (err)
2010 /* The regular expression compilation fails only when
2011 file_rules is buggy. */
2012 gcc_unreachable ();
2016 output_name = NULL;
2017 for_name = NULL;
2019 /* Match the regexpr and trigger the rule if matched. */
2021 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2022 $3, ... $9. */
2023 regmatch_t pmatch[10];
2024 memset (pmatch, 0, sizeof (pmatch));
2025 if (!regexec (files_rules[rulix].frul_re,
2026 inpfname, 10, pmatch, 0))
2028 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2029 (void*) inpf, inpfname, rulix,
2030 files_rules[rulix].frul_srcexpr);
2031 for_name =
2032 matching_file_name_substitute (inpfname, pmatch,
2033 files_rules[rulix].frul_tr_for);
2034 DBGPRINTF ("for_name %s", for_name);
2035 output_name =
2036 matching_file_name_substitute (inpfname, pmatch,
2037 files_rules[rulix].frul_tr_out);
2038 DBGPRINTF ("output_name %s", output_name);
2039 if (files_rules[rulix].frul_action)
2041 /* Invoke our action routine. */
2042 outf_p of = NULL;
2043 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2044 rulix, output_name, for_name);
2045 of =
2046 (files_rules[rulix].frul_action) (inpf,
2047 &output_name, &for_name);
2048 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2049 rulix, (void*)of, output_name, for_name);
2050 /* If the action routine returned something, give it back
2051 immediately and cache it in inpf. */
2052 if (of)
2054 inpf->inpoutf = of;
2055 return of;
2058 /* The rule matched, and had no action, or that action did
2059 not return any output file but could have changed the
2060 output_name or for_name. We break out of the loop on the
2061 files_rules. */
2062 break;
2064 else
2066 /* The regexpr did not match. */
2067 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2068 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2069 continue;
2074 if (!output_name || !for_name)
2076 /* This is impossible, and could only happen if the files_rules is
2077 incomplete or buggy. */
2078 gcc_unreachable ();
2081 /* Look through to see if we've ever seen this output filename
2082 before. If found, cache the result in inpf. */
2083 for (r = output_files; r; r = r->next)
2084 if (strcmp (r->name, output_name) == 0)
2086 inpf->inpoutf = r;
2087 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2088 output_name, for_name);
2089 return r;
2092 /* If not found, create it, and cache it in inpf. */
2093 r = create_file (for_name, output_name);
2095 gcc_assert (r && r->name);
2096 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2097 output_name, for_name);
2098 inpf->inpoutf = r;
2099 return r;
2104 /* The name of an output file, suitable for definitions, that can see
2105 declarations made in INPF and is linked into every language that
2106 uses INPF. */
2108 const char *
2109 get_output_file_name (input_file* inpf)
2111 outf_p o = get_output_file_with_visibility (inpf);
2112 if (o)
2113 return o->name;
2114 return NULL;
2117 /* Check if existing file is equal to the in memory buffer. */
2119 static bool
2120 is_file_equal (outf_p of)
2122 FILE *newfile = fopen (of->name, "r");
2123 size_t i;
2124 bool equal;
2125 if (newfile == NULL)
2126 return false;
2128 equal = true;
2129 for (i = 0; i < of->bufused; i++)
2131 int ch;
2132 ch = fgetc (newfile);
2133 if (ch == EOF || ch != (unsigned char) of->buf[i])
2135 equal = false;
2136 break;
2139 fclose (newfile);
2140 return equal;
2143 /* Copy the output to its final destination,
2144 but don't unnecessarily change modification times. */
2146 static void
2147 close_output_files (void)
2149 int nbwrittenfiles = 0;
2150 outf_p of;
2152 for (of = output_files; of; of = of->next)
2155 if (!is_file_equal (of))
2157 FILE *newfile = NULL;
2158 char *backupname = NULL;
2159 /* Back up the old version of the output file gt-FOO.c as
2160 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2161 if (backup_dir)
2163 backupname = concat (backup_dir, "/",
2164 lbasename (of->name), "~", NULL);
2165 if (!access (of->name, F_OK) && rename (of->name, backupname))
2166 fatal ("failed to back up %s as %s: %s",
2167 of->name, backupname, xstrerror (errno));
2170 newfile = fopen (of->name, "w");
2171 if (newfile == NULL)
2172 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2173 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2174 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2175 if (fclose (newfile) != 0)
2176 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2177 nbwrittenfiles++;
2178 if (verbosity_level >= 2 && backupname)
2179 printf ("%s wrote #%-3d %s backed-up in %s\n",
2180 progname, nbwrittenfiles, of->name, backupname);
2181 else if (verbosity_level >= 1)
2182 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2183 free (backupname);
2185 else
2187 /* output file remains unchanged. */
2188 if (verbosity_level >= 2)
2189 printf ("%s keep %s\n", progname, of->name);
2191 free (of->buf);
2192 of->buf = NULL;
2193 of->bufused = of->buflength = 0;
2195 if (verbosity_level >= 1)
2196 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2199 struct flist
2201 struct flist *next;
2202 int started_p;
2203 const input_file* file;
2204 outf_p f;
2207 struct walk_type_data;
2209 /* For scalars and strings, given the item in 'val'.
2210 For structures, given a pointer to the item in 'val'.
2211 For misc. pointers, given the item in 'val'.
2213 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2214 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2216 /* Parameters for write_types. */
2218 struct write_types_data
2220 const char *prefix;
2221 const char *param_prefix;
2222 const char *subfield_marker_routine;
2223 const char *marker_routine;
2224 const char *reorder_note_routine;
2225 const char *comment;
2226 int skip_hooks; /* skip hook generation if non zero */
2229 static void output_escaped_param (struct walk_type_data *d,
2230 const char *, const char *);
2231 static void output_mangled_typename (outf_p, const_type_p);
2232 static void walk_type (type_p t, struct walk_type_data *d);
2233 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2234 const struct write_types_data *wtd);
2235 static void write_types_process_field
2236 (type_p f, const struct walk_type_data *d);
2237 static void write_types (outf_p output_header,
2238 type_p structures,
2239 type_p param_structs,
2240 const struct write_types_data *wtd);
2241 static void write_types_local_process_field
2242 (type_p f, const struct walk_type_data *d);
2243 static void write_local_func_for_structure
2244 (const_type_p orig_s, type_p s, type_p *param);
2245 static void write_local (outf_p output_header,
2246 type_p structures, type_p param_structs);
2247 static void write_enum_defn (type_p structures, type_p param_structs);
2248 static int contains_scalar_p (type_p t);
2249 static void put_mangled_filename (outf_p, const input_file *);
2250 static void finish_root_table (struct flist *flp, const char *pfx,
2251 const char *tname, const char *lastname,
2252 const char *name);
2253 static void write_root (outf_p, pair_p, type_p, const char *, int,
2254 struct fileloc *, const char *, bool);
2255 static void write_array (outf_p f, pair_p v,
2256 const struct write_types_data *wtd);
2257 static void write_roots (pair_p, bool);
2259 /* Parameters for walk_type. */
2261 struct walk_type_data
2263 process_field_fn process_field;
2264 const void *cookie;
2265 outf_p of;
2266 options_p opt;
2267 const char *val;
2268 const char *prev_val[4];
2269 int indent;
2270 int counter;
2271 const struct fileloc *line;
2272 lang_bitmap bitmap;
2273 type_p *param;
2274 int used_length;
2275 type_p orig_s;
2276 const char *reorder_fn;
2277 bool needs_cast_p;
2278 bool fn_wants_lvalue;
2281 /* Print a mangled name representing T to OF. */
2283 static void
2284 output_mangled_typename (outf_p of, const_type_p t)
2286 if (t == NULL)
2287 oprintf (of, "Z");
2288 else
2289 switch (t->kind)
2291 case TYPE_NONE:
2292 gcc_unreachable ();
2293 break;
2294 case TYPE_POINTER:
2295 oprintf (of, "P");
2296 output_mangled_typename (of, t->u.p);
2297 break;
2298 case TYPE_SCALAR:
2299 oprintf (of, "I");
2300 break;
2301 case TYPE_STRING:
2302 oprintf (of, "S");
2303 break;
2304 case TYPE_STRUCT:
2305 case TYPE_UNION:
2306 case TYPE_LANG_STRUCT:
2307 oprintf (of, "%lu%s", (unsigned long) strlen (t->u.s.tag),
2308 t->u.s.tag);
2309 break;
2310 case TYPE_PARAM_STRUCT:
2312 int i;
2313 for (i = 0; i < NUM_PARAM; i++)
2314 if (t->u.param_struct.param[i] != NULL)
2315 output_mangled_typename (of, t->u.param_struct.param[i]);
2316 output_mangled_typename (of, t->u.param_struct.stru);
2318 break;
2319 case TYPE_ARRAY:
2320 gcc_unreachable ();
2324 /* Print PARAM to D->OF processing escapes. D->VAL references the
2325 current object, D->PREV_VAL the object containing the current
2326 object, ONAME is the name of the option and D->LINE is used to
2327 print error messages. */
2329 static void
2330 output_escaped_param (struct walk_type_data *d, const char *param,
2331 const char *oname)
2333 const char *p;
2335 for (p = param; *p; p++)
2336 if (*p != '%')
2337 oprintf (d->of, "%c", *p);
2338 else
2339 switch (*++p)
2341 case 'h':
2342 oprintf (d->of, "(%s)", d->prev_val[2]);
2343 break;
2344 case '0':
2345 oprintf (d->of, "(%s)", d->prev_val[0]);
2346 break;
2347 case '1':
2348 oprintf (d->of, "(%s)", d->prev_val[1]);
2349 break;
2350 case 'a':
2352 const char *pp = d->val + strlen (d->val);
2353 while (pp[-1] == ']')
2354 while (*pp != '[')
2355 pp--;
2356 oprintf (d->of, "%s", pp);
2358 break;
2359 default:
2360 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2361 oname, '%', *p);
2365 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2366 which is of type T. Write code to D->OF to constrain execution (at
2367 the point that D->PROCESS_FIELD is called) to the appropriate
2368 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2369 pointers to those objects. D->PREV_VAL lists the objects
2370 containing the current object, D->OPT is a list of options to
2371 apply, D->INDENT is the current indentation level, D->LINE is used
2372 to print error messages, D->BITMAP indicates which languages to
2373 print the structure for, and D->PARAM is the current parameter
2374 (from an enclosing param_is option). */
2376 static void
2377 walk_type (type_p t, struct walk_type_data *d)
2379 const char *length = NULL;
2380 const char *desc = NULL;
2381 int maybe_undef_p = 0;
2382 int use_param_num = -1;
2383 int use_params_p = 0;
2384 options_p oo;
2385 const struct nested_ptr_data *nested_ptr_d = NULL;
2387 d->needs_cast_p = false;
2388 for (oo = d->opt; oo; oo = oo->next)
2389 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2390 length = oo->info.string;
2391 else if (strcmp (oo->name, "maybe_undef") == 0)
2392 maybe_undef_p = 1;
2393 else if (strncmp (oo->name, "use_param", 9) == 0
2394 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2395 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2396 else if (strcmp (oo->name, "use_params") == 0)
2397 use_params_p = 1;
2398 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2399 desc = oo->info.string;
2400 else if (strcmp (oo->name, "mark_hook") == 0)
2402 else if (strcmp (oo->name, "nested_ptr") == 0
2403 && oo->kind == OPTION_NESTED)
2404 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2405 else if (strcmp (oo->name, "dot") == 0)
2407 else if (strcmp (oo->name, "tag") == 0)
2409 else if (strcmp (oo->name, "special") == 0)
2411 else if (strcmp (oo->name, "skip") == 0)
2413 else if (strcmp (oo->name, "default") == 0)
2415 else if (strcmp (oo->name, "param_is") == 0)
2417 else if (strncmp (oo->name, "param", 5) == 0
2418 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2420 else if (strcmp (oo->name, "chain_next") == 0)
2422 else if (strcmp (oo->name, "chain_prev") == 0)
2424 else if (strcmp (oo->name, "chain_circular") == 0)
2426 else if (strcmp (oo->name, "reorder") == 0)
2428 else if (strcmp (oo->name, "variable_size") == 0)
2430 else
2431 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2433 if (d->used_length)
2434 length = NULL;
2436 if (use_params_p)
2438 int pointer_p = t->kind == TYPE_POINTER;
2440 if (pointer_p)
2441 t = t->u.p;
2442 if (!UNION_OR_STRUCT_P (t))
2443 error_at_line (d->line, "`use_params' option on unimplemented type");
2444 else
2445 t = find_param_structure (t, d->param);
2446 if (pointer_p)
2447 t = create_pointer (t);
2450 if (use_param_num != -1)
2452 if (d->param != NULL && d->param[use_param_num] != NULL)
2454 type_p nt = d->param[use_param_num];
2456 if (t->kind == TYPE_ARRAY)
2457 nt = create_array (nt, t->u.a.len);
2458 else if (length != NULL && t->kind == TYPE_POINTER)
2459 nt = create_pointer (nt);
2460 d->needs_cast_p = (t->kind != TYPE_POINTER
2461 && (nt->kind == TYPE_POINTER
2462 || nt->kind == TYPE_STRING));
2463 t = nt;
2465 else
2466 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2469 if (maybe_undef_p
2470 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
2472 error_at_line (d->line,
2473 "field `%s' has invalid option `maybe_undef_p'\n",
2474 d->val);
2475 return;
2478 switch (t->kind)
2480 case TYPE_SCALAR:
2481 case TYPE_STRING:
2482 d->process_field (t, d);
2483 break;
2485 case TYPE_POINTER:
2487 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2489 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2490 break;
2493 if (!length)
2495 if (!UNION_OR_STRUCT_P (t->u.p)
2496 && t->u.p->kind != TYPE_PARAM_STRUCT)
2498 error_at_line (d->line,
2499 "field `%s' is pointer to unimplemented type",
2500 d->val);
2501 break;
2504 if (nested_ptr_d)
2506 const char *oldprevval2 = d->prev_val[2];
2508 if (!UNION_OR_STRUCT_P (nested_ptr_d->type))
2510 error_at_line (d->line,
2511 "field `%s' has invalid "
2512 "option `nested_ptr'\n", d->val);
2513 return;
2516 d->prev_val[2] = d->val;
2517 oprintf (d->of, "%*s{\n", d->indent, "");
2518 d->indent += 2;
2519 d->val = xasprintf ("x%d", d->counter++);
2520 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2521 (nested_ptr_d->type->kind == TYPE_UNION
2522 ? "union" : "struct"),
2523 nested_ptr_d->type->u.s.tag,
2524 d->fn_wants_lvalue ? "" : "const ", d->val);
2525 oprintf (d->of, "%*s", d->indent + 2, "");
2526 output_escaped_param (d, nested_ptr_d->convert_from,
2527 "nested_ptr");
2528 oprintf (d->of, ";\n");
2530 d->process_field (nested_ptr_d->type, d);
2532 if (d->fn_wants_lvalue)
2534 oprintf (d->of, "%*s%s = ", d->indent, "",
2535 d->prev_val[2]);
2536 d->prev_val[2] = d->val;
2537 output_escaped_param (d, nested_ptr_d->convert_to,
2538 "nested_ptr");
2539 oprintf (d->of, ";\n");
2542 d->indent -= 2;
2543 oprintf (d->of, "%*s}\n", d->indent, "");
2544 d->val = d->prev_val[2];
2545 d->prev_val[2] = oldprevval2;
2547 else
2548 d->process_field (t->u.p, d);
2550 else
2552 int loopcounter = d->counter++;
2553 const char *oldval = d->val;
2554 const char *oldprevval3 = d->prev_val[3];
2555 char *newval;
2557 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2558 d->indent += 2;
2559 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2560 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2561 "", loopcounter, loopcounter);
2562 output_escaped_param (d, length, "length");
2563 oprintf (d->of, "); i%d++) {\n", loopcounter);
2564 d->indent += 2;
2565 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2566 d->used_length = 1;
2567 d->prev_val[3] = oldval;
2568 walk_type (t->u.p, d);
2569 free (newval);
2570 d->val = oldval;
2571 d->prev_val[3] = oldprevval3;
2572 d->used_length = 0;
2573 d->indent -= 2;
2574 oprintf (d->of, "%*s}\n", d->indent, "");
2575 d->process_field (t, d);
2576 d->indent -= 2;
2577 oprintf (d->of, "%*s}\n", d->indent, "");
2580 break;
2582 case TYPE_ARRAY:
2584 int loopcounter = d->counter++;
2585 const char *oldval = d->val;
2586 char *newval;
2588 /* If it's an array of scalars, we optimize by not generating
2589 any code. */
2590 if (t->u.a.p->kind == TYPE_SCALAR)
2591 break;
2593 /* When walking an array, compute the length and store it in a
2594 local variable before walking the array elements, instead of
2595 recomputing the length expression each time through the loop.
2596 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2597 where the length is stored in the first array element,
2598 because otherwise that operand can get overwritten on the
2599 first iteration. */
2600 oprintf (d->of, "%*s{\n", d->indent, "");
2601 d->indent += 2;
2602 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2603 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2604 d->indent, "", loopcounter);
2605 if (length)
2606 output_escaped_param (d, length, "length");
2607 else
2608 oprintf (d->of, "%s", t->u.a.len);
2609 oprintf (d->of, ");\n");
2611 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2612 d->indent, "",
2613 loopcounter, loopcounter, loopcounter, loopcounter);
2614 d->indent += 2;
2615 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2616 d->used_length = 1;
2617 walk_type (t->u.a.p, d);
2618 free (newval);
2619 d->used_length = 0;
2620 d->val = oldval;
2621 d->indent -= 2;
2622 oprintf (d->of, "%*s}\n", d->indent, "");
2623 d->indent -= 2;
2624 oprintf (d->of, "%*s}\n", d->indent, "");
2626 break;
2628 case TYPE_STRUCT:
2629 case TYPE_UNION:
2631 pair_p f;
2632 const char *oldval = d->val;
2633 const char *oldprevval1 = d->prev_val[1];
2634 const char *oldprevval2 = d->prev_val[2];
2635 const int union_p = t->kind == TYPE_UNION;
2636 int seen_default_p = 0;
2637 options_p o;
2639 if (!t->u.s.line.file)
2640 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2642 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2644 error_at_line (d->line,
2645 "structure `%s' defined for mismatching languages",
2646 t->u.s.tag);
2647 error_at_line (&t->u.s.line, "one structure defined here");
2650 /* Some things may also be defined in the structure's options. */
2651 for (o = t->u.s.opt; o; o = o->next)
2652 if (!desc && strcmp (o->name, "desc") == 0
2653 && o->kind == OPTION_STRING)
2654 desc = o->info.string;
2656 d->prev_val[2] = oldval;
2657 d->prev_val[1] = oldprevval2;
2658 if (union_p)
2660 if (desc == NULL)
2662 error_at_line (d->line,
2663 "missing `desc' option for union `%s'",
2664 t->u.s.tag);
2665 desc = "1";
2667 oprintf (d->of, "%*sswitch (", d->indent, "");
2668 output_escaped_param (d, desc, "desc");
2669 oprintf (d->of, ")\n");
2670 d->indent += 2;
2671 oprintf (d->of, "%*s{\n", d->indent, "");
2673 for (f = t->u.s.fields; f; f = f->next)
2675 options_p oo;
2676 const char *dot = ".";
2677 const char *tagid = NULL;
2678 int skip_p = 0;
2679 int default_p = 0;
2680 int use_param_p = 0;
2681 char *newval;
2683 d->reorder_fn = NULL;
2684 for (oo = f->opt; oo; oo = oo->next)
2685 if (strcmp (oo->name, "dot") == 0
2686 && oo->kind == OPTION_STRING)
2687 dot = oo->info.string;
2688 else if (strcmp (oo->name, "tag") == 0
2689 && oo->kind == OPTION_STRING)
2690 tagid = oo->info.string;
2691 else if (strcmp (oo->name, "skip") == 0)
2692 skip_p = 1;
2693 else if (strcmp (oo->name, "default") == 0)
2694 default_p = 1;
2695 else if (strcmp (oo->name, "reorder") == 0
2696 && oo->kind == OPTION_STRING)
2697 d->reorder_fn = oo->info.string;
2698 else if (strncmp (oo->name, "use_param", 9) == 0
2699 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2700 use_param_p = 1;
2702 if (skip_p)
2703 continue;
2705 if (union_p && tagid)
2707 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
2708 d->indent += 2;
2710 else if (union_p && default_p)
2712 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2713 d->indent += 2;
2714 seen_default_p = 1;
2716 else if (!union_p && (default_p || tagid))
2717 error_at_line (d->line,
2718 "can't use `%s' outside a union on field `%s'",
2719 default_p ? "default" : "tag", f->name);
2720 else if (union_p && !(default_p || tagid)
2721 && f->type->kind == TYPE_SCALAR)
2723 fprintf (stderr,
2724 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2725 get_input_file_name (d->line->file), d->line->line,
2726 f->name);
2727 continue;
2729 else if (union_p && !(default_p || tagid))
2730 error_at_line (d->line,
2731 "field `%s' is missing `tag' or `default' option",
2732 f->name);
2734 d->line = &f->line;
2735 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
2736 d->opt = f->opt;
2737 d->used_length = false;
2739 if (union_p && use_param_p && d->param == NULL)
2740 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
2741 else
2742 walk_type (f->type, d);
2744 free (newval);
2746 if (union_p)
2748 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2749 d->indent -= 2;
2752 d->reorder_fn = NULL;
2754 d->val = oldval;
2755 d->prev_val[1] = oldprevval1;
2756 d->prev_val[2] = oldprevval2;
2758 if (union_p && !seen_default_p)
2760 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2761 oprintf (d->of, "%*s break;\n", d->indent, "");
2763 if (union_p)
2765 oprintf (d->of, "%*s}\n", d->indent, "");
2766 d->indent -= 2;
2769 break;
2771 case TYPE_LANG_STRUCT:
2773 type_p nt;
2774 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
2775 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
2776 break;
2777 if (nt == NULL)
2778 error_at_line (d->line, "structure `%s' differs between languages",
2779 t->u.s.tag);
2780 else
2781 walk_type (nt, d);
2783 break;
2785 case TYPE_PARAM_STRUCT:
2787 type_p *oldparam = d->param;
2789 d->param = t->u.param_struct.param;
2790 walk_type (t->u.param_struct.stru, d);
2791 d->param = oldparam;
2793 break;
2795 default:
2796 gcc_unreachable ();
2800 /* process_field routine for marking routines. */
2802 static void
2803 write_types_process_field (type_p f, const struct walk_type_data *d)
2805 const struct write_types_data *wtd;
2806 const char *cast = d->needs_cast_p ? "(void *)" : "";
2807 wtd = (const struct write_types_data *) d->cookie;
2809 switch (f->kind)
2811 case TYPE_NONE:
2812 gcc_unreachable ();
2813 case TYPE_POINTER:
2814 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
2815 wtd->subfield_marker_routine, cast, d->val);
2816 if (wtd->param_prefix)
2818 oprintf (d->of, ", %s", d->prev_val[3]);
2819 if (d->orig_s)
2821 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
2822 output_mangled_typename (d->of, d->orig_s);
2824 else
2825 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
2827 if (f->u.p->kind == TYPE_PARAM_STRUCT
2828 && f->u.p->u.s.line.file != NULL)
2830 oprintf (d->of, ", gt_e_");
2831 output_mangled_typename (d->of, f);
2833 else if (UNION_OR_STRUCT_P (f) && f->u.p->u.s.line.file != NULL)
2835 oprintf (d->of, ", gt_ggc_e_");
2836 output_mangled_typename (d->of, f);
2838 else
2839 oprintf (d->of, ", gt_types_enum_last");
2841 oprintf (d->of, ");\n");
2842 if (d->reorder_fn && wtd->reorder_note_routine)
2843 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
2844 wtd->reorder_note_routine, cast, d->val,
2845 d->prev_val[3], d->reorder_fn);
2846 break;
2848 case TYPE_STRING:
2849 case TYPE_STRUCT:
2850 case TYPE_UNION:
2851 case TYPE_LANG_STRUCT:
2852 case TYPE_PARAM_STRUCT:
2853 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
2854 output_mangled_typename (d->of, f);
2855 oprintf (d->of, " (%s%s);\n", cast, d->val);
2856 if (d->reorder_fn && wtd->reorder_note_routine)
2857 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
2858 wtd->reorder_note_routine, cast, d->val, cast, d->val,
2859 d->reorder_fn);
2860 break;
2862 case TYPE_SCALAR:
2863 break;
2865 case TYPE_ARRAY:
2866 gcc_unreachable ();
2870 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
2872 static void
2873 output_type_enum (outf_p of, type_p s)
2875 if (s->kind == TYPE_PARAM_STRUCT && s->u.param_struct.line.file != NULL)
2877 oprintf (of, ", gt_e_");
2878 output_mangled_typename (of, s);
2880 else if (UNION_OR_STRUCT_P (s) && s->u.s.line.file != NULL)
2882 oprintf (of, ", gt_ggc_e_");
2883 output_mangled_typename (of, s);
2885 else
2886 oprintf (of, ", gt_types_enum_last");
2889 /* Return an output file that is suitable for definitions which can
2890 reference struct S */
2892 static outf_p
2893 get_output_file_for_structure (const_type_p s, type_p *param)
2895 const input_file *fn;
2896 int i;
2898 gcc_assert (UNION_OR_STRUCT_P (s));
2899 fn = s->u.s.line.file;
2901 /* This is a hack, and not the good kind either. */
2902 for (i = NUM_PARAM - 1; i >= 0; i--)
2903 if (param && param[i] && param[i]->kind == TYPE_POINTER
2904 && UNION_OR_STRUCT_P (param[i]->u.p))
2905 fn = param[i]->u.p->u.s.line.file;
2907 /* The call to get_output_file_with_visibility may update fn by
2908 caching its result inside, so we need the CONST_CAST. */
2909 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
2912 /* For S, a structure that's part of ORIG_S, and using parameters
2913 PARAM, write out a routine that:
2914 - Takes a parameter, a void * but actually of type *S
2915 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
2916 field of S or its substructures and (in some cases) things
2917 that are pointed to by S.
2920 static void
2921 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2922 const struct write_types_data *wtd)
2924 const char *chain_next = NULL;
2925 const char *chain_prev = NULL;
2926 const char *chain_circular = NULL;
2927 const char *mark_hook_name = NULL;
2928 options_p opt;
2929 struct walk_type_data d;
2931 memset (&d, 0, sizeof (d));
2932 d.of = get_output_file_for_structure (s, param);
2933 for (opt = s->u.s.opt; opt; opt = opt->next)
2934 if (strcmp (opt->name, "chain_next") == 0
2935 && opt->kind == OPTION_STRING)
2936 chain_next = opt->info.string;
2937 else if (strcmp (opt->name, "chain_prev") == 0
2938 && opt->kind == OPTION_STRING)
2939 chain_prev = opt->info.string;
2940 else if (strcmp (opt->name, "chain_circular") == 0
2941 && opt->kind == OPTION_STRING)
2942 chain_circular = opt->info.string;
2943 else if (strcmp (opt->name, "mark_hook") == 0
2944 && opt->kind == OPTION_STRING)
2945 mark_hook_name = opt->info.string;
2946 if (chain_prev != NULL && chain_next == NULL)
2947 error_at_line (&s->u.s.line, "chain_prev without chain_next");
2948 if (chain_circular != NULL && chain_next != NULL)
2949 error_at_line (&s->u.s.line, "chain_circular with chain_next");
2950 if (chain_circular != NULL)
2951 chain_next = chain_circular;
2953 d.process_field = write_types_process_field;
2954 d.cookie = wtd;
2955 d.orig_s = orig_s;
2956 d.opt = s->u.s.opt;
2957 d.line = &s->u.s.line;
2958 d.bitmap = s->u.s.bitmap;
2959 d.param = param;
2960 d.prev_val[0] = "*x";
2961 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
2962 d.prev_val[3] = "x";
2963 d.val = "(*x)";
2965 oprintf (d.of, "\n");
2966 oprintf (d.of, "void\n");
2967 if (param == NULL)
2968 oprintf (d.of, "gt_%sx_%s", wtd->prefix, orig_s->u.s.tag);
2969 else
2971 oprintf (d.of, "gt_%s_", wtd->prefix);
2972 output_mangled_typename (d.of, orig_s);
2974 oprintf (d.of, " (void *x_p)\n");
2975 oprintf (d.of, "{\n");
2976 oprintf (d.of, " %s %s * %sx = (%s %s *)x_p;\n",
2977 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
2978 chain_next == NULL ? "const " : "",
2979 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2980 if (chain_next != NULL)
2981 oprintf (d.of, " %s %s * xlimit = x;\n",
2982 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2983 if (chain_next == NULL)
2985 oprintf (d.of, " if (%s (x", wtd->marker_routine);
2986 if (wtd->param_prefix)
2988 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
2989 output_mangled_typename (d.of, orig_s);
2990 output_type_enum (d.of, orig_s);
2992 oprintf (d.of, "))\n");
2994 else
2996 if (chain_circular != NULL)
2997 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
2998 else
2999 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3000 if (wtd->param_prefix)
3002 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3003 output_mangled_typename (d.of, orig_s);
3004 output_type_enum (d.of, orig_s);
3006 oprintf (d.of, "))\n");
3007 if (chain_circular != NULL)
3008 oprintf (d.of, " return;\n do\n");
3009 if (mark_hook_name && !wtd->skip_hooks)
3011 oprintf (d.of, " {\n");
3012 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3014 oprintf (d.of, " xlimit = (");
3015 d.prev_val[2] = "*xlimit";
3016 output_escaped_param (&d, chain_next, "chain_next");
3017 oprintf (d.of, ");\n");
3018 if (mark_hook_name && !wtd->skip_hooks)
3019 oprintf (d.of, " }\n");
3020 if (chain_prev != NULL)
3022 oprintf (d.of, " if (x != xlimit)\n");
3023 oprintf (d.of, " for (;;)\n");
3024 oprintf (d.of, " {\n");
3025 oprintf (d.of, " %s %s * const xprev = (",
3026 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3028 d.prev_val[2] = "*x";
3029 output_escaped_param (&d, chain_prev, "chain_prev");
3030 oprintf (d.of, ");\n");
3031 oprintf (d.of, " if (xprev == NULL) break;\n");
3032 oprintf (d.of, " x = xprev;\n");
3033 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3034 if (wtd->param_prefix)
3036 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3037 output_mangled_typename (d.of, orig_s);
3038 output_type_enum (d.of, orig_s);
3040 oprintf (d.of, ");\n");
3041 oprintf (d.of, " }\n");
3043 if (chain_circular != NULL)
3045 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3046 if (wtd->param_prefix)
3048 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3049 output_mangled_typename (d.of, orig_s);
3050 output_type_enum (d.of, orig_s);
3052 oprintf (d.of, "));\n");
3053 if (mark_hook_name && !wtd->skip_hooks)
3054 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3055 oprintf (d.of, " do\n");
3057 else
3058 oprintf (d.of, " while (x != xlimit)\n");
3060 oprintf (d.of, " {\n");
3061 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3063 oprintf (d.of, " %s (x);\n", mark_hook_name);
3065 d.prev_val[2] = "*x";
3066 d.indent = 6;
3067 walk_type (s, &d);
3069 if (chain_next != NULL)
3071 oprintf (d.of, " x = (");
3072 output_escaped_param (&d, chain_next, "chain_next");
3073 oprintf (d.of, ");\n");
3076 oprintf (d.of, " }\n");
3077 if (chain_circular != NULL)
3078 oprintf (d.of, " while (x != xlimit);\n");
3079 oprintf (d.of, "}\n");
3082 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3084 static void
3085 write_types (outf_p output_header, type_p structures, type_p param_structs,
3086 const struct write_types_data *wtd)
3088 int nbfun = 0; /* Count the emitted functions. */
3089 type_p s;
3091 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3092 /* We first emit the macros and the declarations. Functions' code is
3093 emitted afterwards. This is needed in plugin mode. */
3094 oprintf (output_header, "/* macros and declarations */\n");
3095 for (s = structures; s; s = s->next)
3096 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3098 options_p opt;
3100 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3101 continue;
3103 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3104 output_mangled_typename (output_header, s);
3105 oprintf (output_header, "(X) do { \\\n");
3106 oprintf (output_header,
3107 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3108 s->u.s.tag);
3109 oprintf (output_header, " } while (0)\n");
3111 for (opt = s->u.s.opt; opt; opt = opt->next)
3112 if (strcmp (opt->name, "ptr_alias") == 0
3113 && opt->kind == OPTION_TYPE)
3115 const_type_p const t = (const_type_p) opt->info.type;
3116 if (t->kind == TYPE_STRUCT
3117 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3118 oprintf (output_header,
3119 "#define gt_%sx_%s gt_%sx_%s\n",
3120 wtd->prefix, s->u.s.tag, wtd->prefix, t->u.s.tag);
3121 else
3122 error_at_line (&s->u.s.line,
3123 "structure alias is not a structure");
3124 break;
3126 if (opt)
3127 continue;
3129 /* Declare the marker procedure only once. */
3130 oprintf (output_header,
3131 "extern void gt_%sx_%s (void *);\n",
3132 wtd->prefix, s->u.s.tag);
3134 if (s->u.s.line.file == NULL)
3136 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3137 s->u.s.tag);
3138 continue;
3142 for (s = param_structs; s; s = s->next)
3143 if (s->gc_used == GC_POINTED_TO)
3145 type_p stru = s->u.param_struct.stru;
3147 /* Declare the marker procedure. */
3148 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3149 output_mangled_typename (output_header, s);
3150 oprintf (output_header, " (void *);\n");
3152 if (stru->u.s.line.file == NULL)
3154 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3155 s->u.s.tag);
3156 continue;
3160 /* At last we emit the functions code. */
3161 oprintf (output_header, "\n/* functions code */\n");
3162 for (s = structures; s; s = s->next)
3163 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3165 options_p opt;
3167 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3168 continue;
3169 for (opt = s->u.s.opt; opt; opt = opt->next)
3170 if (strcmp (opt->name, "ptr_alias") == 0)
3171 break;
3172 if (opt)
3173 continue;
3175 if (s->kind == TYPE_LANG_STRUCT)
3177 type_p ss;
3178 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3180 nbfun++;
3181 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3182 nbfun, (void*) ss, ss->u.s.tag);
3183 write_func_for_structure (s, ss, NULL, wtd);
3186 else
3188 nbfun++;
3189 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3190 nbfun, (void*) s, s->u.s.tag);
3191 write_func_for_structure (s, s, NULL, wtd);
3194 else
3196 /* Structure s is not possibly pointed to, so can be ignored. */
3197 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3198 (void*)s, s->u.s.tag,
3199 (int) s->gc_used);
3202 for (s = param_structs; s; s = s->next)
3203 if (s->gc_used == GC_POINTED_TO)
3205 type_p *param = s->u.param_struct.param;
3206 type_p stru = s->u.param_struct.stru;
3207 if (stru->u.s.line.file == NULL)
3208 continue;
3209 if (stru->kind == TYPE_LANG_STRUCT)
3211 type_p ss;
3212 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3214 nbfun++;
3215 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3216 nbfun, (void*) ss, ss->u.s.tag);
3217 write_func_for_structure (s, ss, param, wtd);
3220 else
3222 nbfun++;
3223 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3224 nbfun, (void*) s,
3225 (void*) stru, stru->u.s.tag);
3226 write_func_for_structure (s, stru, param, wtd);
3229 else
3231 /* Param structure s is not pointed to, so should be ignored. */
3232 DBGPRINTF ("ignored s @ %p", (void*)s);
3234 if (verbosity_level >= 2)
3235 printf ("%s emitted %d routines for %s\n",
3236 progname, nbfun, wtd->comment);
3239 static const struct write_types_data ggc_wtd = {
3240 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3241 "GC marker procedures. ",
3242 FALSE
3245 static const struct write_types_data pch_wtd = {
3246 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3247 "gt_pch_note_reorder",
3248 "PCH type-walking procedures. ",
3249 TRUE
3252 /* Write out the local pointer-walking routines. */
3254 /* process_field routine for local pointer-walking. */
3256 static void
3257 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3259 switch (f->kind)
3261 case TYPE_POINTER:
3262 case TYPE_STRUCT:
3263 case TYPE_UNION:
3264 case TYPE_LANG_STRUCT:
3265 case TYPE_PARAM_STRUCT:
3266 case TYPE_STRING:
3267 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3268 d->prev_val[3]);
3269 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3270 break;
3272 case TYPE_SCALAR:
3273 break;
3275 default:
3276 gcc_unreachable ();
3280 /* For S, a structure that's part of ORIG_S, and using parameters
3281 PARAM, write out a routine that:
3282 - Is of type gt_note_pointers
3283 - Calls PROCESS_FIELD on each field of S or its substructures.
3286 static void
3287 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3289 struct walk_type_data d;
3291 memset (&d, 0, sizeof (d));
3292 d.of = get_output_file_for_structure (s, param);
3293 d.process_field = write_types_local_process_field;
3294 d.opt = s->u.s.opt;
3295 d.line = &s->u.s.line;
3296 d.bitmap = s->u.s.bitmap;
3297 d.param = param;
3298 d.prev_val[0] = d.prev_val[2] = "*x";
3299 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3300 d.prev_val[3] = "x";
3301 d.val = "(*x)";
3302 d.fn_wants_lvalue = true;
3304 oprintf (d.of, "\n");
3305 oprintf (d.of, "void\n");
3306 oprintf (d.of, "gt_pch_p_");
3307 output_mangled_typename (d.of, orig_s);
3308 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3309 "\tvoid *x_p,\n"
3310 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3311 "\tATTRIBUTE_UNUSED void *cookie)\n");
3312 oprintf (d.of, "{\n");
3313 oprintf (d.of, " %s %s * const x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3314 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3315 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3316 d.indent = 2;
3317 walk_type (s, &d);
3318 oprintf (d.of, "}\n");
3321 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3323 static void
3324 write_local (outf_p output_header, type_p structures, type_p param_structs)
3326 type_p s;
3328 if (!output_header)
3329 return;
3330 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3331 for (s = structures; s; s = s->next)
3332 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3334 options_p opt;
3336 if (s->u.s.line.file == NULL)
3337 continue;
3338 for (opt = s->u.s.opt; opt; opt = opt->next)
3339 if (strcmp (opt->name, "ptr_alias") == 0
3340 && opt->kind == OPTION_TYPE)
3342 const_type_p const t = (const_type_p) opt->info.type;
3343 if (t->kind == TYPE_STRUCT
3344 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3346 oprintf (output_header, "#define gt_pch_p_");
3347 output_mangled_typename (output_header, s);
3348 oprintf (output_header, " gt_pch_p_");
3349 output_mangled_typename (output_header, t);
3350 oprintf (output_header, "\n");
3352 else
3353 error_at_line (&s->u.s.line,
3354 "structure alias is not a structure");
3355 break;
3357 if (opt)
3358 continue;
3360 /* Declare the marker procedure only once. */
3361 oprintf (output_header, "extern void gt_pch_p_");
3362 output_mangled_typename (output_header, s);
3363 oprintf (output_header,
3364 "\n (void *, void *, gt_pointer_operator, void *);\n");
3366 if (s->kind == TYPE_LANG_STRUCT)
3368 type_p ss;
3369 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3370 write_local_func_for_structure (s, ss, NULL);
3372 else
3373 write_local_func_for_structure (s, s, NULL);
3376 for (s = param_structs; s; s = s->next)
3377 if (s->gc_used == GC_POINTED_TO)
3379 type_p *param = s->u.param_struct.param;
3380 type_p stru = s->u.param_struct.stru;
3382 /* Declare the marker procedure. */
3383 oprintf (output_header, "extern void gt_pch_p_");
3384 output_mangled_typename (output_header, s);
3385 oprintf (output_header,
3386 "\n (void *, void *, gt_pointer_operator, void *);\n");
3388 if (stru->u.s.line.file == NULL)
3390 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3391 s->u.s.tag);
3392 continue;
3395 if (stru->kind == TYPE_LANG_STRUCT)
3397 type_p ss;
3398 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3399 write_local_func_for_structure (s, ss, param);
3401 else
3402 write_local_func_for_structure (s, stru, param);
3406 /* Nonzero if S is a type for which typed GC allocators should be output. */
3408 #define USED_BY_TYPED_GC_P(s) \
3409 (((s->kind == TYPE_POINTER) \
3410 && ((s->u.p->gc_used == GC_POINTED_TO) \
3411 || (s->u.p->gc_used == GC_USED))) \
3412 || (UNION_OR_STRUCT_P (s) && \
3413 (((s)->gc_used == GC_POINTED_TO) \
3414 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
3415 && s->u.s.line.file != NULL) \
3416 || ((s)->gc_used == GC_USED \
3417 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
3420 /* Write out the 'enum' definition for gt_types_enum. */
3422 static void
3423 write_enum_defn (type_p structures, type_p param_structs)
3425 type_p s;
3426 int nbstruct = 0;
3427 int nbparamstruct = 0;
3429 if (!header_file)
3430 return;
3431 oprintf (header_file, "\n/* Enumeration of types known. */\n");
3432 oprintf (header_file, "enum gt_types_enum {\n");
3433 for (s = structures; s; s = s->next)
3434 if (USED_BY_TYPED_GC_P (s))
3436 nbstruct++;
3437 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
3438 (void*) s, nbstruct);
3439 if (UNION_OR_STRUCT_P (s))
3440 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
3441 (void*) s, nbstruct, s->u.s.tag);
3442 oprintf (header_file, " gt_ggc_e_");
3443 output_mangled_typename (header_file, s);
3444 oprintf (header_file, ",\n");
3446 for (s = param_structs; s; s = s->next)
3447 if (s->gc_used == GC_POINTED_TO)
3449 nbparamstruct++;
3450 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
3451 (void*) s, nbparamstruct);
3452 oprintf (header_file, " gt_e_");
3453 output_mangled_typename (header_file, s);
3454 oprintf (header_file, ",\n");
3456 oprintf (header_file, " gt_types_enum_last\n");
3457 oprintf (header_file, "};\n");
3458 if (verbosity_level >= 2)
3459 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
3460 progname, nbstruct, nbparamstruct);
3464 /* Might T contain any non-pointer elements? */
3466 static int
3467 contains_scalar_p (type_p t)
3469 switch (t->kind)
3471 case TYPE_STRING:
3472 case TYPE_POINTER:
3473 return 0;
3474 case TYPE_ARRAY:
3475 return contains_scalar_p (t->u.a.p);
3476 default:
3477 /* Could also check for structures that have no non-pointer
3478 fields, but there aren't enough of those to worry about. */
3479 return 1;
3483 /* Mangle INPF and print it to F. */
3485 static void
3486 put_mangled_filename (outf_p f, const input_file *inpf)
3488 /* The call to get_output_file_name may indirectly update fn since
3489 get_output_file_with_visibility caches its result inside, so we
3490 need the CONST_CAST. */
3491 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
3492 if (!f || !name)
3493 return;
3494 for (; *name != 0; name++)
3495 if (ISALNUM (*name))
3496 oprintf (f, "%c", *name);
3497 else
3498 oprintf (f, "%c", '_');
3501 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
3502 LASTNAME, and NAME are all strings to insert in various places in
3503 the resulting code. */
3505 static void
3506 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
3507 const char *tname, const char *name)
3509 struct flist *fli2;
3511 for (fli2 = flp; fli2; fli2 = fli2->next)
3512 if (fli2->started_p)
3514 oprintf (fli2->f, " %s\n", lastname);
3515 oprintf (fli2->f, "};\n\n");
3518 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
3519 if (fli2->started_p)
3521 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
3522 int fnum;
3524 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
3525 if (bitmap & 1)
3527 oprintf (base_files[fnum],
3528 "extern const struct %s gt_%s_", tname, pfx);
3529 put_mangled_filename (base_files[fnum], fli2->file);
3530 oprintf (base_files[fnum], "[];\n");
3535 size_t fnum;
3536 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3537 oprintf (base_files[fnum],
3538 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
3542 for (fli2 = flp; fli2; fli2 = fli2->next)
3543 if (fli2->started_p)
3545 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
3546 int fnum;
3548 fli2->started_p = 0;
3550 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
3551 if (bitmap & 1)
3553 oprintf (base_files[fnum], " gt_%s_", pfx);
3554 put_mangled_filename (base_files[fnum], fli2->file);
3555 oprintf (base_files[fnum], ",\n");
3560 size_t fnum;
3561 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3563 oprintf (base_files[fnum], " NULL\n");
3564 oprintf (base_files[fnum], "};\n");
3569 /* Write the first three fields (pointer, count and stride) for
3570 root NAME to F. V and LINE are as for write_root.
3572 Return true if the entry could be written; return false on error. */
3574 static bool
3575 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
3577 type_p ap;
3579 if (!v)
3581 error_at_line (line, "`%s' is too complex to be a root", name);
3582 return false;
3585 oprintf (f, " {\n");
3586 oprintf (f, " &%s,\n", name);
3587 oprintf (f, " 1");
3589 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3590 if (ap->u.a.len[0])
3591 oprintf (f, " * (%s)", ap->u.a.len);
3592 else if (ap == v->type)
3593 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
3594 oprintf (f, ",\n");
3595 oprintf (f, " sizeof (%s", v->name);
3596 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3597 oprintf (f, "[0]");
3598 oprintf (f, "),\n");
3599 return true;
3602 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
3603 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
3604 of the caller. */
3606 static void
3607 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
3608 int has_length, struct fileloc *line, const char *if_marked,
3609 bool emit_pch, type_p field_type, const char *field_name)
3611 /* If the field reference is relative to V, rather than to some
3612 subcomponent of V, we can mark any subarrays with a single stride.
3613 We're effectively treating the field as a global variable in its
3614 own right. */
3615 if (v && type == v->type)
3617 struct pair newv;
3619 newv = *v;
3620 newv.type = field_type;
3621 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
3622 v = &newv;
3624 /* Otherwise, any arrays nested in the structure are too complex to
3625 handle. */
3626 else if (field_type->kind == TYPE_ARRAY)
3627 v = NULL;
3628 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
3629 has_length, line, if_marked, emit_pch);
3632 /* Write out to F the table entry and any marker routines needed to
3633 mark NAME as TYPE. V can be one of three values:
3635 - null, if NAME is too complex to represent using a single
3636 count and stride. In this case, it is an error for NAME to
3637 contain any gc-ed data.
3639 - the outermost array that contains NAME, if NAME is part of an array.
3641 - the C variable that contains NAME, if NAME is not part of an array.
3643 LINE is the line of the C source that declares the root variable.
3644 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
3645 is nonzero iff we are building the root table for hash table caches. */
3647 static void
3648 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
3649 struct fileloc *line, const char *if_marked, bool emit_pch)
3651 switch (type->kind)
3653 case TYPE_STRUCT:
3655 pair_p fld;
3656 for (fld = type->u.s.fields; fld; fld = fld->next)
3658 int skip_p = 0;
3659 const char *desc = NULL;
3660 options_p o;
3662 for (o = fld->opt; o; o = o->next)
3663 if (strcmp (o->name, "skip") == 0)
3664 skip_p = 1;
3665 else if (strcmp (o->name, "desc") == 0
3666 && o->kind == OPTION_STRING)
3667 desc = o->info.string;
3668 else if (strcmp (o->name, "param_is") == 0)
3670 else
3671 error_at_line (line,
3672 "field `%s' of global `%s' has unknown option `%s'",
3673 fld->name, name, o->name);
3675 if (skip_p)
3676 continue;
3677 else if (desc && fld->type->kind == TYPE_UNION)
3679 pair_p validf = NULL;
3680 pair_p ufld;
3682 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
3684 const char *tag = NULL;
3685 options_p oo;
3686 for (oo = ufld->opt; oo; oo = oo->next)
3687 if (strcmp (oo->name, "tag") == 0
3688 && oo->kind == OPTION_STRING)
3689 tag = oo->info.string;
3690 if (tag == NULL || strcmp (tag, desc) != 0)
3691 continue;
3692 if (validf != NULL)
3693 error_at_line (line,
3694 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
3695 name, fld->name, validf->name,
3696 name, fld->name, ufld->name, tag);
3697 validf = ufld;
3699 if (validf != NULL)
3700 write_field_root (f, v, type, name, 0, line, if_marked,
3701 emit_pch, validf->type,
3702 ACONCAT ((fld->name, ".",
3703 validf->name, NULL)));
3705 else if (desc)
3706 error_at_line (line,
3707 "global `%s.%s' has `desc' option but is not union",
3708 name, fld->name);
3709 else
3710 write_field_root (f, v, type, name, 0, line, if_marked,
3711 emit_pch, fld->type, fld->name);
3714 break;
3716 case TYPE_ARRAY:
3718 char *newname;
3719 newname = xasprintf ("%s[0]", name);
3720 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
3721 emit_pch);
3722 free (newname);
3724 break;
3726 case TYPE_POINTER:
3728 type_p tp;
3730 if (!start_root_entry (f, v, name, line))
3731 return;
3733 tp = type->u.p;
3735 if (!has_length && UNION_OR_STRUCT_P (tp))
3737 oprintf (f, " &gt_ggc_mx_%s,\n", tp->u.s.tag);
3738 if (emit_pch)
3739 oprintf (f, " &gt_pch_nx_%s", tp->u.s.tag);
3740 else
3741 oprintf (f, " NULL");
3743 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
3745 oprintf (f, " &gt_ggc_m_");
3746 output_mangled_typename (f, tp);
3747 if (emit_pch)
3749 oprintf (f, ",\n &gt_pch_n_");
3750 output_mangled_typename (f, tp);
3752 else
3753 oprintf (f, ",\n NULL");
3755 else if (has_length
3756 && (tp->kind == TYPE_POINTER || UNION_OR_STRUCT_P (tp)))
3758 oprintf (f, " &gt_ggc_ma_%s,\n", name);
3759 if (emit_pch)
3760 oprintf (f, " &gt_pch_na_%s", name);
3761 else
3762 oprintf (f, " NULL");
3764 else
3766 error_at_line (line,
3767 "global `%s' is pointer to unimplemented type",
3768 name);
3770 if (if_marked)
3771 oprintf (f, ",\n &%s", if_marked);
3772 oprintf (f, "\n },\n");
3774 break;
3776 case TYPE_STRING:
3778 if (!start_root_entry (f, v, name, line))
3779 return;
3781 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
3782 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
3783 oprintf (f, " },\n");
3785 break;
3787 case TYPE_SCALAR:
3788 break;
3790 default:
3791 error_at_line (line, "global `%s' is unimplemented type", name);
3795 /* This generates a routine to walk an array. */
3797 static void
3798 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
3800 struct walk_type_data d;
3801 char *prevval3;
3803 memset (&d, 0, sizeof (d));
3804 d.of = f;
3805 d.cookie = wtd;
3806 d.indent = 2;
3807 d.line = &v->line;
3808 d.opt = v->opt;
3809 d.bitmap = get_lang_bitmap (v->line.file);
3810 d.param = NULL;
3812 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
3814 if (wtd->param_prefix)
3816 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
3817 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
3818 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
3819 wtd->param_prefix, v->name);
3820 oprintf (d.of,
3821 " ATTRIBUTE_UNUSED void *x_p,\n"
3822 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3823 " ATTRIBUTE_UNUSED void * cookie)\n");
3824 oprintf (d.of, "{\n");
3825 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3826 d.process_field = write_types_local_process_field;
3827 walk_type (v->type, &d);
3828 oprintf (f, "}\n\n");
3831 d.opt = v->opt;
3832 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
3833 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
3834 wtd->prefix, v->name);
3835 oprintf (f, "{\n");
3836 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3837 d.process_field = write_types_process_field;
3838 walk_type (v->type, &d);
3839 free (prevval3);
3840 oprintf (f, "}\n\n");
3843 /* Output a table describing the locations and types of VARIABLES. */
3845 static void
3846 write_roots (pair_p variables, bool emit_pch)
3848 pair_p v;
3849 struct flist *flp = NULL;
3851 for (v = variables; v; v = v->next)
3853 outf_p f =
3854 get_output_file_with_visibility (CONST_CAST (input_file*,
3855 v->line.file));
3856 struct flist *fli;
3857 const char *length = NULL;
3858 int deletable_p = 0;
3859 options_p o;
3860 for (o = v->opt; o; o = o->next)
3861 if (strcmp (o->name, "length") == 0
3862 && o->kind == OPTION_STRING)
3863 length = o->info.string;
3864 else if (strcmp (o->name, "deletable") == 0)
3865 deletable_p = 1;
3866 else if (strcmp (o->name, "param_is") == 0)
3868 else if (strncmp (o->name, "param", 5) == 0
3869 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
3871 else if (strcmp (o->name, "if_marked") == 0)
3873 else
3874 error_at_line (&v->line,
3875 "global `%s' has unknown option `%s'",
3876 v->name, o->name);
3878 for (fli = flp; fli; fli = fli->next)
3879 if (fli->f == f && f)
3880 break;
3881 if (fli == NULL)
3883 fli = XNEW (struct flist);
3884 fli->f = f;
3885 fli->next = flp;
3886 fli->started_p = 0;
3887 fli->file = v->line.file;
3888 gcc_assert (fli->file);
3889 flp = fli;
3891 oprintf (f, "\n/* GC roots. */\n\n");
3894 if (!deletable_p
3895 && length
3896 && v->type->kind == TYPE_POINTER
3897 && (v->type->u.p->kind == TYPE_POINTER
3898 || v->type->u.p->kind == TYPE_STRUCT))
3900 write_array (f, v, &ggc_wtd);
3901 write_array (f, v, &pch_wtd);
3905 for (v = variables; v; v = v->next)
3907 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
3908 v->line.file));
3909 struct flist *fli;
3910 int skip_p = 0;
3911 int length_p = 0;
3912 options_p o;
3914 for (o = v->opt; o; o = o->next)
3915 if (strcmp (o->name, "length") == 0)
3916 length_p = 1;
3917 else if (strcmp (o->name, "deletable") == 0
3918 || strcmp (o->name, "if_marked") == 0)
3919 skip_p = 1;
3921 if (skip_p)
3922 continue;
3924 for (fli = flp; fli; fli = fli->next)
3925 if (fli->f == f)
3926 break;
3927 if (!fli->started_p)
3929 fli->started_p = 1;
3931 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
3932 put_mangled_filename (f, v->line.file);
3933 oprintf (f, "[] = {\n");
3936 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
3939 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3940 "gt_ggc_rtab");
3942 for (v = variables; v; v = v->next)
3944 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
3945 v->line.file));
3946 struct flist *fli;
3947 int skip_p = 1;
3948 options_p o;
3950 for (o = v->opt; o; o = o->next)
3951 if (strcmp (o->name, "deletable") == 0)
3952 skip_p = 0;
3953 else if (strcmp (o->name, "if_marked") == 0)
3954 skip_p = 1;
3956 if (skip_p)
3957 continue;
3959 for (fli = flp; fli; fli = fli->next)
3960 if (fli->f == f)
3961 break;
3962 if (!fli->started_p)
3964 fli->started_p = 1;
3966 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
3967 put_mangled_filename (f, v->line.file);
3968 oprintf (f, "[] = {\n");
3971 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3972 v->name, v->name);
3975 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3976 "gt_ggc_deletable_rtab");
3978 for (v = variables; v; v = v->next)
3980 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
3981 v->line.file));
3982 struct flist *fli;
3983 const char *if_marked = NULL;
3984 int length_p = 0;
3985 options_p o;
3987 for (o = v->opt; o; o = o->next)
3988 if (strcmp (o->name, "length") == 0)
3989 length_p = 1;
3990 else if (strcmp (o->name, "if_marked") == 0
3991 && o->kind == OPTION_STRING)
3992 if_marked = o->info.string;
3993 if (if_marked == NULL)
3994 continue;
3995 if (v->type->kind != TYPE_POINTER
3996 || v->type->u.p->kind != TYPE_PARAM_STRUCT
3997 || v->type->u.p->u.param_struct.stru != find_structure ("htab", 0))
3999 error_at_line (&v->line,
4000 "if_marked option used but not hash table");
4001 continue;
4004 for (fli = flp; fli; fli = fli->next)
4005 if (fli->f == f)
4006 break;
4007 if (!fli->started_p)
4009 fli->started_p = 1;
4011 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4012 put_mangled_filename (f, v->line.file);
4013 oprintf (f, "[] = {\n");
4016 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4017 v->name, length_p, &v->line, if_marked, emit_pch);
4020 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4021 "gt_ggc_cache_rtab");
4023 if (!emit_pch)
4024 return;
4026 for (v = variables; v; v = v->next)
4028 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4029 v->line.file));
4030 struct flist *fli;
4031 int length_p = 0;
4032 int if_marked_p = 0;
4033 options_p o;
4035 for (o = v->opt; o; o = o->next)
4036 if (strcmp (o->name, "length") == 0)
4037 length_p = 1;
4038 else if (strcmp (o->name, "if_marked") == 0)
4039 if_marked_p = 1;
4041 if (!if_marked_p)
4042 continue;
4044 for (fli = flp; fli; fli = fli->next)
4045 if (fli->f == f)
4046 break;
4047 if (!fli->started_p)
4049 fli->started_p = 1;
4051 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4052 put_mangled_filename (f, v->line.file);
4053 oprintf (f, "[] = {\n");
4056 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4059 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4060 "gt_pch_cache_rtab");
4062 for (v = variables; v; v = v->next)
4064 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4065 v->line.file));
4066 struct flist *fli;
4067 int skip_p = 0;
4068 options_p o;
4070 for (o = v->opt; o; o = o->next)
4071 if (strcmp (o->name, "deletable") == 0
4072 || strcmp (o->name, "if_marked") == 0)
4073 skip_p = 1;
4075 if (skip_p)
4076 continue;
4078 if (!contains_scalar_p (v->type))
4079 continue;
4081 for (fli = flp; fli; fli = fli->next)
4082 if (fli->f == f)
4083 break;
4084 if (!fli->started_p)
4086 fli->started_p = 1;
4088 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4089 put_mangled_filename (f, v->line.file);
4090 oprintf (f, "[] = {\n");
4093 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4094 v->name, v->name);
4097 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4098 "gt_pch_scalar_rtab");
4101 /* Record the definition of a generic VEC structure, as if we had expanded
4102 the macros in vec.h:
4104 typedef struct VEC_<type>_base GTY(()) {
4105 unsigned num;
4106 unsigned alloc;
4107 <type> GTY((length ("%h.num"))) vec[1];
4108 } VEC_<type>_base
4110 where the GTY(()) tags are only present if is_scalar is _false_. */
4112 void
4113 note_def_vec (const char *type_name, bool is_scalar, struct fileloc *pos)
4115 pair_p fields;
4116 type_p t;
4117 options_p o;
4118 type_p len_ty = create_scalar_type ("unsigned");
4119 const char *name = concat ("VEC_", type_name, "_base", (char *) 0);
4121 if (is_scalar)
4123 t = create_scalar_type (type_name);
4124 o = 0;
4126 else
4128 t = resolve_typedef (type_name, pos);
4129 o = create_string_option (0, "length", "%h.num");
4131 /* We assemble the field list in reverse order. */
4132 fields = create_field_at (0, create_array (t, "1"), "vec", o, pos);
4133 fields = create_field_at (fields, len_ty, "alloc", 0, pos);
4134 fields = create_field_at (fields, len_ty, "num", 0, pos);
4136 do_typedef (name, new_structure (name, 0, pos, fields, 0), pos);
4139 /* Record the definition of an allocation-specific VEC structure, as if
4140 we had expanded the macros in vec.h:
4142 typedef struct VEC_<type>_<astrat> {
4143 VEC_<type>_base base;
4144 } VEC_<type>_<astrat>;
4146 void
4147 note_def_vec_alloc (const char *type, const char *astrat, struct fileloc *pos)
4149 const char *astratname = concat ("VEC_", type, "_", astrat, (char *) 0);
4150 const char *basename = concat ("VEC_", type, "_base", (char *) 0);
4152 pair_p field = create_field_at (0, resolve_typedef (basename, pos),
4153 "base", 0, pos);
4155 do_typedef (astratname, new_structure (astratname, 0, pos, field, 0), pos);
4158 /* Returns the specifier keyword for a string or union type S, empty string
4159 otherwise. */
4161 static const char *
4162 get_type_specifier (const type_p s)
4164 if (s->kind == TYPE_STRUCT || s->kind == TYPE_LANG_STRUCT)
4165 return "struct ";
4166 if (s->kind == TYPE_UNION)
4167 return "union ";
4168 return "";
4171 /* TRUE if type S has the GTY variable_size annotation. */
4173 static bool
4174 variable_size_p (const type_p s)
4176 options_p o;
4177 for (o = s->u.s.opt; o; o = o->next)
4178 if (strcmp (o->name, "variable_size") == 0)
4179 return true;
4180 return false;
4183 enum alloc_quantity
4184 { single, vector };
4185 enum alloc_zone
4186 { any_zone, specific_zone };
4188 /* Writes one typed allocator definition for type identifier TYPE_NAME with
4189 optional type specifier TYPE_SPECIFIER. The allocator name will contain
4190 ALLOCATOR_TYPE. If VARIABLE_SIZE is true, the allocator will have an extra
4191 parameter specifying number of bytes to allocate. If QUANTITY is set to
4192 VECTOR, a vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
4193 the allocator will be zone-specific. */
4195 static void
4196 write_typed_alloc_def (bool variable_size, const char *type_specifier,
4197 const char *type_name, const char *allocator_type,
4198 enum alloc_quantity quantity, enum alloc_zone zone)
4200 bool two_args = variable_size && (quantity == vector);
4201 bool third_arg = ((zone == specific_zone)
4202 && (variable_size || (quantity == vector)));
4204 oprintf (header_file, "#define ggc_alloc_%s%s", allocator_type, type_name);
4205 oprintf (header_file, "(%s%s%s%s%s) ",
4206 (variable_size ? "SIZE" : ""),
4207 (two_args ? ", " : ""),
4208 (quantity == vector) ? "n" : "",
4209 (third_arg ? ", " : ""), (zone == specific_zone) ? "z" : "");
4210 oprintf (header_file, "((%s%s *)", type_specifier, type_name);
4211 oprintf (header_file, "(ggc_internal_%salloc_stat (", allocator_type);
4212 if (zone == specific_zone)
4213 oprintf (header_file, "z, ");
4214 if (variable_size)
4215 oprintf (header_file, "SIZE");
4216 else
4217 oprintf (header_file, "sizeof (%s%s)", type_specifier, type_name);
4218 if (quantity == vector)
4219 oprintf (header_file, ", n");
4220 oprintf (header_file, " MEM_STAT_INFO)))\n");
4223 /* Writes a typed allocator definition for a struct or union S. */
4225 static void
4226 write_typed_struct_alloc_def (const type_p s, const char *allocator_type,
4227 enum alloc_quantity quantity,
4228 enum alloc_zone zone)
4230 write_typed_alloc_def (variable_size_p (s), get_type_specifier (s),
4231 s->u.s.tag, allocator_type, quantity, zone);
4234 /* Writes a typed allocator definition for a typedef P. */
4236 static void
4237 write_typed_typedef_alloc_def (const pair_p p, const char *allocator_type,
4238 enum alloc_quantity quantity,
4239 enum alloc_zone zone)
4241 write_typed_alloc_def (variable_size_p (p->type), "", p->name,
4242 allocator_type, quantity, zone);
4245 /* Writes typed allocator definitions for the types in STRUCTURES and
4246 TYPEDEFS that are used by GC. */
4248 static void
4249 write_typed_alloc_defns (const type_p structures, const pair_p typedefs)
4251 type_p s;
4252 pair_p p;
4254 oprintf (header_file,
4255 "\n/* Allocators for known structs and unions. */\n\n");
4256 for (s = structures; s; s = s->next)
4258 if (!USED_BY_TYPED_GC_P (s))
4259 continue;
4260 write_typed_struct_alloc_def (s, "", single, any_zone);
4261 write_typed_struct_alloc_def (s, "cleared_", single, any_zone);
4262 write_typed_struct_alloc_def (s, "vec_", vector, any_zone);
4263 write_typed_struct_alloc_def (s, "cleared_vec_", vector, any_zone);
4264 write_typed_struct_alloc_def (s, "zone_", single, specific_zone);
4265 write_typed_struct_alloc_def (s, "zone_cleared_", single,
4266 specific_zone);
4267 write_typed_struct_alloc_def (s, "zone_vec_", vector, specific_zone);
4268 write_typed_struct_alloc_def (s, "zone_cleared_vec_", vector,
4269 specific_zone);
4272 oprintf (header_file, "\n/* Allocators for known typedefs. */\n");
4273 for (p = typedefs; p; p = p->next)
4275 s = p->type;
4276 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4277 continue;
4278 write_typed_typedef_alloc_def (p, "", single, any_zone);
4279 write_typed_typedef_alloc_def (p, "cleared_", single, any_zone);
4280 write_typed_typedef_alloc_def (p, "vec_", vector, any_zone);
4281 write_typed_typedef_alloc_def (p, "cleared_vec_", vector, any_zone);
4282 write_typed_typedef_alloc_def (p, "zone_", single, specific_zone);
4283 write_typed_typedef_alloc_def (p, "zone_cleared_", single,
4284 specific_zone);
4285 write_typed_typedef_alloc_def (p, "zone_cleared_vec_", vector,
4286 specific_zone);
4290 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4291 guaranteee for somewhat increased readability. If name conflicts do happen,
4292 this funcion will have to be adjusted to be more like
4293 output_mangled_typename. */
4295 static void
4296 output_typename (outf_p of, const_type_p t)
4298 switch (t->kind)
4300 case TYPE_STRING:
4301 oprintf (of, "str");
4302 break;
4303 case TYPE_SCALAR:
4304 oprintf (of, "scalar");
4305 break;
4306 case TYPE_POINTER:
4307 output_typename (of, t->u.p);
4308 break;
4309 case TYPE_STRUCT:
4310 case TYPE_UNION:
4311 case TYPE_LANG_STRUCT:
4312 oprintf (of, "%s", t->u.s.tag);
4313 break;
4314 case TYPE_PARAM_STRUCT:
4316 int i;
4317 for (i = 0; i < NUM_PARAM; i++)
4318 if (t->u.param_struct.param[i] != NULL)
4320 output_typename (of, t->u.param_struct.param[i]);
4321 oprintf (of, "_");
4323 output_typename (of, t->u.param_struct.stru);
4324 break;
4326 default:
4327 gcc_unreachable ();
4331 /* Writes a typed GC allocator for type S that is suitable as a callback for
4332 the splay tree implementation in libiberty. */
4334 static void
4335 write_splay_tree_allocator_def (const_type_p s)
4337 outf_p of = get_output_file_with_visibility (NULL);
4338 oprintf (of, "void * ggc_alloc_splay_tree_");
4339 output_typename (of, s);
4340 oprintf (of, " (int sz, void * nl)\n");
4341 oprintf (of, "{\n");
4342 oprintf (of, " return ggc_splay_alloc (");
4343 oprintf (of, "gt_e_");
4344 output_mangled_typename (of, s);
4345 oprintf (of, ", sz, nl);\n");
4346 oprintf (of, "}\n\n");
4349 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4350 for the splay tree implementation in libiberty. */
4352 static void
4353 write_splay_tree_allocators (const_type_p param_structs)
4355 const_type_p s;
4357 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4358 for (s = param_structs; s; s = s->next)
4359 if (s->gc_used == GC_POINTED_TO)
4361 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4362 output_typename (header_file, s);
4363 oprintf (header_file, " (int, void *);\n");
4364 write_splay_tree_allocator_def (s);
4368 static void dump_pair (int indent, pair_p p);
4369 static void dump_type (int indent, type_p p);
4370 static void dump_type_list (int indent, type_p p);
4372 #define INDENT 2
4374 /* Dumps the value of typekind KIND. */
4376 static void
4377 dump_typekind (int indent, enum typekind kind)
4379 printf ("%*ckind = ", indent, ' ');
4380 switch (kind)
4382 case TYPE_SCALAR:
4383 printf ("TYPE_SCALAR");
4384 break;
4385 case TYPE_STRING:
4386 printf ("TYPE_STRING");
4387 break;
4388 case TYPE_STRUCT:
4389 printf ("TYPE_STRUCT");
4390 break;
4391 case TYPE_UNION:
4392 printf ("TYPE_UNION");
4393 break;
4394 case TYPE_POINTER:
4395 printf ("TYPE_POINTER");
4396 break;
4397 case TYPE_ARRAY:
4398 printf ("TYPE_ARRAY");
4399 break;
4400 case TYPE_LANG_STRUCT:
4401 printf ("TYPE_LANG_STRUCT");
4402 break;
4403 case TYPE_PARAM_STRUCT:
4404 printf ("TYPE_PARAM_STRUCT");
4405 break;
4406 default:
4407 gcc_unreachable ();
4409 printf ("\n");
4412 /* Dumps the value of GC_USED flag. */
4414 static void
4415 dump_gc_used (int indent, enum gc_used_enum gc_used)
4417 printf ("%*cgc_used = ", indent, ' ');
4418 switch (gc_used)
4420 case GC_UNUSED:
4421 printf ("GC_UNUSED");
4422 break;
4423 case GC_USED:
4424 printf ("GC_USED");
4425 break;
4426 case GC_MAYBE_POINTED_TO:
4427 printf ("GC_MAYBE_POINTED_TO");
4428 break;
4429 case GC_POINTED_TO:
4430 printf ("GC_POINTED_TO");
4431 break;
4432 default:
4433 gcc_unreachable ();
4435 printf ("\n");
4438 /* Dumps the type options OPT. */
4440 static void
4441 dump_options (int indent, options_p opt)
4443 options_p o;
4444 printf ("%*coptions = ", indent, ' ');
4445 o = opt;
4446 while (o)
4448 switch (o->kind)
4450 case OPTION_STRING:
4451 printf ("%s:string %s ", o->name, o->info.string);
4452 break;
4453 case OPTION_TYPE:
4454 printf ("%s:type ", o->name);
4455 dump_type (indent+1, o->info.type);
4456 break;
4457 case OPTION_NESTED:
4458 printf ("%s:nested ", o->name);
4459 break;
4460 case OPTION_NONE:
4461 gcc_unreachable ();
4463 o = o->next;
4465 printf ("\n");
4468 /* Dumps the source file location in LINE. */
4470 static void
4471 dump_fileloc (int indent, struct fileloc line)
4473 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
4474 get_input_file_name (line.file),
4475 line.line);
4478 /* Recursively dumps the struct, union, or a language-specific
4479 struct T. */
4481 static void
4482 dump_type_u_s (int indent, type_p t)
4484 pair_p fields;
4486 gcc_assert (t->kind == TYPE_STRUCT || t->kind == TYPE_UNION
4487 || t->kind == TYPE_LANG_STRUCT);
4488 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4489 dump_fileloc (indent, t->u.s.line);
4490 printf ("%*cu.s.fields =\n", indent, ' ');
4491 fields = t->u.s.fields;
4492 while (fields)
4494 dump_pair (indent + INDENT, fields);
4495 fields = fields->next;
4497 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4498 dump_options (indent, t->u.s.opt);
4499 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4500 if (t->kind == TYPE_LANG_STRUCT)
4502 printf ("%*cu.s.lang_struct:\n", indent, ' ');
4503 dump_type_list (indent + INDENT, t->u.s.lang_struct);
4507 /* Recursively dumps the array T. */
4509 static void
4510 dump_type_u_a (int indent, type_p t)
4512 gcc_assert (t->kind == TYPE_ARRAY);
4513 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4514 dump_type_list (indent + INDENT, t->u.a.p);
4517 /* Recursively dumps the parameterized struct T. */
4519 static void
4520 dump_type_u_param_struct (int indent, type_p t)
4522 int i;
4523 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
4524 printf ("%*cu.param_struct.stru:\n", indent, ' ');
4525 dump_type_list (indent, t->u.param_struct.stru);
4526 dump_fileloc (indent, t->u.param_struct.line);
4527 for (i = 0; i < NUM_PARAM; i++)
4529 if (t->u.param_struct.param[i] == NULL)
4530 continue;
4531 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
4532 dump_type (indent + INDENT, t->u.param_struct.param[i]);
4536 /* Recursively dumps the type list T. */
4538 static void
4539 dump_type_list (int indent, type_p t)
4541 type_p p = t;
4542 while (p)
4544 dump_type (indent, p);
4545 p = p->next;
4549 static htab_t seen_types;
4551 /* Recursively dumps the type T if it was not dumped previously. */
4553 static void
4554 dump_type (int indent, type_p t)
4556 PTR *slot;
4558 printf ("%*cType at %p: ", indent, ' ', (void *) t);
4559 slot = htab_find_slot (seen_types, t, INSERT);
4560 if (*slot != NULL)
4562 printf ("already seen.\n");
4563 return;
4565 *slot = t;
4566 printf ("\n");
4568 dump_typekind (indent, t->kind);
4569 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4570 (void *) t->pointer_to);
4571 dump_gc_used (indent + INDENT, t->gc_used);
4572 switch (t->kind)
4574 case TYPE_SCALAR:
4575 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4576 t->u.scalar_is_char ? "true" : "false");
4577 break;
4578 case TYPE_STRING:
4579 break;
4580 case TYPE_STRUCT:
4581 case TYPE_UNION:
4582 case TYPE_LANG_STRUCT:
4583 dump_type_u_s (indent + INDENT, t);
4584 break;
4585 case TYPE_POINTER:
4586 printf ("%*cp:\n", indent + INDENT, ' ');
4587 dump_type (indent + INDENT, t->u.p);
4588 break;
4589 case TYPE_ARRAY:
4590 dump_type_u_a (indent + INDENT, t);
4591 break;
4592 case TYPE_PARAM_STRUCT:
4593 dump_type_u_param_struct (indent + INDENT, t);
4594 break;
4595 default:
4596 gcc_unreachable ();
4598 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4601 /* Dumps the pair P. */
4603 static void
4604 dump_pair (int indent, pair_p p)
4606 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4607 dump_type (indent, p->type);
4608 dump_fileloc (indent, p->line);
4609 dump_options (indent, p->opt);
4610 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4613 /* Dumps the list of pairs PP. */
4615 static void
4616 dump_pair_list (const char *name, pair_p pp)
4618 pair_p p;
4619 printf ("%s:\n", name);
4620 for (p = pp; p != NULL; p = p->next)
4621 dump_pair (0, p);
4622 printf ("End of %s\n\n", name);
4625 /* Dumps the STRUCTURES. */
4627 static void
4628 dump_structures (const char *name, type_p structures)
4630 printf ("%s:\n", name);
4631 dump_type_list (0, structures);
4632 printf ("End of %s\n\n", name);
4635 /* Dumps the internal structures of gengtype. This is useful to debug
4636 gengtype itself, or to understand what it does, e.g. for plugin
4637 developers. */
4639 static void
4640 dump_everything (void)
4642 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4643 dump_pair_list ("typedefs", typedefs);
4644 dump_structures ("structures", structures);
4645 dump_structures ("param_structs", param_structs);
4646 dump_pair_list ("variables", variables);
4647 htab_delete (seen_types);
4652 /* Option specification for getopt_long. */
4653 static const struct option gengtype_long_options[] = {
4654 {"help", no_argument, NULL, 'h'},
4655 {"version", no_argument, NULL, 'V'},
4656 {"verbose", no_argument, NULL, 'v'},
4657 {"dump", no_argument, NULL, 'd'},
4658 {"debug", no_argument, NULL, 'D'},
4659 {"plugin", required_argument, NULL, 'P'},
4660 {"srcdir", required_argument, NULL, 'S'},
4661 {"backupdir", required_argument, NULL, 'B'},
4662 {"inputs", required_argument, NULL, 'I'},
4663 {"read-state", required_argument, NULL, 'r'},
4664 {"write-state", required_argument, NULL, 'w'},
4665 /* Terminating NULL placeholder. */
4666 {NULL, no_argument, NULL, 0},
4670 static void
4671 print_usage (void)
4673 printf ("Usage: %s\n", progname);
4674 printf ("\t -h | --help " " \t# Give this help.\n");
4675 printf ("\t -D | --debug "
4676 " \t# Give debug output to debug %s itself.\n", progname);
4677 printf ("\t -V | --version " " \t# Give version information.\n");
4678 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
4679 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4680 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4681 " \t# Generate for plugin.\n");
4682 printf ("\t -S | --srcdir <GCC-directory> "
4683 " \t# Specify the GCC source directory.\n");
4684 printf ("\t -B | --backupdir <directory> "
4685 " \t# Specify the backup directory for updated files.\n");
4686 printf ("\t -I | --inputs <input-list> "
4687 " \t# Specify the file with source files list.\n");
4688 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
4689 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
4692 static void
4693 print_version (void)
4695 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
4696 printf ("Report bugs: %s\n", bug_report_url);
4699 /* Parse the program options using getopt_long... */
4700 static void
4701 parse_program_options (int argc, char **argv)
4703 int opt = -1;
4704 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
4705 gengtype_long_options, NULL)) >= 0)
4707 switch (opt)
4709 case 'h': /* --help */
4710 print_usage ();
4711 break;
4712 case 'V': /* --version */
4713 print_version ();
4714 break;
4715 case 'd': /* --dump */
4716 do_dump = 1;
4717 break;
4718 case 'D': /* --debug */
4719 do_debug = 1;
4720 break;
4721 case 'v': /* --verbose */
4722 verbosity_level++;
4723 break;
4724 case 'P': /* --plugin */
4725 if (optarg)
4726 plugin_output_filename = optarg;
4727 else
4728 fatal ("missing plugin output file name");
4729 break;
4730 case 'S': /* --srcdir */
4731 if (optarg)
4732 srcdir = optarg;
4733 else
4734 fatal ("missing source directory");
4735 srcdir_len = strlen (srcdir);
4736 break;
4737 case 'B': /* --backupdir */
4738 if (optarg)
4739 backup_dir = optarg;
4740 else
4741 fatal ("missing backup directory");
4742 break;
4743 case 'I': /* --inputs */
4744 if (optarg)
4745 inputlist = optarg;
4746 else
4747 fatal ("missing input list");
4748 break;
4749 case 'r': /* --read-state */
4750 if (optarg)
4751 read_state_filename = optarg;
4752 else
4753 fatal ("missing read state file");
4754 DBGPRINTF ("read state %s\n", optarg);
4755 break;
4756 case 'w': /* --write-state */
4757 DBGPRINTF ("write state %s\n", optarg);
4758 if (optarg)
4759 write_state_filename = optarg;
4760 else
4761 fatal ("missing write state file");
4762 break;
4763 default:
4764 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
4765 print_usage ();
4766 fatal ("unexpected flag");
4769 if (plugin_output_filename)
4771 /* In plugin mode we require some input files. */
4772 int i = 0;
4773 if (optind >= argc)
4774 fatal ("no source files given in plugin mode");
4775 nb_plugin_files = argc - optind;
4776 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
4777 for (i = 0; i < (int) nb_plugin_files; i++)
4779 char *name = argv[i + optind];
4780 plugin_files[i] = input_file_by_name (name);
4787 /******* Manage input files. ******/
4789 /* Hash table of unique input file names. */
4790 static htab_t input_file_htab;
4792 /* Find or allocate a new input_file by hash-consing it. */
4793 input_file*
4794 input_file_by_name (const char* name)
4796 PTR* slot;
4797 input_file* f = NULL;
4798 int namlen = 0;
4799 if (!name)
4800 return NULL;
4801 namlen = strlen (name);
4802 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
4803 f->inpbitmap = 0;
4804 f->inpoutf = NULL;
4805 strcpy (f->inpname, name);
4806 slot = htab_find_slot (input_file_htab, f, INSERT);
4807 gcc_assert (slot != NULL);
4808 if (*slot)
4810 /* Already known input file. */
4811 free (f);
4812 return (input_file*)(*slot);
4814 /* New input file. */
4815 *slot = f;
4816 return f;
4819 /* Hash table support routines for input_file-s. */
4820 static hashval_t
4821 htab_hash_inputfile (const void *p)
4823 const input_file *inpf = (const input_file *) p;
4824 gcc_assert (inpf);
4825 return htab_hash_string (get_input_file_name (inpf));
4828 static int
4829 htab_eq_inputfile (const void *x, const void *y)
4831 const input_file *inpfx = (const input_file *) x;
4832 const input_file *inpfy = (const input_file *) y;
4833 gcc_assert (inpfx != NULL && inpfy != NULL);
4834 return !strcmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
4839 main (int argc, char **argv)
4841 size_t i;
4842 static struct fileloc pos = { NULL, 0 };
4843 outf_p output_header;
4845 /* Mandatory common initializations. */
4846 progname = "gengtype"; /* For fatal and messages. */
4847 /* Create the hash-table used to hash-cons input files. */
4848 input_file_htab =
4849 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
4850 /* Initialize our special input files. */
4851 this_file = input_file_by_name (__FILE__);
4852 system_h_file = input_file_by_name ("system.h");
4853 /* Set the scalar_is_char union number for predefined scalar types. */
4854 scalar_nonchar.u.scalar_is_char = FALSE;
4855 scalar_char.u.scalar_is_char = TRUE;
4857 parse_program_options (argc, argv);
4859 #if ENABLE_CHECKING
4860 if (do_debug)
4862 time_t now = (time_t) 0;
4863 time (&now);
4864 DBGPRINTF ("gengtype started pid %d at %s",
4865 (int) getpid (), ctime (&now));
4867 #endif /* ENABLE_CHECKING */
4869 /* Parse the input list and the input files. */
4870 DBGPRINTF ("inputlist %s", inputlist);
4871 if (read_state_filename)
4873 if (inputlist)
4874 fatal ("input list %s cannot be given with a read state file %s",
4875 inputlist, read_state_filename);
4876 read_state (read_state_filename);
4877 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
4878 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
4880 else if (inputlist)
4882 /* These types are set up with #define or else outside of where
4883 we can see them. We should initialize them before calling
4884 read_input_list. */
4885 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
4886 Call;} while(0)
4887 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
4888 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
4889 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
4890 POS_HERE (do_scalar_typedef ("double_int", &pos));
4891 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
4892 POS_HERE (do_scalar_typedef ("uint8", &pos));
4893 POS_HERE (do_scalar_typedef ("jword", &pos));
4894 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
4895 POS_HERE (do_scalar_typedef ("void", &pos));
4896 POS_HERE (do_typedef ("PTR",
4897 create_pointer (resolve_typedef ("void", &pos)),
4898 &pos));
4899 #undef POS_HERE
4900 read_input_list (inputlist);
4901 for (i = 0; i < num_gt_files; i++)
4903 parse_file (get_input_file_name (gt_files[i]));
4904 DBGPRINTF ("parsed file #%d %s",
4905 (int) i, get_input_file_name (gt_files[i]));
4907 if (verbosity_level >= 1)
4908 printf ("%s parsed %d files with %d GTY types\n",
4909 progname, (int) num_gt_files, type_count);
4911 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
4912 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
4915 else
4916 fatal ("either an input list or a read state file should be given");
4917 if (hit_error)
4918 return 1;
4921 if (plugin_output_filename)
4923 size_t ix = 0;
4924 /* In plugin mode, we should have read a state file, and have
4925 given at least one plugin file. */
4926 if (!read_state_filename)
4927 fatal ("No read state given in plugin mode for %s",
4928 plugin_output_filename);
4930 if (nb_plugin_files == 0 || !plugin_files)
4931 fatal ("No plugin files given in plugin mode for %s",
4932 plugin_output_filename);
4934 /* Parse our plugin files and augment the state. */
4935 for (ix = 0; ix < nb_plugin_files; ix++)
4936 parse_file (get_input_file_name (plugin_files[ix]));
4938 if (hit_error)
4939 return 1;
4941 plugin_output = create_file ("GCC", plugin_output_filename);
4942 DBGPRINTF ("created plugin_output %p named %s",
4943 (void *) plugin_output, plugin_output->name);
4945 else
4946 { /* No plugin files, we are in normal mode. */
4947 if (!srcdir)
4948 fatal ("gengtype needs a source directory in normal mode");
4950 if (hit_error)
4951 return 1;
4953 gen_rtx_next ();
4955 /* The call to set_gc_used may indirectly call find_param_structure
4956 hence enlarge the param_structs list of types. */
4957 set_gc_used (variables);
4959 /* The state at this point is read from the state input file or by
4960 parsing source files and optionally augmented by parsing plugin
4961 source files. Write it now. */
4962 if (write_state_filename)
4964 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
4965 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
4967 if (hit_error)
4968 fatal ("didn't write state file %s after errors",
4969 write_state_filename);
4971 DBGPRINTF ("before write_state %s", write_state_filename);
4972 write_state (write_state_filename);
4974 if (do_dump)
4975 dump_everything ();
4977 /* After having written the state file we return immediately to
4978 avoid generating any output file. */
4979 if (hit_error)
4980 return 1;
4981 else
4982 return 0;
4986 open_base_files ();
4988 write_enum_defn (structures, param_structs);
4989 write_typed_alloc_defns (structures, typedefs);
4990 output_header = plugin_output ? plugin_output : header_file;
4991 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
4992 structures);
4993 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
4994 param_structs);
4996 write_types (output_header, structures, param_structs, &ggc_wtd);
4997 if (plugin_files == NULL)
4999 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5000 structures);
5001 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5002 param_structs);
5003 write_types (header_file, structures, param_structs, &pch_wtd);
5004 write_local (header_file, structures, param_structs);
5006 write_splay_tree_allocators (param_structs);
5007 write_roots (variables, plugin_files == NULL);
5008 write_rtx_next ();
5009 close_output_files ();
5011 if (do_dump)
5012 dump_everything ();
5014 /* Don't bother about free-ing any input or plugin file, etc. */
5016 if (hit_error)
5017 return 1;
5018 return 0;