]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gengtype.c
2010-10-14 Jeremie Salvucci <jeremie.salvucci@free.fr>
[thirdparty/gcc.git] / gcc / gengtype.c
1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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 "gengtype.h"
29
30 /* Data types, macros, etc. used only in this file. */
31
32 /* Kinds of types we can understand. */
33 enum typekind
34 {
35 TYPE_SCALAR,
36 TYPE_STRING,
37 TYPE_STRUCT,
38 TYPE_UNION,
39 TYPE_POINTER,
40 TYPE_ARRAY,
41 TYPE_LANG_STRUCT,
42 TYPE_PARAM_STRUCT
43 };
44
45
46 /* A way to pass data through to the output end. */
47 struct options
48 {
49 struct options *next;
50 const char *name;
51 const char *info;
52 };
53
54 /* Option data for the 'nested_ptr' option. */
55 struct nested_ptr_data
56 {
57 type_p type;
58 const char *convert_to;
59 const char *convert_from;
60 };
61
62 /* A name and a type. */
63 struct pair
64 {
65 pair_p next;
66 const char *name;
67 type_p type;
68 struct fileloc line;
69 options_p opt;
70 };
71
72 #define NUM_PARAM 10
73
74 /* A description of a type. */
75 enum gc_used_enum
76 {
77 GC_UNUSED = 0,
78 GC_USED,
79 /* Used for structures whose definitions we haven't seen so far when
80 we encounter a pointer to it that is annotated with ``maybe_undef''.
81 If after reading in everything we don't have source file
82 information for it, we assume that it never has been defined. */
83 GC_MAYBE_POINTED_TO,
84 GC_POINTED_TO
85 };
86
87 struct type
88 {
89 enum typekind kind;
90 type_p next;
91 type_p pointer_to;
92 enum gc_used_enum gc_used;
93 union
94 {
95 type_p p;
96 struct
97 {
98 const char *tag;
99 struct fileloc line;
100 pair_p fields;
101 options_p opt;
102 lang_bitmap bitmap;
103 type_p lang_struct;
104 } s;
105 bool scalar_is_char;
106 struct
107 {
108 type_p p;
109 const char *len;
110 } a;
111 struct
112 {
113 type_p stru;
114 type_p param[NUM_PARAM];
115 struct fileloc line;
116 } param_struct;
117 } u;
118 };
119
120 #define UNION_P(x) \
121 ((x)->kind == TYPE_UNION || \
122 ((x)->kind == TYPE_LANG_STRUCT \
123 && (x)->u.s.lang_struct->kind == TYPE_UNION))
124 #define UNION_OR_STRUCT_P(x) \
125 ((x)->kind == TYPE_UNION \
126 || (x)->kind == TYPE_STRUCT \
127 || (x)->kind == TYPE_LANG_STRUCT)
128
129
130
131 const char *get_output_file_name (const char *);
132
133
134 /* The list of output files. */
135 outf_p output_files;
136
137 /* The output header file that is included into pretty much every
138 source file. */
139 outf_p header_file;
140
141
142 /* The name of the file containing the list of input files. */
143 static char *inputlist;
144
145 /* The plugin input files and their number; in that case only
146 a single file is produced. */
147 static char **plugin_files;
148 static size_t nb_plugin_files;
149
150 /* The generated plugin output file and name. */
151 static outf_p plugin_output;
152 static char *plugin_output_filename;
153
154 /* Our source directory and its length. */
155 const char *srcdir;
156 size_t srcdir_len;
157
158 /* Variables used for reading and writing the state. */
159 const char *read_state_filename;
160 const char *write_state_filename;
161
162 /* Variables to help debugging. */
163 int do_dump;
164 int do_debug;
165
166 static outf_p create_file (const char *, const char *);
167
168 static const char *get_file_basename (const char *);
169 static const char *get_file_realbasename (const char *);
170 static const char *get_file_srcdir_relative_path (const char *);
171
172 static int get_prefix_langdir_index (const char *);
173 static const char *get_file_langdir (const char *);
174 \f
175
176 /* Nonzero iff an error has occurred. */
177 bool hit_error = false;
178
179 static void gen_rtx_next (void);
180 static void write_rtx_next (void);
181 static void open_base_files (void);
182 static void close_output_files (void);
183
184 /* Report an error at POS, printing MSG. */
185
186 void
187 error_at_line (const struct fileloc *pos, const char *msg, ...)
188 {
189 va_list ap;
190
191 va_start (ap, msg);
192
193 fprintf (stderr, "%s:%d: ", pos->file, pos->line);
194 vfprintf (stderr, msg, ap);
195 fputc ('\n', stderr);
196 hit_error = true;
197
198 va_end (ap);
199 }
200
201 /* asprintf, but produces fatal message on out-of-memory. */
202 char *
203 xasprintf (const char *format, ...)
204 {
205 int n;
206 char *result;
207 va_list ap;
208
209 va_start (ap, format);
210 n = vasprintf (&result, format, ap);
211 if (result == NULL || n < 0)
212 fatal ("out of memory");
213 va_end (ap);
214
215 return result;
216 }
217 \f
218 /* Input file handling. */
219
220 /* Table of all input files. */
221 static const char **gt_files;
222 static size_t num_gt_files;
223
224 /* A number of places use the name of this "gengtype.h" file for a
225 location for things that we can't rely on the source to define.
226 Make sure we can still use pointer comparison on filenames. */
227 const char this_file[] = __FILE__;
228 /* The "system.h" file is likewise specially useful. */
229 const char system_h_file[] = "system.h";
230
231 /* Vector of per-language directories. */
232 static const char **lang_dir_names;
233 static size_t num_lang_dirs;
234
235 /* An array of output files suitable for definitions. There is one
236 BASE_FILES entry for each language. */
237 static outf_p *base_files;
238
239 /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
240 INPUT_FILE is used by <lang>.
241
242 This function should be written to assume that a file _is_ used
243 if the situation is unclear. If it wrongly assumes a file _is_ used,
244 a linker error will result. If it wrongly assumes a file _is not_ used,
245 some GC roots may be missed, which is a much harder-to-debug problem.
246
247 The relevant bitmap is stored immediately before the file's name in the
248 buffer set up by read_input_list. It may be unaligned, so we have to
249 read it byte-by-byte. */
250
251 static lang_bitmap
252 get_lang_bitmap (const char *gtfile)
253 {
254
255 if (gtfile == this_file || gtfile == system_h_file)
256 {
257 /* Things defined in this "gengtype.c" file or in "system.h" are
258 universal (and there is no space for their lang_bitmap before
259 their file names). */
260 return (((lang_bitmap) 1) << num_lang_dirs) - 1;
261 }
262 else
263 {
264 lang_bitmap n = 0;
265 int i;
266 for (i = -(int) sizeof (lang_bitmap); i < 0; i++)
267 n = (n << CHAR_BIT) + (unsigned char) gtfile[i];
268 return n;
269 }
270 }
271
272 /* Set the bitmap returned by get_lang_bitmap. The only legitimate
273 caller of this function is read_input_list. */
274 static void
275 set_lang_bitmap (char *gtfile, lang_bitmap n)
276 {
277 int i;
278 for (i = -1; i >= -(int) sizeof (lang_bitmap); i--)
279 {
280 gtfile[i] = n & ((1U << CHAR_BIT) - 1);
281 n >>= CHAR_BIT;
282 }
283 }
284
285
286 #if ENABLE_CHECKING
287 /* Utility debugging function, printing the various type counts within
288 a list of types. Called thru the DBGPRINT_COUNT_TYPE macro. */
289 void
290 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
291 {
292 int nb_types = 0, nb_scalar = 0, nb_string = 0;
293 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
294 int nb_lang_struct = 0, nb_param_struct = 0;
295 type_p p = NULL;
296 for (p = t; p; p = p->next)
297 {
298 nb_types++;
299 switch (p->kind)
300 {
301 case TYPE_SCALAR:
302 nb_scalar++;
303 break;
304 case TYPE_STRING:
305 nb_string++;
306 break;
307 case TYPE_STRUCT:
308 nb_struct++;
309 break;
310 case TYPE_UNION:
311 nb_union++;
312 break;
313 case TYPE_POINTER:
314 nb_pointer++;
315 break;
316 case TYPE_ARRAY:
317 nb_array++;
318 break;
319 case TYPE_LANG_STRUCT:
320 nb_lang_struct++;
321 break;
322 case TYPE_PARAM_STRUCT:
323 nb_param_struct++;
324 break;
325 default:
326 gcc_unreachable ();
327 }
328 }
329 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
330 lbasename (fil), lin, msg, nb_types);
331 if (nb_scalar > 0 || nb_string > 0)
332 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
333 if (nb_struct > 0 || nb_union > 0)
334 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
335 if (nb_pointer > 0 || nb_array > 0)
336 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
337 if (nb_lang_struct > 0 || nb_param_struct > 0)
338 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
339 nb_lang_struct, nb_param_struct);
340 fprintf (stderr, "\n");
341 }
342 #endif /* ENABLE_CHECKING */
343
344 /* Scan the input file, LIST, and determine how much space we need to
345 store strings in. Also, count the number of language directories
346 and files. The numbers returned are overestimates as they does not
347 consider repeated files. */
348 static size_t
349 measure_input_list (FILE *list)
350 {
351 size_t n = 0;
352 int c;
353 bool atbol = true;
354 num_lang_dirs = 0;
355 num_gt_files = plugin_files ? nb_plugin_files : 0;
356 while ((c = getc (list)) != EOF)
357 {
358 n++;
359 if (atbol)
360 {
361 if (c == '[')
362 num_lang_dirs++;
363 else
364 {
365 /* Add space for a lang_bitmap before the input file name. */
366 n += sizeof (lang_bitmap);
367 num_gt_files++;
368 }
369 atbol = false;
370 }
371
372 if (c == '\n')
373 atbol = true;
374 }
375
376 rewind (list);
377 return n;
378 }
379
380 /* Read one input line from LIST to HEREP (which is updated). A
381 pointer to the string is returned via LINEP. If it was a language
382 subdirectory in square brackets, strip off the square brackets and
383 return true. Otherwise, leave space before the string for a
384 lang_bitmap, and return false. At EOF, returns false, does not
385 touch *HEREP, and sets *LINEP to NULL. POS is used for
386 diagnostics. */
387 static bool
388 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
389 {
390 char *here = *herep;
391 char *line;
392 int c = getc (list);
393
394 /* Read over whitespace. */
395 while (c == '\n' || c == ' ')
396 c = getc (list);
397
398 if (c == EOF)
399 {
400 *linep = 0;
401 return false;
402 }
403 else if (c == '[')
404 {
405 /* No space for a lang_bitmap is necessary. Discard the '['. */
406 c = getc (list);
407 line = here;
408 while (c != ']' && c != '\n' && c != EOF)
409 {
410 *here++ = c;
411 c = getc (list);
412 }
413 *here++ = '\0';
414
415 if (c == ']')
416 {
417 c = getc (list); /* eat what should be a newline */
418 if (c != '\n' && c != EOF)
419 error_at_line (pos, "junk on line after language tag [%s]", line);
420 }
421 else
422 error_at_line (pos, "missing close bracket for language tag [%s",
423 line);
424
425 *herep = here;
426 *linep = line;
427 return true;
428 }
429 else
430 {
431 /* Leave space for a lang_bitmap. */
432 memset (here, 0, sizeof (lang_bitmap));
433 here += sizeof (lang_bitmap);
434 line = here;
435 do
436 {
437 *here++ = c;
438 c = getc (list);
439 }
440 while (c != EOF && c != '\n');
441 *here++ = '\0';
442 *herep = here;
443 *linep = line;
444 return false;
445 }
446 }
447
448 /* Read the list of input files from LIST and compute all of the
449 relevant tables. There is one file per line of the list. At
450 first, all the files on the list are language-generic, but
451 eventually a line will appear which is the name of a language
452 subdirectory in square brackets, like this: [cp]. All subsequent
453 files are specific to that language, until another language
454 subdirectory tag appears. Files can appear more than once, if
455 they apply to more than one language. */
456 static void
457 read_input_list (const char *listname)
458 {
459 FILE *list = fopen (listname, "r");
460 if (!list)
461 fatal ("cannot open %s: %s", listname, xstrerror (errno));
462 else
463 {
464 struct fileloc epos;
465 size_t bufsz = measure_input_list (list);
466 char *buf = XNEWVEC (char, bufsz);
467 char *here = buf;
468 char *committed = buf;
469 char *limit = buf + bufsz;
470 char *line;
471 bool is_language;
472 size_t langno = 0;
473 size_t nfiles = 0;
474 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
475
476 epos.file = listname;
477 epos.line = 0;
478
479 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
480 gt_files = XNEWVEC (const char *, num_gt_files);
481
482 for (;;)
483 {
484 next_line:
485 epos.line++;
486 committed = here;
487 is_language = read_input_line (list, &here, &line, &epos);
488 gcc_assert (here <= limit);
489 if (line == 0)
490 break;
491 else if (is_language)
492 {
493 size_t i;
494 gcc_assert (langno <= num_lang_dirs);
495 for (i = 0; i < langno; i++)
496 if (strcmp (lang_dir_names[i], line) == 0)
497 {
498 error_at_line (&epos, "duplicate language tag [%s]",
499 line);
500 curlangs = 1 << i;
501 here = committed;
502 goto next_line;
503 }
504
505 curlangs = 1 << langno;
506 lang_dir_names[langno++] = line;
507 }
508 else
509 {
510 size_t i;
511 gcc_assert (nfiles <= num_gt_files);
512 for (i = 0; i < nfiles; i++)
513 if (strcmp (gt_files[i], line) == 0)
514 {
515 /* Throw away the string we just read, and add the
516 current language to the existing string's bitmap. */
517 lang_bitmap bmap = get_lang_bitmap (gt_files[i]);
518 if (bmap & curlangs)
519 error_at_line (&epos,
520 "file %s specified more than once "
521 "for language %s", line,
522 langno ==
523 0 ? "(all)" : lang_dir_names[langno -
524 1]);
525
526 bmap |= curlangs;
527 set_lang_bitmap (CONST_CAST (char *, gt_files[i]), bmap);
528 here = committed;
529 goto next_line;
530 }
531
532 set_lang_bitmap (line, curlangs);
533 gt_files[nfiles++] = line;
534 }
535 }
536 /* Update the global counts now that we know accurately how many
537 things there are. (We do not bother resizing the arrays down.) */
538 num_lang_dirs = langno;
539 /* Add the plugin files if provided. */
540 if (plugin_files)
541 {
542 size_t i;
543 for (i = 0; i < nb_plugin_files; i++)
544 gt_files[nfiles++] = plugin_files[i];
545 }
546 num_gt_files = nfiles;
547 }
548
549 /* Sanity check: any file that resides in a language subdirectory
550 (e.g. 'cp') ought to belong to the corresponding language.
551 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
552 (Can you even do that? Should you be allowed to?) */
553 {
554 size_t f;
555 for (f = 0; f < num_gt_files; f++)
556 {
557 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
558 const char *basename = get_file_basename (gt_files[f]);
559 const char *slashpos = strchr (basename, '/');
560
561 if (slashpos)
562 {
563 size_t l;
564 for (l = 0; l < num_lang_dirs; l++)
565 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
566 && memcmp (basename, lang_dir_names[l],
567 strlen (lang_dir_names[l])) == 0)
568 {
569 if (!(bitmap & (1 << l)))
570 error ("%s is in language directory '%s' but is not "
571 "tagged for that language",
572 basename, lang_dir_names[l]);
573 break;
574 }
575 }
576 }
577 }
578
579 if (ferror (list))
580 fatal ("error reading %s: %s", listname, xstrerror (errno));
581
582 fclose (list);
583 }
584 \f
585
586
587 /* The one and only TYPE_STRING. */
588
589 static struct type string_type = {
590 TYPE_STRING, 0, 0, GC_USED, {0}
591 };
592
593 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
594 set to appropriate values at the beginning of main. */
595
596 static struct type scalar_nonchar = {
597 TYPE_SCALAR, 0, 0, GC_USED, {0}
598 };
599
600 static struct type scalar_char = {
601 TYPE_SCALAR, 0, 0, GC_USED, {0}
602 };
603
604 /* Lists of various things. */
605
606 static pair_p typedefs;
607 static type_p structures;
608 static type_p param_structs;
609 static pair_p variables;
610
611 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
612 static type_p adjust_field_tree_exp (type_p t, options_p opt);
613 static type_p adjust_field_rtx_def (type_p t, options_p opt);
614
615 /* Define S as a typedef to T at POS. */
616
617 void
618 do_typedef (const char *s, type_p t, struct fileloc *pos)
619 {
620 pair_p p;
621
622 /* temporary kludge - gengtype doesn't handle conditionals or
623 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
624 is coming from this file (main() sets them up with safe dummy
625 definitions). */
626 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
627 return;
628
629 for (p = typedefs; p != NULL; p = p->next)
630 if (strcmp (p->name, s) == 0)
631 {
632 if (p->type != t)
633 {
634 error_at_line (pos, "type `%s' previously defined", s);
635 error_at_line (&p->line, "previously defined here");
636 }
637 return;
638 }
639
640 p = XNEW (struct pair);
641 p->next = typedefs;
642 p->name = s;
643 p->type = t;
644 p->line = *pos;
645 p->opt = NULL;
646 typedefs = p;
647 }
648
649 /* Define S as a typename of a scalar. Cannot be used to define
650 typedefs of 'char'. Note: is also used for pointer-to-function
651 typedefs (which are therefore not treated as pointers). */
652
653 void
654 do_scalar_typedef (const char *s, struct fileloc *pos)
655 {
656 do_typedef (s, &scalar_nonchar, pos);
657 }
658
659 /* Return the type previously defined for S. Use POS to report errors. */
660
661 type_p
662 resolve_typedef (const char *s, struct fileloc *pos)
663 {
664 pair_p p;
665 for (p = typedefs; p != NULL; p = p->next)
666 if (strcmp (p->name, s) == 0)
667 return p->type;
668 error_at_line (pos, "unidentified type `%s'", s);
669 return &scalar_nonchar; /* treat as "int" */
670 }
671
672 /* Create and return a new structure with tag NAME (or a union iff
673 ISUNION is nonzero), at POS with fields FIELDS and options O. */
674
675 type_p
676 new_structure (const char *name, int isunion, struct fileloc *pos,
677 pair_p fields, options_p o)
678 {
679 type_p si;
680 type_p s = NULL;
681 lang_bitmap bitmap = get_lang_bitmap (pos->file);
682
683 /* temporary kludge - gengtype doesn't handle conditionals or
684 macros. Ignore any attempt to define struct location_s, unless
685 it is coming from this file (main() sets it up safely). */
686 if (!strcmp (name, "location_s") && !isunion && pos->file != this_file)
687 return find_structure (name, 0);
688
689 for (si = structures; si != NULL; si = si->next)
690 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
691 {
692 type_p ls = NULL;
693 if (si->kind == TYPE_LANG_STRUCT)
694 {
695 ls = si;
696
697 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
698 if (si->u.s.bitmap == bitmap)
699 s = si;
700 }
701 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
702 {
703 ls = si;
704 si = XCNEW (struct type);
705 memcpy (si, ls, sizeof (struct type));
706 ls->kind = TYPE_LANG_STRUCT;
707 ls->u.s.lang_struct = si;
708 ls->u.s.fields = NULL;
709 si->next = NULL;
710 si->pointer_to = NULL;
711 si->u.s.lang_struct = ls;
712 }
713 else
714 s = si;
715
716 if (ls != NULL && s == NULL)
717 {
718 s = XCNEW (struct type);
719 s->next = ls->u.s.lang_struct;
720 ls->u.s.lang_struct = s;
721 s->u.s.lang_struct = ls;
722 }
723 break;
724 }
725
726 if (s == NULL)
727 {
728 s = XCNEW (struct type);
729 s->next = structures;
730 structures = s;
731 }
732
733 if (s->u.s.line.file != NULL
734 || (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap)))
735 {
736 error_at_line (pos, "duplicate definition of '%s %s'",
737 isunion ? "union" : "struct", s->u.s.tag);
738 error_at_line (&s->u.s.line, "previous definition here");
739 }
740
741 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
742 s->u.s.tag = name;
743 s->u.s.line = *pos;
744 s->u.s.fields = fields;
745 s->u.s.opt = o;
746 s->u.s.bitmap = bitmap;
747 if (s->u.s.lang_struct)
748 s->u.s.lang_struct->u.s.bitmap |= bitmap;
749
750 /* Reset location_s's location to input.h so that we know where to
751 write out its mark routine. */
752 if (!strcmp (name, "location_s") && !isunion && pos->file == this_file)
753 {
754 size_t n;
755 for (n = 0; n < num_gt_files; n++)
756 if (!strcmp (gt_files[n] + strlen (gt_files[n]) - strlen ("input.h"),
757 "input.h"))
758 {
759 s->u.s.line.file = gt_files[n];
760 break;
761 }
762 }
763
764 return s;
765 }
766
767 /* Return the previously-defined structure with tag NAME (or a union
768 iff ISUNION is nonzero), or a new empty structure or union if none
769 was defined previously. */
770
771 type_p
772 find_structure (const char *name, int isunion)
773 {
774 type_p s;
775
776 for (s = structures; s != NULL; s = s->next)
777 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
778 return s;
779
780 s = XCNEW (struct type);
781 s->next = structures;
782 structures = s;
783 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
784 s->u.s.tag = name;
785 structures = s;
786 return s;
787 }
788
789 /* Return the previously-defined parameterized structure for structure
790 T and parameters PARAM, or a new parameterized empty structure or
791 union if none was defined previously. */
792
793 static type_p
794 find_param_structure (type_p t, type_p param[NUM_PARAM])
795 {
796 type_p res;
797
798 for (res = param_structs; res; res = res->next)
799 if (res->u.param_struct.stru == t
800 && memcmp (res->u.param_struct.param, param,
801 sizeof (type_p) * NUM_PARAM) == 0)
802 break;
803 if (res == NULL)
804 {
805 res = XCNEW (struct type);
806 res->kind = TYPE_PARAM_STRUCT;
807 res->next = param_structs;
808 param_structs = res;
809 res->u.param_struct.stru = t;
810 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
811 }
812 return res;
813 }
814
815 /* Return a scalar type with name NAME. */
816
817 type_p
818 create_scalar_type (const char *name)
819 {
820 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
821 return &scalar_char;
822 else
823 return &scalar_nonchar;
824 }
825
826 /* Return a pointer to T. */
827
828 type_p
829 create_pointer (type_p t)
830 {
831 if (!t->pointer_to)
832 {
833 type_p r = XCNEW (struct type);
834 r->kind = TYPE_POINTER;
835 r->u.p = t;
836 t->pointer_to = r;
837 }
838 return t->pointer_to;
839 }
840
841 /* Return an array of length LEN. */
842
843 type_p
844 create_array (type_p t, const char *len)
845 {
846 type_p v;
847
848 v = XCNEW (struct type);
849 v->kind = TYPE_ARRAY;
850 v->u.a.p = t;
851 v->u.a.len = len;
852 return v;
853 }
854
855 /* Return an options structure with name NAME and info INFO. NEXT is the
856 next option in the chain. */
857
858 options_p
859 create_option (options_p next, const char *name, const void *info)
860 {
861 options_p o = XNEW (struct options);
862 o->next = next;
863 o->name = name;
864 o->info = (const char *) info;
865 return o;
866 }
867
868 /* Return an options structure for a "nested_ptr" option. */
869 options_p
870 create_nested_ptr_option (options_p next, type_p t,
871 const char *to, const char *from)
872 {
873 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
874
875 d->type = adjust_field_type (t, 0);
876 d->convert_to = to;
877 d->convert_from = from;
878 return create_option (next, "nested_ptr", d);
879 }
880
881 /* Add a variable named S of type T with options O defined at POS,
882 to `variables'. */
883
884 void
885 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
886 {
887 pair_p n;
888 n = XNEW (struct pair);
889 n->name = s;
890 n->type = t;
891 n->line = *pos;
892 n->opt = o;
893 n->next = variables;
894 variables = n;
895 }
896
897 /* Most-general structure field creator. */
898 static pair_p
899 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
900 const char *file, int line)
901 {
902 pair_p field;
903
904 field = XNEW (struct pair);
905 field->next = next;
906 field->type = type;
907 field->name = name;
908 field->opt = opt;
909 field->line.file = file;
910 field->line.line = line;
911 return field;
912 }
913
914 /* Create a field that came from the source code we are scanning,
915 i.e. we have a 'struct fileloc', and possibly options; also,
916 adjust_field_type should be called. */
917 pair_p
918 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
919 struct fileloc *pos)
920 {
921 return create_field_all (next, adjust_field_type (type, opt),
922 name, opt, pos->file, pos->line);
923 }
924
925 /* Create a fake field with the given type and name. NEXT is the next
926 field in the chain. */
927 #define create_field(next,type,name) \
928 create_field_all(next,type,name, 0, this_file, __LINE__)
929
930 /* Like create_field, but the field is only valid when condition COND
931 is true. */
932
933 static pair_p
934 create_optional_field_ (pair_p next, type_p type, const char *name,
935 const char *cond, int line)
936 {
937 static int id = 1;
938 pair_p union_fields;
939 type_p union_type;
940
941 /* Create a fake union type with a single nameless field of type TYPE.
942 The field has a tag of "1". This allows us to make the presence
943 of a field of type TYPE depend on some boolean "desc" being true. */
944 union_fields = create_field (NULL, type, "");
945 union_fields->opt = create_option (union_fields->opt, "dot", "");
946 union_fields->opt = create_option (union_fields->opt, "tag", "1");
947 union_type = new_structure (xasprintf ("%s_%d", "fake_union", id++), 1,
948 &lexer_line, union_fields, NULL);
949
950 /* Create the field and give it the new fake union type. Add a "desc"
951 tag that specifies the condition under which the field is valid. */
952 return create_field_all (next, union_type, name,
953 create_option (0, "desc", cond), this_file, line);
954 }
955
956 #define create_optional_field(next,type,name,cond) \
957 create_optional_field_(next,type,name,cond,__LINE__)
958
959 /* Reverse a linked list of 'struct pair's in place. */
960 pair_p
961 nreverse_pairs (pair_p list)
962 {
963 pair_p prev = 0, p, next;
964 for (p = list; p; p = next)
965 {
966 next = p->next;
967 p->next = prev;
968 prev = p;
969 }
970 return prev;
971 }
972 \f
973
974 /* We don't care how long a CONST_DOUBLE is. */
975 #define CONST_DOUBLE_FORMAT "ww"
976 /* We don't want to see codes that are only for generator files. */
977 #undef GENERATOR_FILE
978
979 enum rtx_code
980 {
981 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
982 #include "rtl.def"
983 #undef DEF_RTL_EXPR
984 NUM_RTX_CODE
985 };
986
987 static const char *const rtx_name[NUM_RTX_CODE] = {
988 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
989 #include "rtl.def"
990 #undef DEF_RTL_EXPR
991 };
992
993 static const char *const rtx_format[NUM_RTX_CODE] = {
994 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
995 #include "rtl.def"
996 #undef DEF_RTL_EXPR
997 };
998
999 static int rtx_next_new[NUM_RTX_CODE];
1000
1001 /* We also need codes and names for insn notes (not register notes).
1002 Note that we do *not* bias the note values here. */
1003 enum insn_note
1004 {
1005 #define DEF_INSN_NOTE(NAME) NAME,
1006 #include "insn-notes.def"
1007 #undef DEF_INSN_NOTE
1008
1009 NOTE_INSN_MAX
1010 };
1011
1012 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1013 default field for line number notes. */
1014 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1015 #define DEF_INSN_NOTE(NAME) #NAME,
1016 #include "insn-notes.def"
1017 #undef DEF_INSN_NOTE
1018 };
1019
1020 #undef CONST_DOUBLE_FORMAT
1021 #define GENERATOR_FILE
1022
1023 /* Generate the contents of the rtx_next array. This really doesn't belong
1024 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1025
1026 static void
1027 gen_rtx_next (void)
1028 {
1029 int i;
1030 for (i = 0; i < NUM_RTX_CODE; i++)
1031 {
1032 int k;
1033
1034 rtx_next_new[i] = -1;
1035 if (strncmp (rtx_format[i], "iuu", 3) == 0)
1036 rtx_next_new[i] = 2;
1037 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1038 rtx_next_new[i] = 1;
1039 else
1040 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1041 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1042 rtx_next_new[i] = k;
1043 }
1044 }
1045
1046 /* Write out the contents of the rtx_next array. */
1047 static void
1048 write_rtx_next (void)
1049 {
1050 outf_p f = get_output_file_with_visibility (NULL);
1051 int i;
1052 if (!f)
1053 return;
1054
1055 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1056 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1057 for (i = 0; i < NUM_RTX_CODE; i++)
1058 if (rtx_next_new[i] == -1)
1059 oprintf (f, " 0,\n");
1060 else
1061 oprintf (f,
1062 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1063 oprintf (f, "};\n");
1064 }
1065
1066 /* Handle `special("rtx_def")'. This is a special case for field
1067 `fld' of struct rtx_def, which is an array of unions whose values
1068 are based in a complex way on the type of RTL. */
1069
1070 static type_p
1071 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1072 {
1073 pair_p flds = NULL;
1074 options_p nodot;
1075 int i;
1076 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1077 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1078
1079 if (t->kind != TYPE_UNION)
1080 {
1081 error_at_line (&lexer_line,
1082 "special `rtx_def' must be applied to a union");
1083 return &string_type;
1084 }
1085
1086 nodot = create_option (NULL, "dot", "");
1087
1088 rtx_tp = create_pointer (find_structure ("rtx_def", 0));
1089 rtvec_tp = create_pointer (find_structure ("rtvec_def", 0));
1090 tree_tp = create_pointer (find_structure ("tree_node", 1));
1091 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", 0));
1092 reg_attrs_tp = create_pointer (find_structure ("reg_attrs", 0));
1093 basic_block_tp = create_pointer (find_structure ("basic_block_def", 0));
1094 constant_tp =
1095 create_pointer (find_structure ("constant_descriptor_rtx", 0));
1096 scalar_tp = &scalar_nonchar; /* rtunion int */
1097
1098 {
1099 pair_p note_flds = NULL;
1100 int c;
1101
1102 for (c = 0; c <= NOTE_INSN_MAX; c++)
1103 {
1104 switch (c)
1105 {
1106 case NOTE_INSN_MAX:
1107 case NOTE_INSN_DELETED_LABEL:
1108 note_flds = create_field (note_flds, &string_type, "rt_str");
1109 break;
1110
1111 case NOTE_INSN_BLOCK_BEG:
1112 case NOTE_INSN_BLOCK_END:
1113 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1114 break;
1115
1116 case NOTE_INSN_VAR_LOCATION:
1117 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1118 break;
1119
1120 default:
1121 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1122 break;
1123 }
1124 /* NOTE_INSN_MAX is used as the default field for line
1125 number notes. */
1126 if (c == NOTE_INSN_MAX)
1127 note_flds->opt = create_option (nodot, "default", "");
1128 else
1129 note_flds->opt = create_option (nodot, "tag", note_insn_name[c]);
1130 }
1131 note_union_tp = new_structure ("rtx_def_note_subunion", 1,
1132 &lexer_line, note_flds, NULL);
1133 }
1134 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1135 {
1136 pair_p sym_flds;
1137
1138 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1139 sym_flds->opt = create_option (nodot, "default", "");
1140
1141 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1142 sym_flds->opt = create_option (nodot, "tag", "1");
1143
1144 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", 1,
1145 &lexer_line, sym_flds, NULL);
1146 }
1147 for (i = 0; i < NUM_RTX_CODE; i++)
1148 {
1149 pair_p subfields = NULL;
1150 size_t aindex, nmindex;
1151 const char *sname;
1152 type_p substruct;
1153 char *ftag;
1154
1155 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1156 {
1157 type_p t;
1158 const char *subname;
1159
1160 switch (rtx_format[i][aindex])
1161 {
1162 case '*':
1163 case 'i':
1164 case 'n':
1165 case 'w':
1166 t = scalar_tp;
1167 subname = "rt_int";
1168 break;
1169
1170 case '0':
1171 if (i == MEM && aindex == 1)
1172 t = mem_attrs_tp, subname = "rt_mem";
1173 else if (i == JUMP_INSN && aindex == 8)
1174 t = rtx_tp, subname = "rt_rtx";
1175 else if (i == CODE_LABEL && aindex == 5)
1176 t = scalar_tp, subname = "rt_int";
1177 else if (i == CODE_LABEL && aindex == 4)
1178 t = rtx_tp, subname = "rt_rtx";
1179 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1180 t = rtx_tp, subname = "rt_rtx";
1181 else if (i == NOTE && aindex == 4)
1182 t = note_union_tp, subname = "";
1183 else if (i == NOTE && aindex == 5)
1184 t = scalar_tp, subname = "rt_int";
1185 else if (i == NOTE && aindex >= 7)
1186 t = scalar_tp, subname = "rt_int";
1187 else if (i == ADDR_DIFF_VEC && aindex == 4)
1188 t = scalar_tp, subname = "rt_int";
1189 else if (i == VALUE && aindex == 0)
1190 t = scalar_tp, subname = "rt_int";
1191 else if (i == DEBUG_EXPR && aindex == 0)
1192 t = tree_tp, subname = "rt_tree";
1193 else if (i == REG && aindex == 1)
1194 t = scalar_tp, subname = "rt_int";
1195 else if (i == REG && aindex == 2)
1196 t = reg_attrs_tp, subname = "rt_reg";
1197 else if (i == SCRATCH && aindex == 0)
1198 t = scalar_tp, subname = "rt_int";
1199 else if (i == SYMBOL_REF && aindex == 1)
1200 t = scalar_tp, subname = "rt_int";
1201 else if (i == SYMBOL_REF && aindex == 2)
1202 t = symbol_union_tp, subname = "";
1203 else if (i == BARRIER && aindex >= 3)
1204 t = scalar_tp, subname = "rt_int";
1205 else
1206 {
1207 error_at_line (&lexer_line,
1208 "rtx type `%s' has `0' in position %lu, can't handle",
1209 rtx_name[i], (unsigned long) aindex);
1210 t = &string_type;
1211 subname = "rt_int";
1212 }
1213 break;
1214
1215 case 's':
1216 case 'S':
1217 case 'T':
1218 t = &string_type;
1219 subname = "rt_str";
1220 break;
1221
1222 case 'e':
1223 case 'u':
1224 t = rtx_tp;
1225 subname = "rt_rtx";
1226 break;
1227
1228 case 'E':
1229 case 'V':
1230 t = rtvec_tp;
1231 subname = "rt_rtvec";
1232 break;
1233
1234 case 't':
1235 t = tree_tp;
1236 subname = "rt_tree";
1237 break;
1238
1239 case 'B':
1240 t = basic_block_tp;
1241 subname = "rt_bb";
1242 break;
1243
1244 default:
1245 error_at_line (&lexer_line,
1246 "rtx type `%s' has `%c' in position %lu, can't handle",
1247 rtx_name[i], rtx_format[i][aindex],
1248 (unsigned long) aindex);
1249 t = &string_type;
1250 subname = "rt_int";
1251 break;
1252 }
1253
1254 subfields = create_field (subfields, t,
1255 xasprintf (".fld[%lu].%s",
1256 (unsigned long) aindex,
1257 subname));
1258 subfields->opt = nodot;
1259 if (t == note_union_tp)
1260 subfields->opt = create_option (subfields->opt, "desc",
1261 "NOTE_KIND (&%0)");
1262 if (t == symbol_union_tp)
1263 subfields->opt = create_option (subfields->opt, "desc",
1264 "CONSTANT_POOL_ADDRESS_P (&%0)");
1265 }
1266
1267 if (i == SYMBOL_REF)
1268 {
1269 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P holds. */
1270 type_p field_tp = find_structure ("block_symbol", 0);
1271 subfields
1272 = create_optional_field (subfields, field_tp, "block_sym",
1273 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1274 }
1275
1276 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1277 substruct = new_structure (sname, 0, &lexer_line, subfields, NULL);
1278
1279 ftag = xstrdup (rtx_name[i]);
1280 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1281 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1282
1283 flds = create_field (flds, substruct, "");
1284 flds->opt = create_option (nodot, "tag", ftag);
1285 }
1286
1287 return new_structure ("rtx_def_subunion", 1, &lexer_line, flds, nodot);
1288 }
1289
1290 /* Handle `special("tree_exp")'. This is a special case for
1291 field `operands' of struct tree_exp, which although it claims to contain
1292 pointers to trees, actually sometimes contains pointers to RTL too.
1293 Passed T, the old type of the field, and OPT its options. Returns
1294 a new type for the field. */
1295
1296 static type_p
1297 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1298 {
1299 pair_p flds;
1300 options_p nodot;
1301
1302 if (t->kind != TYPE_ARRAY)
1303 {
1304 error_at_line (&lexer_line,
1305 "special `tree_exp' must be applied to an array");
1306 return &string_type;
1307 }
1308
1309 nodot = create_option (NULL, "dot", "");
1310
1311 flds = create_field (NULL, t, "");
1312 flds->opt = create_option (nodot, "length",
1313 "TREE_OPERAND_LENGTH ((tree) &%0)");
1314 flds->opt = create_option (flds->opt, "default", "");
1315
1316 return new_structure ("tree_exp_subunion", 1, &lexer_line, flds, nodot);
1317 }
1318
1319 /* Perform any special processing on a type T, about to become the type
1320 of a field. Return the appropriate type for the field.
1321 At present:
1322 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1323 - Similarly for arrays of pointer-to-char;
1324 - Converts structures for which a parameter is provided to
1325 TYPE_PARAM_STRUCT;
1326 - Handles "special" options.
1327 */
1328
1329 type_p
1330 adjust_field_type (type_p t, options_p opt)
1331 {
1332 int length_p = 0;
1333 const int pointer_p = t->kind == TYPE_POINTER;
1334 type_p params[NUM_PARAM];
1335 int params_p = 0;
1336 int i;
1337
1338 for (i = 0; i < NUM_PARAM; i++)
1339 params[i] = NULL;
1340
1341 for (; opt; opt = opt->next)
1342 if (strcmp (opt->name, "length") == 0)
1343 length_p = 1;
1344 else if (strcmp (opt->name, "param_is") == 0
1345 || (strncmp (opt->name, "param", 5) == 0
1346 && ISDIGIT (opt->name[5])
1347 && strcmp (opt->name + 6, "_is") == 0))
1348 {
1349 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1350
1351 if (!UNION_OR_STRUCT_P (t)
1352 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
1353 {
1354 error_at_line (&lexer_line,
1355 "option `%s' may only be applied to structures or structure pointers",
1356 opt->name);
1357 return t;
1358 }
1359
1360 params_p = 1;
1361 if (params[num] != NULL)
1362 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1363 if (!ISDIGIT (opt->name[5]))
1364 params[num] =
1365 create_pointer (CONST_CAST2 (type_p, const char *, opt->info));
1366 else
1367 params[num] = CONST_CAST2 (type_p, const char *, opt->info);
1368 }
1369 else if (strcmp (opt->name, "special") == 0)
1370 {
1371 const char *special_name = opt->info;
1372 if (strcmp (special_name, "tree_exp") == 0)
1373 t = adjust_field_tree_exp (t, opt);
1374 else if (strcmp (special_name, "rtx_def") == 0)
1375 t = adjust_field_rtx_def (t, opt);
1376 else
1377 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1378 }
1379
1380 if (params_p)
1381 {
1382 type_p realt;
1383
1384 if (pointer_p)
1385 t = t->u.p;
1386 realt = find_param_structure (t, params);
1387 t = pointer_p ? create_pointer (realt) : realt;
1388 }
1389
1390 if (!length_p
1391 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1392 return &string_type;
1393 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1394 && t->u.a.p->u.p->kind == TYPE_SCALAR
1395 && t->u.a.p->u.p->u.scalar_is_char)
1396 return create_array (&string_type, t->u.a.len);
1397
1398 return t;
1399 }
1400 \f
1401
1402 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *);
1403 static void set_gc_used (pair_p);
1404
1405 /* Handle OPT for set_gc_used_type. */
1406
1407 static void
1408 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1409 int *pass_param, int *length, int *skip,
1410 type_p *nested_ptr)
1411 {
1412 options_p o;
1413 for (o = opt; o; o = o->next)
1414 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO)
1415 set_gc_used_type (CONST_CAST2 (type_p, const char *, o->info),
1416 GC_POINTED_TO, NULL);
1417 else if (strcmp (o->name, "maybe_undef") == 0)
1418 *maybe_undef = 1;
1419 else if (strcmp (o->name, "use_params") == 0)
1420 *pass_param = 1;
1421 else if (strcmp (o->name, "length") == 0)
1422 *length = 1;
1423 else if (strcmp (o->name, "skip") == 0)
1424 *skip = 1;
1425 else if (strcmp (o->name, "nested_ptr") == 0)
1426 *nested_ptr = ((const struct nested_ptr_data *) o->info)->type;
1427 }
1428
1429 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1430
1431 static void
1432 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM])
1433 {
1434 if (t->gc_used >= level)
1435 return;
1436
1437 t->gc_used = level;
1438
1439 switch (t->kind)
1440 {
1441 case TYPE_STRUCT:
1442 case TYPE_UNION:
1443 {
1444 pair_p f;
1445 int dummy;
1446 type_p dummy2;
1447
1448 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1449 &dummy2);
1450
1451 for (f = t->u.s.fields; f; f = f->next)
1452 {
1453 int maybe_undef = 0;
1454 int pass_param = 0;
1455 int length = 0;
1456 int skip = 0;
1457 type_p nested_ptr = NULL;
1458 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1459 &length, &skip, &nested_ptr);
1460
1461 if (nested_ptr && f->type->kind == TYPE_POINTER)
1462 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1463 pass_param ? param : NULL);
1464 else if (length && f->type->kind == TYPE_POINTER)
1465 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1466 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1467 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1468 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1469 set_gc_used_type (find_param_structure (f->type->u.p, param),
1470 GC_POINTED_TO, NULL);
1471 else if (skip)
1472 ; /* target type is not used through this field */
1473 else
1474 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL);
1475 }
1476 break;
1477 }
1478
1479 case TYPE_POINTER:
1480 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1481 break;
1482
1483 case TYPE_ARRAY:
1484 set_gc_used_type (t->u.a.p, GC_USED, param);
1485 break;
1486
1487 case TYPE_LANG_STRUCT:
1488 for (t = t->u.s.lang_struct; t; t = t->next)
1489 set_gc_used_type (t, level, param);
1490 break;
1491
1492 case TYPE_PARAM_STRUCT:
1493 {
1494 int i;
1495 for (i = 0; i < NUM_PARAM; i++)
1496 if (t->u.param_struct.param[i] != 0)
1497 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1498 }
1499 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1500 level = GC_POINTED_TO;
1501 else
1502 level = GC_USED;
1503 t->u.param_struct.stru->gc_used = GC_UNUSED;
1504 set_gc_used_type (t->u.param_struct.stru, level,
1505 t->u.param_struct.param);
1506 break;
1507
1508 default:
1509 break;
1510 }
1511 }
1512
1513 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1514
1515 static void
1516 set_gc_used (pair_p variables)
1517 {
1518 pair_p p;
1519 for (p = variables; p; p = p->next)
1520 set_gc_used_type (p->type, GC_USED, NULL);
1521 }
1522 \f
1523 /* File mapping routines. For each input file, there is one output .c file
1524 (but some output files have many input files), and there is one .h file
1525 for the whole build. */
1526
1527 /* Output file handling. */
1528
1529 /* Create and return an outf_p for a new file for NAME, to be called
1530 ONAME. */
1531
1532 static outf_p
1533 create_file (const char *name, const char *oname)
1534 {
1535 static const char *const hdr[] = {
1536 " Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc.\n",
1537 "\n",
1538 "This file is part of GCC.\n",
1539 "\n",
1540 "GCC is free software; you can redistribute it and/or modify it under\n",
1541 "the terms of the GNU General Public License as published by the Free\n",
1542 "Software Foundation; either version 3, or (at your option) any later\n",
1543 "version.\n",
1544 "\n",
1545 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1546 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1547 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1548 "for more details.\n",
1549 "\n",
1550 "You should have received a copy of the GNU General Public License\n",
1551 "along with GCC; see the file COPYING3. If not see\n",
1552 "<http://www.gnu.org/licenses/>. */\n",
1553 "\n",
1554 "/* This file is machine generated. Do not edit. */\n"
1555 };
1556 outf_p f;
1557 size_t i;
1558
1559 gcc_assert (name != NULL);
1560 gcc_assert (oname != NULL);
1561 f = XCNEW (struct outf);
1562 f->next = output_files;
1563 f->name = oname;
1564 output_files = f;
1565
1566 oprintf (f, "/* Type information for %s.\n", name);
1567 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1568 oprintf (f, "%s", hdr[i]);
1569 return f;
1570 }
1571
1572 /* Print, like fprintf, to O.
1573 N.B. You might think this could be implemented more efficiently
1574 with vsnprintf(). Unfortunately, there are C libraries that
1575 provide that function but without the C99 semantics for its return
1576 value, making it impossible to know how much space is required. */
1577 void
1578 oprintf (outf_p o, const char *format, ...)
1579 {
1580 char *s;
1581 size_t slength;
1582 va_list ap;
1583
1584 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1585 in that case. */
1586 if (!o)
1587 return;
1588
1589 va_start (ap, format);
1590 slength = vasprintf (&s, format, ap);
1591 if (s == NULL || (int) slength < 0)
1592 fatal ("out of memory");
1593 va_end (ap);
1594
1595 if (o->bufused + slength > o->buflength)
1596 {
1597 size_t new_len = o->buflength;
1598 if (new_len == 0)
1599 new_len = 1024;
1600 do
1601 {
1602 new_len *= 2;
1603 }
1604 while (o->bufused + slength >= new_len);
1605 o->buf = XRESIZEVEC (char, o->buf, new_len);
1606 o->buflength = new_len;
1607 }
1608 memcpy (o->buf + o->bufused, s, slength);
1609 o->bufused += slength;
1610 free (s);
1611 }
1612
1613 /* Open the global header file and the language-specific header files. */
1614
1615 static void
1616 open_base_files (void)
1617 {
1618 size_t i;
1619
1620 if (nb_plugin_files > 0 && plugin_files)
1621 return;
1622
1623 header_file = create_file ("GCC", "gtype-desc.h");
1624
1625 base_files = XNEWVEC (outf_p, num_lang_dirs);
1626
1627 for (i = 0; i < num_lang_dirs; i++)
1628 base_files[i] = create_file (lang_dir_names[i],
1629 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1630
1631 /* gtype-desc.c is a little special, so we create it here. */
1632 {
1633 /* The order of files here matters very much. */
1634 static const char *const ifiles[] = {
1635 "config.h", "system.h", "coretypes.h", "tm.h",
1636 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1637 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1638 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1639 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1640 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1641 "cfglayout.h", "except.h", "output.h", "gimple.h", "cfgloop.h",
1642 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h", NULL
1643 };
1644 const char *const *ifp;
1645 outf_p gtype_desc_c;
1646
1647 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1648 for (ifp = ifiles; *ifp; ifp++)
1649 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1650
1651 /* Make sure we handle "cfun" specially. */
1652 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1653 oprintf (gtype_desc_c, "#undef cfun\n");
1654 }
1655 }
1656
1657 /* For F a filename, return the real basename of F, with all the directory
1658 components skipped. */
1659
1660 static const char *
1661 get_file_realbasename (const char *f)
1662 {
1663 const char *lastslash = strrchr (f, '/');
1664
1665 return (lastslash != NULL) ? lastslash + 1 : f;
1666 }
1667
1668 /* For F a filename, return the relative path to F from $(srcdir) if the
1669 latter is a prefix in F, NULL otherwise. */
1670
1671 static const char *
1672 get_file_srcdir_relative_path (const char *f)
1673 {
1674 if (strlen (f) > srcdir_len
1675 && IS_DIR_SEPARATOR (f[srcdir_len])
1676 && memcmp (f, srcdir, srcdir_len) == 0)
1677 return f + srcdir_len + 1;
1678 else
1679 return NULL;
1680 }
1681
1682 /* For F a filename, return the relative path to F from $(srcdir) if the
1683 latter is a prefix in F, or the real basename of F otherwise. */
1684
1685 static const char *
1686 get_file_basename (const char *f)
1687 {
1688 const char *srcdir_path = get_file_srcdir_relative_path (f);
1689
1690 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (f);
1691 }
1692
1693 /* For F a filename, return the lang_dir_names relative index of the language
1694 directory that is a prefix in F, if any, -1 otherwise. */
1695
1696 static int
1697 get_prefix_langdir_index (const char *f)
1698 {
1699 size_t f_len = strlen (f);
1700 size_t lang_index;
1701
1702 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1703 {
1704 const char *langdir = lang_dir_names[lang_index];
1705 size_t langdir_len = strlen (langdir);
1706
1707 if (f_len > langdir_len
1708 && IS_DIR_SEPARATOR (f[langdir_len])
1709 && memcmp (f, langdir, langdir_len) == 0)
1710 return lang_index;
1711 }
1712
1713 return -1;
1714 }
1715
1716 /* For F a filename, return the name of language directory where F is located,
1717 if any, NULL otherwise. */
1718
1719 static const char *
1720 get_file_langdir (const char *f)
1721 {
1722 /* Get the relative path to F from $(srcdir) and find the language by
1723 comparing the prefix with language directory names. If F is not even
1724 srcdir relative, no point in looking further. */
1725
1726 int lang_index;
1727 const char *srcdir_relative_path = get_file_srcdir_relative_path (f);
1728 const char *r;
1729
1730 if (!srcdir_relative_path)
1731 return NULL;
1732
1733 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1734 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1735 r = "c-family";
1736 else if (lang_index >= 0)
1737 r = lang_dir_names[lang_index];
1738 else
1739 r = NULL;
1740
1741 return r;
1742 }
1743
1744 /* The gt- output file name for F. */
1745
1746 static const char *
1747 get_file_gtfilename (const char *f)
1748 {
1749 /* Cook up an initial version of the gt- file name from the file real
1750 basename and the language name, if any. */
1751
1752 const char *basename = get_file_realbasename (f);
1753 const char *langdir = get_file_langdir (f);
1754
1755 char *result =
1756 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1757 : xasprintf ("gt-%s", basename));
1758
1759 /* Then replace all non alphanumerics characters by '-' and change the
1760 extension to ".h". We expect the input filename extension was at least
1761 one character long. */
1762
1763 char *s = result;
1764
1765 for (; *s != '.'; s++)
1766 if (!ISALNUM (*s) && *s != '-')
1767 *s = '-';
1768
1769 memcpy (s, ".h", sizeof (".h"));
1770
1771 return result;
1772 }
1773
1774 /* An output file, suitable for definitions, that can see declarations
1775 made in INPUT_FILE and is linked into every language that uses
1776 INPUT_FILE. */
1777
1778 outf_p
1779 get_output_file_with_visibility (const char *input_file)
1780 {
1781 outf_p r;
1782 size_t len;
1783 const char *basename;
1784 const char *for_name;
1785 const char *output_name;
1786
1787 /* This can happen when we need a file with visibility on a
1788 structure that we've never seen. We have to just hope that it's
1789 globally visible. */
1790 if (input_file == NULL)
1791 input_file = "system.h";
1792
1793 /* In plugin mode, return NULL unless the input_file is one of the
1794 plugin_files. */
1795 if (plugin_files)
1796 {
1797 size_t i;
1798 for (i = 0; i < nb_plugin_files; i++)
1799 if (strcmp (input_file, plugin_files[i]) == 0)
1800 return plugin_output;
1801
1802 return NULL;
1803 }
1804
1805 /* Determine the output file name. */
1806 basename = get_file_basename (input_file);
1807
1808 len = strlen (basename);
1809 if ((len > 2 && memcmp (basename + len - 2, ".c", 2) == 0)
1810 || (len > 2 && memcmp (basename + len - 2, ".y", 2) == 0)
1811 || (len > 3 && memcmp (basename + len - 3, ".in", 3) == 0))
1812 {
1813 output_name = get_file_gtfilename (input_file);
1814 for_name = basename;
1815 }
1816 /* Some headers get used by more than one front-end; hence, it
1817 would be inappropriate to spew them out to a single gtype-<lang>.h
1818 (and gengtype doesn't know how to direct spewage into multiple
1819 gtype-<lang>.h headers at this time). Instead, we pair up these
1820 headers with source files (and their special purpose gt-*.h headers). */
1821 else if (strncmp (basename, "c-family", 8) == 0
1822 && IS_DIR_SEPARATOR (basename[8])
1823 && strcmp (basename + 9, "c-common.h") == 0)
1824 output_name = "gt-c-family-c-common.h", for_name = "c-family/c-common.c";
1825 else if (strcmp (basename, "c-lang.h") == 0)
1826 output_name = "gt-c-decl.h", for_name = "c-decl.c";
1827 else if (strcmp (basename, "c-tree.h") == 0)
1828 output_name = "gt-c-decl.h", for_name = "c-decl.c";
1829 else if (strncmp (basename, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename[2])
1830 && strcmp (basename + 3, "cp-tree.h") == 0)
1831 output_name = "gt-cp-tree.h", for_name = "cp/tree.c";
1832 else if (strncmp (basename, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename[2])
1833 && strcmp (basename + 3, "decl.h") == 0)
1834 output_name = "gt-cp-decl.h", for_name = "cp/decl.c";
1835 else if (strncmp (basename, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename[2])
1836 && strcmp (basename + 3, "name-lookup.h") == 0)
1837 output_name = "gt-cp-name-lookup.h", for_name = "cp/name-lookup.c";
1838 else if (strncmp (basename, "objc", 4) == 0
1839 && IS_DIR_SEPARATOR (basename[4])
1840 && strcmp (basename + 5, "objc-act.h") == 0)
1841 output_name = "gt-objc-objc-act.h", for_name = "objc/objc-act.c";
1842 else
1843 {
1844 int lang_index = get_prefix_langdir_index (basename);
1845
1846 if (lang_index >= 0)
1847 return base_files[lang_index];
1848
1849 output_name = "gtype-desc.c";
1850 for_name = NULL;
1851 }
1852
1853 /* Look through to see if we've ever seen this output filename before. */
1854 for (r = output_files; r; r = r->next)
1855 if (strcmp (r->name, output_name) == 0)
1856 return r;
1857
1858 /* If not, create it. */
1859 r = create_file (for_name, output_name);
1860
1861 gcc_assert (r && r->name);
1862 return r;
1863 }
1864
1865 /* The name of an output file, suitable for definitions, that can see
1866 declarations made in INPUT_FILE and is linked into every language
1867 that uses INPUT_FILE. */
1868
1869 const char *
1870 get_output_file_name (const char *input_file)
1871 {
1872 outf_p o = get_output_file_with_visibility (input_file);
1873 if (o)
1874 return o->name;
1875 return NULL;
1876 }
1877
1878 /* Check if existing file is equal to the in memory buffer. */
1879
1880 static bool
1881 is_file_equal (outf_p of)
1882 {
1883 FILE *newfile = fopen (of->name, "r");
1884 size_t i;
1885 bool equal;
1886 if (newfile == NULL)
1887 return false;
1888
1889 equal = true;
1890 for (i = 0; i < of->bufused; i++)
1891 {
1892 int ch;
1893 ch = fgetc (newfile);
1894 if (ch == EOF || ch != (unsigned char) of->buf[i])
1895 {
1896 equal = false;
1897 break;
1898 }
1899 }
1900 fclose (newfile);
1901 return equal;
1902 }
1903
1904 /* Copy the output to its final destination,
1905 but don't unnecessarily change modification times. */
1906
1907 static void
1908 close_output_files (void)
1909 {
1910 outf_p of;
1911
1912 for (of = output_files; of; of = of->next)
1913 {
1914
1915 if (!is_file_equal (of))
1916 {
1917 FILE *newfile = fopen (of->name, "w");
1918 if (newfile == NULL)
1919 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
1920 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
1921 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
1922 if (fclose (newfile) != 0)
1923 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
1924 }
1925 free (of->buf);
1926 of->buf = NULL;
1927 of->bufused = of->buflength = 0;
1928 }
1929 }
1930 \f
1931 struct flist
1932 {
1933 struct flist *next;
1934 int started_p;
1935 const char *name;
1936 outf_p f;
1937 };
1938
1939 struct walk_type_data;
1940
1941 /* For scalars and strings, given the item in 'val'.
1942 For structures, given a pointer to the item in 'val'.
1943 For misc. pointers, given the item in 'val'.
1944 */
1945 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
1946 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
1947
1948 /* Parameters for write_types. */
1949
1950 struct write_types_data
1951 {
1952 const char *prefix;
1953 const char *param_prefix;
1954 const char *subfield_marker_routine;
1955 const char *marker_routine;
1956 const char *reorder_note_routine;
1957 const char *comment;
1958 int skip_hooks; /* skip hook generation if non zero */
1959 };
1960
1961 static void output_escaped_param (struct walk_type_data *d,
1962 const char *, const char *);
1963 static void output_mangled_typename (outf_p, const_type_p);
1964 static void walk_type (type_p t, struct walk_type_data *d);
1965 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
1966 const struct write_types_data *wtd);
1967 static void write_types_process_field
1968 (type_p f, const struct walk_type_data *d);
1969 static void write_types (outf_p output_header,
1970 type_p structures,
1971 type_p param_structs,
1972 const struct write_types_data *wtd);
1973 static void write_types_local_process_field
1974 (type_p f, const struct walk_type_data *d);
1975 static void write_local_func_for_structure
1976 (const_type_p orig_s, type_p s, type_p *param);
1977 static void write_local (outf_p output_header,
1978 type_p structures, type_p param_structs);
1979 static void write_enum_defn (type_p structures, type_p param_structs);
1980 static int contains_scalar_p (type_p t);
1981 static void put_mangled_filename (outf_p, const char *);
1982 static void finish_root_table (struct flist *flp, const char *pfx,
1983 const char *tname, const char *lastname,
1984 const char *name);
1985 static void write_root (outf_p, pair_p, type_p, const char *, int,
1986 struct fileloc *, const char *, bool);
1987 static void write_array (outf_p f, pair_p v,
1988 const struct write_types_data *wtd);
1989 static void write_roots (pair_p, bool);
1990
1991 /* Parameters for walk_type. */
1992
1993 struct walk_type_data
1994 {
1995 process_field_fn process_field;
1996 const void *cookie;
1997 outf_p of;
1998 options_p opt;
1999 const char *val;
2000 const char *prev_val[4];
2001 int indent;
2002 int counter;
2003 const struct fileloc *line;
2004 lang_bitmap bitmap;
2005 type_p *param;
2006 int used_length;
2007 type_p orig_s;
2008 const char *reorder_fn;
2009 bool needs_cast_p;
2010 bool fn_wants_lvalue;
2011 };
2012
2013 /* Print a mangled name representing T to OF. */
2014
2015 static void
2016 output_mangled_typename (outf_p of, const_type_p t)
2017 {
2018 if (t == NULL)
2019 oprintf (of, "Z");
2020 else
2021 switch (t->kind)
2022 {
2023 case TYPE_POINTER:
2024 oprintf (of, "P");
2025 output_mangled_typename (of, t->u.p);
2026 break;
2027 case TYPE_SCALAR:
2028 oprintf (of, "I");
2029 break;
2030 case TYPE_STRING:
2031 oprintf (of, "S");
2032 break;
2033 case TYPE_STRUCT:
2034 case TYPE_UNION:
2035 case TYPE_LANG_STRUCT:
2036 oprintf (of, "%lu%s", (unsigned long) strlen (t->u.s.tag),
2037 t->u.s.tag);
2038 break;
2039 case TYPE_PARAM_STRUCT:
2040 {
2041 int i;
2042 for (i = 0; i < NUM_PARAM; i++)
2043 if (t->u.param_struct.param[i] != NULL)
2044 output_mangled_typename (of, t->u.param_struct.param[i]);
2045 output_mangled_typename (of, t->u.param_struct.stru);
2046 }
2047 break;
2048 case TYPE_ARRAY:
2049 gcc_unreachable ();
2050 }
2051 }
2052
2053 /* Print PARAM to D->OF processing escapes. D->VAL references the
2054 current object, D->PREV_VAL the object containing the current
2055 object, ONAME is the name of the option and D->LINE is used to
2056 print error messages. */
2057
2058 static void
2059 output_escaped_param (struct walk_type_data *d, const char *param,
2060 const char *oname)
2061 {
2062 const char *p;
2063
2064 for (p = param; *p; p++)
2065 if (*p != '%')
2066 oprintf (d->of, "%c", *p);
2067 else
2068 switch (*++p)
2069 {
2070 case 'h':
2071 oprintf (d->of, "(%s)", d->prev_val[2]);
2072 break;
2073 case '0':
2074 oprintf (d->of, "(%s)", d->prev_val[0]);
2075 break;
2076 case '1':
2077 oprintf (d->of, "(%s)", d->prev_val[1]);
2078 break;
2079 case 'a':
2080 {
2081 const char *pp = d->val + strlen (d->val);
2082 while (pp[-1] == ']')
2083 while (*pp != '[')
2084 pp--;
2085 oprintf (d->of, "%s", pp);
2086 }
2087 break;
2088 default:
2089 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2090 oname, '%', *p);
2091 }
2092 }
2093
2094 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2095 which is of type T. Write code to D->OF to constrain execution (at
2096 the point that D->PROCESS_FIELD is called) to the appropriate
2097 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2098 pointers to those objects. D->PREV_VAL lists the objects
2099 containing the current object, D->OPT is a list of options to
2100 apply, D->INDENT is the current indentation level, D->LINE is used
2101 to print error messages, D->BITMAP indicates which languages to
2102 print the structure for, and D->PARAM is the current parameter
2103 (from an enclosing param_is option). */
2104
2105 static void
2106 walk_type (type_p t, struct walk_type_data *d)
2107 {
2108 const char *length = NULL;
2109 const char *desc = NULL;
2110 int maybe_undef_p = 0;
2111 int use_param_num = -1;
2112 int use_params_p = 0;
2113 options_p oo;
2114 const struct nested_ptr_data *nested_ptr_d = NULL;
2115
2116 d->needs_cast_p = false;
2117 for (oo = d->opt; oo; oo = oo->next)
2118 if (strcmp (oo->name, "length") == 0)
2119 length = oo->info;
2120 else if (strcmp (oo->name, "maybe_undef") == 0)
2121 maybe_undef_p = 1;
2122 else if (strncmp (oo->name, "use_param", 9) == 0
2123 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2124 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2125 else if (strcmp (oo->name, "use_params") == 0)
2126 use_params_p = 1;
2127 else if (strcmp (oo->name, "desc") == 0)
2128 desc = oo->info;
2129 else if (strcmp (oo->name, "mark_hook") == 0)
2130 ;
2131 else if (strcmp (oo->name, "nested_ptr") == 0)
2132 nested_ptr_d = (const struct nested_ptr_data *) oo->info;
2133 else if (strcmp (oo->name, "dot") == 0)
2134 ;
2135 else if (strcmp (oo->name, "tag") == 0)
2136 ;
2137 else if (strcmp (oo->name, "special") == 0)
2138 ;
2139 else if (strcmp (oo->name, "skip") == 0)
2140 ;
2141 else if (strcmp (oo->name, "default") == 0)
2142 ;
2143 else if (strcmp (oo->name, "descbits") == 0)
2144 ;
2145 else if (strcmp (oo->name, "param_is") == 0)
2146 ;
2147 else if (strncmp (oo->name, "param", 5) == 0
2148 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2149 ;
2150 else if (strcmp (oo->name, "chain_next") == 0)
2151 ;
2152 else if (strcmp (oo->name, "chain_prev") == 0)
2153 ;
2154 else if (strcmp (oo->name, "chain_circular") == 0)
2155 ;
2156 else if (strcmp (oo->name, "reorder") == 0)
2157 ;
2158 else if (strcmp (oo->name, "variable_size") == 0)
2159 ;
2160 else
2161 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2162
2163 if (d->used_length)
2164 length = NULL;
2165
2166 if (use_params_p)
2167 {
2168 int pointer_p = t->kind == TYPE_POINTER;
2169
2170 if (pointer_p)
2171 t = t->u.p;
2172 if (!UNION_OR_STRUCT_P (t))
2173 error_at_line (d->line, "`use_params' option on unimplemented type");
2174 else
2175 t = find_param_structure (t, d->param);
2176 if (pointer_p)
2177 t = create_pointer (t);
2178 }
2179
2180 if (use_param_num != -1)
2181 {
2182 if (d->param != NULL && d->param[use_param_num] != NULL)
2183 {
2184 type_p nt = d->param[use_param_num];
2185
2186 if (t->kind == TYPE_ARRAY)
2187 nt = create_array (nt, t->u.a.len);
2188 else if (length != NULL && t->kind == TYPE_POINTER)
2189 nt = create_pointer (nt);
2190 d->needs_cast_p = (t->kind != TYPE_POINTER
2191 && (nt->kind == TYPE_POINTER
2192 || nt->kind == TYPE_STRING));
2193 t = nt;
2194 }
2195 else
2196 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2197 }
2198
2199 if (maybe_undef_p
2200 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
2201 {
2202 error_at_line (d->line,
2203 "field `%s' has invalid option `maybe_undef_p'\n",
2204 d->val);
2205 return;
2206 }
2207
2208 switch (t->kind)
2209 {
2210 case TYPE_SCALAR:
2211 case TYPE_STRING:
2212 d->process_field (t, d);
2213 break;
2214
2215 case TYPE_POINTER:
2216 {
2217 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2218 {
2219 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2220 break;
2221 }
2222
2223 if (!length)
2224 {
2225 if (!UNION_OR_STRUCT_P (t->u.p)
2226 && t->u.p->kind != TYPE_PARAM_STRUCT)
2227 {
2228 error_at_line (d->line,
2229 "field `%s' is pointer to unimplemented type",
2230 d->val);
2231 break;
2232 }
2233
2234 if (nested_ptr_d)
2235 {
2236 const char *oldprevval2 = d->prev_val[2];
2237
2238 if (!UNION_OR_STRUCT_P (nested_ptr_d->type))
2239 {
2240 error_at_line (d->line,
2241 "field `%s' has invalid "
2242 "option `nested_ptr'\n", d->val);
2243 return;
2244 }
2245
2246 d->prev_val[2] = d->val;
2247 oprintf (d->of, "%*s{\n", d->indent, "");
2248 d->indent += 2;
2249 d->val = xasprintf ("x%d", d->counter++);
2250 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2251 (nested_ptr_d->type->kind == TYPE_UNION
2252 ? "union" : "struct"),
2253 nested_ptr_d->type->u.s.tag,
2254 d->fn_wants_lvalue ? "" : "const ", d->val);
2255 oprintf (d->of, "%*s", d->indent + 2, "");
2256 output_escaped_param (d, nested_ptr_d->convert_from,
2257 "nested_ptr");
2258 oprintf (d->of, ";\n");
2259
2260 d->process_field (nested_ptr_d->type, d);
2261
2262 if (d->fn_wants_lvalue)
2263 {
2264 oprintf (d->of, "%*s%s = ", d->indent, "",
2265 d->prev_val[2]);
2266 d->prev_val[2] = d->val;
2267 output_escaped_param (d, nested_ptr_d->convert_to,
2268 "nested_ptr");
2269 oprintf (d->of, ";\n");
2270 }
2271
2272 d->indent -= 2;
2273 oprintf (d->of, "%*s}\n", d->indent, "");
2274 d->val = d->prev_val[2];
2275 d->prev_val[2] = oldprevval2;
2276 }
2277 else
2278 d->process_field (t->u.p, d);
2279 }
2280 else
2281 {
2282 int loopcounter = d->counter++;
2283 const char *oldval = d->val;
2284 const char *oldprevval3 = d->prev_val[3];
2285 char *newval;
2286
2287 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2288 d->indent += 2;
2289 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2290 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2291 "", loopcounter, loopcounter);
2292 output_escaped_param (d, length, "length");
2293 oprintf (d->of, "); i%d++) {\n", loopcounter);
2294 d->indent += 2;
2295 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2296 d->used_length = 1;
2297 d->prev_val[3] = oldval;
2298 walk_type (t->u.p, d);
2299 free (newval);
2300 d->val = oldval;
2301 d->prev_val[3] = oldprevval3;
2302 d->used_length = 0;
2303 d->indent -= 2;
2304 oprintf (d->of, "%*s}\n", d->indent, "");
2305 d->process_field (t, d);
2306 d->indent -= 2;
2307 oprintf (d->of, "%*s}\n", d->indent, "");
2308 }
2309 }
2310 break;
2311
2312 case TYPE_ARRAY:
2313 {
2314 int loopcounter = d->counter++;
2315 const char *oldval = d->val;
2316 char *newval;
2317
2318 /* If it's an array of scalars, we optimize by not generating
2319 any code. */
2320 if (t->u.a.p->kind == TYPE_SCALAR)
2321 break;
2322
2323 /* When walking an array, compute the length and store it in a
2324 local variable before walking the array elements, instead of
2325 recomputing the length expression each time through the loop.
2326 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2327 where the length is stored in the first array element,
2328 because otherwise that operand can get overwritten on the
2329 first iteration. */
2330 oprintf (d->of, "%*s{\n", d->indent, "");
2331 d->indent += 2;
2332 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2333 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2334 d->indent, "", loopcounter);
2335 if (length)
2336 output_escaped_param (d, length, "length");
2337 else
2338 oprintf (d->of, "%s", t->u.a.len);
2339 oprintf (d->of, ");\n");
2340
2341 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2342 d->indent, "",
2343 loopcounter, loopcounter, loopcounter, loopcounter);
2344 d->indent += 2;
2345 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2346 d->used_length = 1;
2347 walk_type (t->u.a.p, d);
2348 free (newval);
2349 d->used_length = 0;
2350 d->val = oldval;
2351 d->indent -= 2;
2352 oprintf (d->of, "%*s}\n", d->indent, "");
2353 d->indent -= 2;
2354 oprintf (d->of, "%*s}\n", d->indent, "");
2355 }
2356 break;
2357
2358 case TYPE_STRUCT:
2359 case TYPE_UNION:
2360 {
2361 pair_p f;
2362 const char *oldval = d->val;
2363 const char *oldprevval1 = d->prev_val[1];
2364 const char *oldprevval2 = d->prev_val[2];
2365 const int union_p = t->kind == TYPE_UNION;
2366 int seen_default_p = 0;
2367 options_p o;
2368
2369 if (!t->u.s.line.file)
2370 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2371
2372 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2373 {
2374 error_at_line (d->line,
2375 "structure `%s' defined for mismatching languages",
2376 t->u.s.tag);
2377 error_at_line (&t->u.s.line, "one structure defined here");
2378 }
2379
2380 /* Some things may also be defined in the structure's options. */
2381 for (o = t->u.s.opt; o; o = o->next)
2382 if (!desc && strcmp (o->name, "desc") == 0)
2383 desc = o->info;
2384
2385 d->prev_val[2] = oldval;
2386 d->prev_val[1] = oldprevval2;
2387 if (union_p)
2388 {
2389 if (desc == NULL)
2390 {
2391 error_at_line (d->line,
2392 "missing `desc' option for union `%s'",
2393 t->u.s.tag);
2394 desc = "1";
2395 }
2396 oprintf (d->of, "%*sswitch (", d->indent, "");
2397 output_escaped_param (d, desc, "desc");
2398 oprintf (d->of, ")\n");
2399 d->indent += 2;
2400 oprintf (d->of, "%*s{\n", d->indent, "");
2401 }
2402 for (f = t->u.s.fields; f; f = f->next)
2403 {
2404 options_p oo;
2405 const char *dot = ".";
2406 const char *tagid = NULL;
2407 int skip_p = 0;
2408 int default_p = 0;
2409 int use_param_p = 0;
2410 char *newval;
2411
2412 d->reorder_fn = NULL;
2413 for (oo = f->opt; oo; oo = oo->next)
2414 if (strcmp (oo->name, "dot") == 0)
2415 dot = oo->info;
2416 else if (strcmp (oo->name, "tag") == 0)
2417 tagid = oo->info;
2418 else if (strcmp (oo->name, "skip") == 0)
2419 skip_p = 1;
2420 else if (strcmp (oo->name, "default") == 0)
2421 default_p = 1;
2422 else if (strcmp (oo->name, "reorder") == 0)
2423 d->reorder_fn = oo->info;
2424 else if (strncmp (oo->name, "use_param", 9) == 0
2425 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2426 use_param_p = 1;
2427
2428 if (skip_p)
2429 continue;
2430
2431 if (union_p && tagid)
2432 {
2433 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
2434 d->indent += 2;
2435 }
2436 else if (union_p && default_p)
2437 {
2438 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2439 d->indent += 2;
2440 seen_default_p = 1;
2441 }
2442 else if (!union_p && (default_p || tagid))
2443 error_at_line (d->line,
2444 "can't use `%s' outside a union on field `%s'",
2445 default_p ? "default" : "tag", f->name);
2446 else if (union_p && !(default_p || tagid)
2447 && f->type->kind == TYPE_SCALAR)
2448 {
2449 fprintf (stderr,
2450 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2451 d->line->file, d->line->line, f->name);
2452 continue;
2453 }
2454 else if (union_p && !(default_p || tagid))
2455 error_at_line (d->line,
2456 "field `%s' is missing `tag' or `default' option",
2457 f->name);
2458
2459 d->line = &f->line;
2460 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
2461 d->opt = f->opt;
2462 d->used_length = false;
2463
2464 if (union_p && use_param_p && d->param == NULL)
2465 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
2466 else
2467 walk_type (f->type, d);
2468
2469 free (newval);
2470
2471 if (union_p)
2472 {
2473 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2474 d->indent -= 2;
2475 }
2476 }
2477 d->reorder_fn = NULL;
2478
2479 d->val = oldval;
2480 d->prev_val[1] = oldprevval1;
2481 d->prev_val[2] = oldprevval2;
2482
2483 if (union_p && !seen_default_p)
2484 {
2485 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2486 oprintf (d->of, "%*s break;\n", d->indent, "");
2487 }
2488 if (union_p)
2489 {
2490 oprintf (d->of, "%*s}\n", d->indent, "");
2491 d->indent -= 2;
2492 }
2493 }
2494 break;
2495
2496 case TYPE_LANG_STRUCT:
2497 {
2498 type_p nt;
2499 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
2500 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
2501 break;
2502 if (nt == NULL)
2503 error_at_line (d->line, "structure `%s' differs between languages",
2504 t->u.s.tag);
2505 else
2506 walk_type (nt, d);
2507 }
2508 break;
2509
2510 case TYPE_PARAM_STRUCT:
2511 {
2512 type_p *oldparam = d->param;
2513
2514 d->param = t->u.param_struct.param;
2515 walk_type (t->u.param_struct.stru, d);
2516 d->param = oldparam;
2517 }
2518 break;
2519
2520 default:
2521 gcc_unreachable ();
2522 }
2523 }
2524
2525 /* process_field routine for marking routines. */
2526
2527 static void
2528 write_types_process_field (type_p f, const struct walk_type_data *d)
2529 {
2530 const struct write_types_data *wtd;
2531 const char *cast = d->needs_cast_p ? "(void *)" : "";
2532 wtd = (const struct write_types_data *) d->cookie;
2533
2534 switch (f->kind)
2535 {
2536 case TYPE_POINTER:
2537 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
2538 wtd->subfield_marker_routine, cast, d->val);
2539 if (wtd->param_prefix)
2540 {
2541 oprintf (d->of, ", %s", d->prev_val[3]);
2542 if (d->orig_s)
2543 {
2544 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
2545 output_mangled_typename (d->of, d->orig_s);
2546 }
2547 else
2548 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
2549
2550 if (f->u.p->kind == TYPE_PARAM_STRUCT
2551 && f->u.p->u.s.line.file != NULL)
2552 {
2553 oprintf (d->of, ", gt_e_");
2554 output_mangled_typename (d->of, f);
2555 }
2556 else if (UNION_OR_STRUCT_P (f) && f->u.p->u.s.line.file != NULL)
2557 {
2558 oprintf (d->of, ", gt_ggc_e_");
2559 output_mangled_typename (d->of, f);
2560 }
2561 else
2562 oprintf (d->of, ", gt_types_enum_last");
2563 }
2564 oprintf (d->of, ");\n");
2565 if (d->reorder_fn && wtd->reorder_note_routine)
2566 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
2567 wtd->reorder_note_routine, cast, d->val,
2568 d->prev_val[3], d->reorder_fn);
2569 break;
2570
2571 case TYPE_STRING:
2572 case TYPE_STRUCT:
2573 case TYPE_UNION:
2574 case TYPE_LANG_STRUCT:
2575 case TYPE_PARAM_STRUCT:
2576 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
2577 output_mangled_typename (d->of, f);
2578 oprintf (d->of, " (%s%s);\n", cast, d->val);
2579 if (d->reorder_fn && wtd->reorder_note_routine)
2580 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
2581 wtd->reorder_note_routine, cast, d->val, cast, d->val,
2582 d->reorder_fn);
2583 break;
2584
2585 case TYPE_SCALAR:
2586 break;
2587
2588 default:
2589 gcc_unreachable ();
2590 }
2591 }
2592
2593 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
2594
2595 static void
2596 output_type_enum (outf_p of, type_p s)
2597 {
2598 if (s->kind == TYPE_PARAM_STRUCT && s->u.param_struct.line.file != NULL)
2599 {
2600 oprintf (of, ", gt_e_");
2601 output_mangled_typename (of, s);
2602 }
2603 else if (UNION_OR_STRUCT_P (s) && s->u.s.line.file != NULL)
2604 {
2605 oprintf (of, ", gt_ggc_e_");
2606 output_mangled_typename (of, s);
2607 }
2608 else
2609 oprintf (of, ", gt_types_enum_last");
2610 }
2611
2612 /* Return an output file that is suitable for definitions which can
2613 reference struct S */
2614
2615 static outf_p
2616 get_output_file_for_structure (const_type_p s, type_p *param)
2617 {
2618 const char *fn = s->u.s.line.file;
2619 int i;
2620
2621 /* This is a hack, and not the good kind either. */
2622 for (i = NUM_PARAM - 1; i >= 0; i--)
2623 if (param && param[i] && param[i]->kind == TYPE_POINTER
2624 && UNION_OR_STRUCT_P (param[i]->u.p))
2625 fn = param[i]->u.p->u.s.line.file;
2626
2627 return get_output_file_with_visibility (fn);
2628 }
2629
2630 /* For S, a structure that's part of ORIG_S, and using parameters
2631 PARAM, write out a routine that:
2632 - Takes a parameter, a void * but actually of type *S
2633 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
2634 field of S or its substructures and (in some cases) things
2635 that are pointed to by S.
2636 */
2637
2638 static void
2639 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2640 const struct write_types_data *wtd)
2641 {
2642 const char *chain_next = NULL;
2643 const char *chain_prev = NULL;
2644 const char *chain_circular = NULL;
2645 const char *mark_hook_name = NULL;
2646 options_p opt;
2647 struct walk_type_data d;
2648
2649 memset (&d, 0, sizeof (d));
2650 d.of = get_output_file_for_structure (s, param);
2651
2652 for (opt = s->u.s.opt; opt; opt = opt->next)
2653 if (strcmp (opt->name, "chain_next") == 0)
2654 chain_next = opt->info;
2655 else if (strcmp (opt->name, "chain_prev") == 0)
2656 chain_prev = opt->info;
2657 else if (strcmp (opt->name, "chain_circular") == 0)
2658 chain_circular = opt->info;
2659 else if (strcmp (opt->name, "mark_hook") == 0)
2660 mark_hook_name = opt->info;
2661
2662 if (chain_prev != NULL && chain_next == NULL)
2663 error_at_line (&s->u.s.line, "chain_prev without chain_next");
2664 if (chain_circular != NULL && chain_next != NULL)
2665 error_at_line (&s->u.s.line, "chain_circular with chain_next");
2666 if (chain_circular != NULL)
2667 chain_next = chain_circular;
2668
2669 d.process_field = write_types_process_field;
2670 d.cookie = wtd;
2671 d.orig_s = orig_s;
2672 d.opt = s->u.s.opt;
2673 d.line = &s->u.s.line;
2674 d.bitmap = s->u.s.bitmap;
2675 d.param = param;
2676 d.prev_val[0] = "*x";
2677 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
2678 d.prev_val[3] = "x";
2679 d.val = "(*x)";
2680
2681 oprintf (d.of, "\n");
2682 oprintf (d.of, "void\n");
2683 if (param == NULL)
2684 oprintf (d.of, "gt_%sx_%s", wtd->prefix, orig_s->u.s.tag);
2685 else
2686 {
2687 oprintf (d.of, "gt_%s_", wtd->prefix);
2688 output_mangled_typename (d.of, orig_s);
2689 }
2690 oprintf (d.of, " (void *x_p)\n");
2691 oprintf (d.of, "{\n");
2692 oprintf (d.of, " %s %s * %sx = (%s %s *)x_p;\n",
2693 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
2694 chain_next == NULL ? "const " : "",
2695 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2696 if (chain_next != NULL)
2697 oprintf (d.of, " %s %s * xlimit = x;\n",
2698 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2699 if (chain_next == NULL)
2700 {
2701 oprintf (d.of, " if (%s (x", wtd->marker_routine);
2702 if (wtd->param_prefix)
2703 {
2704 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
2705 output_mangled_typename (d.of, orig_s);
2706 output_type_enum (d.of, orig_s);
2707 }
2708 oprintf (d.of, "))\n");
2709 }
2710 else
2711 {
2712 if (chain_circular != NULL)
2713 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
2714 else
2715 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
2716 if (wtd->param_prefix)
2717 {
2718 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
2719 output_mangled_typename (d.of, orig_s);
2720 output_type_enum (d.of, orig_s);
2721 }
2722 oprintf (d.of, "))\n");
2723 if (chain_circular != NULL)
2724 oprintf (d.of, " return;\n do\n");
2725 if (mark_hook_name && !wtd->skip_hooks)
2726 {
2727 oprintf (d.of, " {\n");
2728 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
2729 }
2730 oprintf (d.of, " xlimit = (");
2731 d.prev_val[2] = "*xlimit";
2732 output_escaped_param (&d, chain_next, "chain_next");
2733 oprintf (d.of, ");\n");
2734 if (mark_hook_name && !wtd->skip_hooks)
2735 oprintf (d.of, " }\n");
2736 if (chain_prev != NULL)
2737 {
2738 oprintf (d.of, " if (x != xlimit)\n");
2739 oprintf (d.of, " for (;;)\n");
2740 oprintf (d.of, " {\n");
2741 oprintf (d.of, " %s %s * const xprev = (",
2742 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2743
2744 d.prev_val[2] = "*x";
2745 output_escaped_param (&d, chain_prev, "chain_prev");
2746 oprintf (d.of, ");\n");
2747 oprintf (d.of, " if (xprev == NULL) break;\n");
2748 oprintf (d.of, " x = xprev;\n");
2749 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
2750 if (wtd->param_prefix)
2751 {
2752 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
2753 output_mangled_typename (d.of, orig_s);
2754 output_type_enum (d.of, orig_s);
2755 }
2756 oprintf (d.of, ");\n");
2757 oprintf (d.of, " }\n");
2758 }
2759 if (chain_circular != NULL)
2760 {
2761 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
2762 if (wtd->param_prefix)
2763 {
2764 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
2765 output_mangled_typename (d.of, orig_s);
2766 output_type_enum (d.of, orig_s);
2767 }
2768 oprintf (d.of, "));\n");
2769 if (mark_hook_name && !wtd->skip_hooks)
2770 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
2771 oprintf (d.of, " do\n");
2772 }
2773 else
2774 oprintf (d.of, " while (x != xlimit)\n");
2775 }
2776 oprintf (d.of, " {\n");
2777 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
2778 {
2779 oprintf (d.of, " %s (x);\n", mark_hook_name);
2780 }
2781 d.prev_val[2] = "*x";
2782 d.indent = 6;
2783 walk_type (s, &d);
2784
2785 if (chain_next != NULL)
2786 {
2787 oprintf (d.of, " x = (");
2788 output_escaped_param (&d, chain_next, "chain_next");
2789 oprintf (d.of, ");\n");
2790 }
2791
2792 oprintf (d.of, " }\n");
2793 if (chain_circular != NULL)
2794 oprintf (d.of, " while (x != xlimit);\n");
2795 oprintf (d.of, "}\n");
2796 }
2797
2798 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
2799
2800 static void
2801 write_types (outf_p output_header, type_p structures, type_p param_structs,
2802 const struct write_types_data *wtd)
2803 {
2804 type_p s;
2805
2806 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
2807 /* We first emit the macros and the declarations. Functions' code is
2808 emitted afterwards. This is needed in plugin mode. */
2809 oprintf (output_header, "/* macros and declarations */\n");
2810 for (s = structures; s; s = s->next)
2811 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
2812 {
2813 options_p opt;
2814
2815 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
2816 continue;
2817
2818 oprintf (output_header, "#define gt_%s_", wtd->prefix);
2819 output_mangled_typename (output_header, s);
2820 oprintf (output_header, "(X) do { \\\n");
2821 oprintf (output_header,
2822 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
2823 s->u.s.tag);
2824 oprintf (output_header, " } while (0)\n");
2825
2826 for (opt = s->u.s.opt; opt; opt = opt->next)
2827 if (strcmp (opt->name, "ptr_alias") == 0)
2828 {
2829 const_type_p const t = (const_type_p) opt->info;
2830 if (t->kind == TYPE_STRUCT
2831 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
2832 oprintf (output_header,
2833 "#define gt_%sx_%s gt_%sx_%s\n",
2834 wtd->prefix, s->u.s.tag, wtd->prefix, t->u.s.tag);
2835 else
2836 error_at_line (&s->u.s.line,
2837 "structure alias is not a structure");
2838 break;
2839 }
2840 if (opt)
2841 continue;
2842
2843 /* Declare the marker procedure only once. */
2844 oprintf (output_header,
2845 "extern void gt_%sx_%s (void *);\n",
2846 wtd->prefix, s->u.s.tag);
2847
2848 if (s->u.s.line.file == NULL)
2849 {
2850 fprintf (stderr, "warning: structure `%s' used but not defined\n",
2851 s->u.s.tag);
2852 continue;
2853 }
2854 }
2855
2856 for (s = param_structs; s; s = s->next)
2857 if (s->gc_used == GC_POINTED_TO)
2858 {
2859 type_p stru = s->u.param_struct.stru;
2860
2861 /* Declare the marker procedure. */
2862 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
2863 output_mangled_typename (output_header, s);
2864 oprintf (output_header, " (void *);\n");
2865
2866 if (stru->u.s.line.file == NULL)
2867 {
2868 fprintf (stderr, "warning: structure `%s' used but not defined\n",
2869 s->u.s.tag);
2870 continue;
2871 }
2872 }
2873
2874 /* At last we emit the functions code. */
2875 oprintf (output_header, "\n/* functions code */\n");
2876 for (s = structures; s; s = s->next)
2877 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
2878 {
2879 options_p opt;
2880
2881 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
2882 continue;
2883 for (opt = s->u.s.opt; opt; opt = opt->next)
2884 if (strcmp (opt->name, "ptr_alias") == 0)
2885 break;
2886 if (opt)
2887 continue;
2888
2889 if (s->kind == TYPE_LANG_STRUCT)
2890 {
2891 type_p ss;
2892 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
2893 write_func_for_structure (s, ss, NULL, wtd);
2894 }
2895 else
2896 write_func_for_structure (s, s, NULL, wtd);
2897 }
2898 for (s = param_structs; s; s = s->next)
2899 if (s->gc_used == GC_POINTED_TO)
2900 {
2901 type_p *param = s->u.param_struct.param;
2902 type_p stru = s->u.param_struct.stru;
2903 if (stru->u.s.line.file == NULL)
2904 continue;
2905 if (stru->kind == TYPE_LANG_STRUCT)
2906 {
2907 type_p ss;
2908 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
2909 write_func_for_structure (s, ss, param, wtd);
2910 }
2911 else
2912 write_func_for_structure (s, stru, param, wtd);
2913 }
2914 }
2915
2916 static const struct write_types_data ggc_wtd = {
2917 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
2918 "GC marker procedures. ",
2919 FALSE
2920 };
2921
2922 static const struct write_types_data pch_wtd = {
2923 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
2924 "gt_pch_note_reorder",
2925 "PCH type-walking procedures. ",
2926 TRUE
2927 };
2928
2929 /* Write out the local pointer-walking routines. */
2930
2931 /* process_field routine for local pointer-walking. */
2932
2933 static void
2934 write_types_local_process_field (type_p f, const struct walk_type_data *d)
2935 {
2936 switch (f->kind)
2937 {
2938 case TYPE_POINTER:
2939 case TYPE_STRUCT:
2940 case TYPE_UNION:
2941 case TYPE_LANG_STRUCT:
2942 case TYPE_PARAM_STRUCT:
2943 case TYPE_STRING:
2944 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
2945 d->prev_val[3]);
2946 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
2947 break;
2948
2949 case TYPE_SCALAR:
2950 break;
2951
2952 default:
2953 gcc_unreachable ();
2954 }
2955 }
2956
2957 /* For S, a structure that's part of ORIG_S, and using parameters
2958 PARAM, write out a routine that:
2959 - Is of type gt_note_pointers
2960 - Calls PROCESS_FIELD on each field of S or its substructures.
2961 */
2962
2963 static void
2964 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
2965 {
2966 struct walk_type_data d;
2967
2968 memset (&d, 0, sizeof (d));
2969 d.of = get_output_file_for_structure (s, param);
2970 d.process_field = write_types_local_process_field;
2971 d.opt = s->u.s.opt;
2972 d.line = &s->u.s.line;
2973 d.bitmap = s->u.s.bitmap;
2974 d.param = param;
2975 d.prev_val[0] = d.prev_val[2] = "*x";
2976 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
2977 d.prev_val[3] = "x";
2978 d.val = "(*x)";
2979 d.fn_wants_lvalue = true;
2980
2981 oprintf (d.of, "\n");
2982 oprintf (d.of, "void\n");
2983 oprintf (d.of, "gt_pch_p_");
2984 output_mangled_typename (d.of, orig_s);
2985 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
2986 "\tvoid *x_p,\n"
2987 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
2988 "\tATTRIBUTE_UNUSED void *cookie)\n");
2989 oprintf (d.of, "{\n");
2990 oprintf (d.of, " %s %s * const x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
2991 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
2992 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2993 d.indent = 2;
2994 walk_type (s, &d);
2995 oprintf (d.of, "}\n");
2996 }
2997
2998 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
2999
3000 static void
3001 write_local (outf_p output_header, type_p structures, type_p param_structs)
3002 {
3003 type_p s;
3004
3005 if (!output_header)
3006 return;
3007 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3008 for (s = structures; s; s = s->next)
3009 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3010 {
3011 options_p opt;
3012
3013 if (s->u.s.line.file == NULL)
3014 continue;
3015
3016 for (opt = s->u.s.opt; opt; opt = opt->next)
3017 if (strcmp (opt->name, "ptr_alias") == 0)
3018 {
3019 const_type_p const t = (const_type_p) opt->info;
3020 if (t->kind == TYPE_STRUCT
3021 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3022 {
3023 oprintf (output_header, "#define gt_pch_p_");
3024 output_mangled_typename (output_header, s);
3025 oprintf (output_header, " gt_pch_p_");
3026 output_mangled_typename (output_header, t);
3027 oprintf (output_header, "\n");
3028 }
3029 else
3030 error_at_line (&s->u.s.line,
3031 "structure alias is not a structure");
3032 break;
3033 }
3034 if (opt)
3035 continue;
3036
3037 /* Declare the marker procedure only once. */
3038 oprintf (output_header, "extern void gt_pch_p_");
3039 output_mangled_typename (output_header, s);
3040 oprintf (output_header,
3041 "\n (void *, void *, gt_pointer_operator, void *);\n");
3042
3043 if (s->kind == TYPE_LANG_STRUCT)
3044 {
3045 type_p ss;
3046 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3047 write_local_func_for_structure (s, ss, NULL);
3048 }
3049 else
3050 write_local_func_for_structure (s, s, NULL);
3051 }
3052
3053 for (s = param_structs; s; s = s->next)
3054 if (s->gc_used == GC_POINTED_TO)
3055 {
3056 type_p *param = s->u.param_struct.param;
3057 type_p stru = s->u.param_struct.stru;
3058
3059 /* Declare the marker procedure. */
3060 oprintf (output_header, "extern void gt_pch_p_");
3061 output_mangled_typename (output_header, s);
3062 oprintf (output_header,
3063 "\n (void *, void *, gt_pointer_operator, void *);\n");
3064
3065 if (stru->u.s.line.file == NULL)
3066 {
3067 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3068 s->u.s.tag);
3069 continue;
3070 }
3071
3072 if (stru->kind == TYPE_LANG_STRUCT)
3073 {
3074 type_p ss;
3075 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3076 write_local_func_for_structure (s, ss, param);
3077 }
3078 else
3079 write_local_func_for_structure (s, stru, param);
3080 }
3081 }
3082
3083 /* Nonzero if S is a type for which typed GC allocators should be output. */
3084
3085 #define USED_BY_TYPED_GC_P(s) \
3086 (((s->kind == TYPE_POINTER) \
3087 && ((s->u.p->gc_used == GC_POINTED_TO) \
3088 || (s->u.p->gc_used == GC_USED))) \
3089 || (UNION_OR_STRUCT_P (s) && \
3090 (((s)->gc_used == GC_POINTED_TO) \
3091 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
3092 && s->u.s.line.file != NULL) \
3093 || ((s)->gc_used == GC_USED \
3094 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
3095
3096
3097 /* Write out the 'enum' definition for gt_types_enum. */
3098
3099 static void
3100 write_enum_defn (type_p structures, type_p param_structs)
3101 {
3102 type_p s;
3103
3104 if (!header_file)
3105 return;
3106 oprintf (header_file, "\n/* Enumeration of types known. */\n");
3107 oprintf (header_file, "enum gt_types_enum {\n");
3108 for (s = structures; s; s = s->next)
3109 if (USED_BY_TYPED_GC_P (s))
3110 {
3111 oprintf (header_file, " gt_ggc_e_");
3112 output_mangled_typename (header_file, s);
3113 oprintf (header_file, ",\n");
3114 }
3115 for (s = param_structs; s; s = s->next)
3116 if (s->gc_used == GC_POINTED_TO)
3117 {
3118 oprintf (header_file, " gt_e_");
3119 output_mangled_typename (header_file, s);
3120 oprintf (header_file, ",\n");
3121 }
3122 oprintf (header_file, " gt_types_enum_last\n");
3123 oprintf (header_file, "};\n");
3124 }
3125
3126 /* Might T contain any non-pointer elements? */
3127
3128 static int
3129 contains_scalar_p (type_p t)
3130 {
3131 switch (t->kind)
3132 {
3133 case TYPE_STRING:
3134 case TYPE_POINTER:
3135 return 0;
3136 case TYPE_ARRAY:
3137 return contains_scalar_p (t->u.a.p);
3138 default:
3139 /* Could also check for structures that have no non-pointer
3140 fields, but there aren't enough of those to worry about. */
3141 return 1;
3142 }
3143 }
3144
3145 /* Mangle FN and print it to F. */
3146
3147 static void
3148 put_mangled_filename (outf_p f, const char *fn)
3149 {
3150 const char *name = get_output_file_name (fn);
3151 if (!f || !name)
3152 return;
3153 for (; *name != 0; name++)
3154 if (ISALNUM (*name))
3155 oprintf (f, "%c", *name);
3156 else
3157 oprintf (f, "%c", '_');
3158 }
3159
3160 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
3161 LASTNAME, and NAME are all strings to insert in various places in
3162 the resulting code. */
3163
3164 static void
3165 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
3166 const char *tname, const char *name)
3167 {
3168 struct flist *fli2;
3169
3170 for (fli2 = flp; fli2; fli2 = fli2->next)
3171 if (fli2->started_p)
3172 {
3173 oprintf (fli2->f, " %s\n", lastname);
3174 oprintf (fli2->f, "};\n\n");
3175 }
3176
3177 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
3178 if (fli2->started_p)
3179 {
3180 lang_bitmap bitmap = get_lang_bitmap (fli2->name);
3181 int fnum;
3182
3183 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
3184 if (bitmap & 1)
3185 {
3186 oprintf (base_files[fnum],
3187 "extern const struct %s gt_%s_", tname, pfx);
3188 put_mangled_filename (base_files[fnum], fli2->name);
3189 oprintf (base_files[fnum], "[];\n");
3190 }
3191 }
3192
3193 {
3194 size_t fnum;
3195 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3196 oprintf (base_files[fnum],
3197 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
3198 }
3199
3200
3201 for (fli2 = flp; fli2; fli2 = fli2->next)
3202 if (fli2->started_p)
3203 {
3204 lang_bitmap bitmap = get_lang_bitmap (fli2->name);
3205 int fnum;
3206
3207 fli2->started_p = 0;
3208
3209 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
3210 if (bitmap & 1)
3211 {
3212 oprintf (base_files[fnum], " gt_%s_", pfx);
3213 put_mangled_filename (base_files[fnum], fli2->name);
3214 oprintf (base_files[fnum], ",\n");
3215 }
3216 }
3217
3218 {
3219 size_t fnum;
3220 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3221 {
3222 oprintf (base_files[fnum], " NULL\n");
3223 oprintf (base_files[fnum], "};\n");
3224 }
3225 }
3226 }
3227
3228 /* Write the first three fields (pointer, count and stride) for
3229 root NAME to F. V and LINE are as for write_root.
3230
3231 Return true if the entry could be written; return false on error. */
3232
3233 static bool
3234 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
3235 {
3236 type_p ap;
3237
3238 if (!v)
3239 {
3240 error_at_line (line, "`%s' is too complex to be a root", name);
3241 return false;
3242 }
3243
3244 oprintf (f, " {\n");
3245 oprintf (f, " &%s,\n", name);
3246 oprintf (f, " 1");
3247
3248 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3249 if (ap->u.a.len[0])
3250 oprintf (f, " * (%s)", ap->u.a.len);
3251 else if (ap == v->type)
3252 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
3253 oprintf (f, ",\n");
3254 oprintf (f, " sizeof (%s", v->name);
3255 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3256 oprintf (f, "[0]");
3257 oprintf (f, "),\n");
3258 return true;
3259 }
3260
3261 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
3262 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
3263 of the caller. */
3264
3265 static void
3266 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
3267 int has_length, struct fileloc *line, const char *if_marked,
3268 bool emit_pch, type_p field_type, const char *field_name)
3269 {
3270 /* If the field reference is relative to V, rather than to some
3271 subcomponent of V, we can mark any subarrays with a single stride.
3272 We're effectively treating the field as a global variable in its
3273 own right. */
3274 if (v && type == v->type)
3275 {
3276 struct pair newv;
3277
3278 newv = *v;
3279 newv.type = field_type;
3280 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
3281 v = &newv;
3282 }
3283 /* Otherwise, any arrays nested in the structure are too complex to
3284 handle. */
3285 else if (field_type->kind == TYPE_ARRAY)
3286 v = NULL;
3287 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
3288 has_length, line, if_marked, emit_pch);
3289 }
3290
3291 /* Write out to F the table entry and any marker routines needed to
3292 mark NAME as TYPE. V can be one of three values:
3293
3294 - null, if NAME is too complex to represent using a single
3295 count and stride. In this case, it is an error for NAME to
3296 contain any gc-ed data.
3297
3298 - the outermost array that contains NAME, if NAME is part of an array.
3299
3300 - the C variable that contains NAME, if NAME is not part of an array.
3301
3302 LINE is the line of the C source that declares the root variable.
3303 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
3304 is nonzero iff we are building the root table for hash table caches. */
3305
3306 static void
3307 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
3308 struct fileloc *line, const char *if_marked, bool emit_pch)
3309 {
3310 switch (type->kind)
3311 {
3312 case TYPE_STRUCT:
3313 {
3314 pair_p fld;
3315 for (fld = type->u.s.fields; fld; fld = fld->next)
3316 {
3317 int skip_p = 0;
3318 const char *desc = NULL;
3319 options_p o;
3320
3321 for (o = fld->opt; o; o = o->next)
3322 if (strcmp (o->name, "skip") == 0)
3323 skip_p = 1;
3324 else if (strcmp (o->name, "desc") == 0)
3325 desc = o->info;
3326 else if (strcmp (o->name, "param_is") == 0)
3327 ;
3328 else
3329 error_at_line (line,
3330 "field `%s' of global `%s' has unknown option `%s'",
3331 fld->name, name, o->name);
3332
3333 if (skip_p)
3334 continue;
3335 else if (desc && fld->type->kind == TYPE_UNION)
3336 {
3337 pair_p validf = NULL;
3338 pair_p ufld;
3339
3340 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
3341 {
3342 const char *tag = NULL;
3343 options_p oo;
3344
3345 for (oo = ufld->opt; oo; oo = oo->next)
3346 if (strcmp (oo->name, "tag") == 0)
3347 tag = oo->info;
3348 if (tag == NULL || strcmp (tag, desc) != 0)
3349 continue;
3350 if (validf != NULL)
3351 error_at_line (line,
3352 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
3353 name, fld->name, validf->name,
3354 name, fld->name, ufld->name, tag);
3355 validf = ufld;
3356 }
3357 if (validf != NULL)
3358 write_field_root (f, v, type, name, 0, line, if_marked,
3359 emit_pch, validf->type,
3360 ACONCAT ((fld->name, ".",
3361 validf->name, NULL)));
3362 }
3363 else if (desc)
3364 error_at_line (line,
3365 "global `%s.%s' has `desc' option but is not union",
3366 name, fld->name);
3367 else
3368 write_field_root (f, v, type, name, 0, line, if_marked,
3369 emit_pch, fld->type, fld->name);
3370 }
3371 }
3372 break;
3373
3374 case TYPE_ARRAY:
3375 {
3376 char *newname;
3377 newname = xasprintf ("%s[0]", name);
3378 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
3379 emit_pch);
3380 free (newname);
3381 }
3382 break;
3383
3384 case TYPE_POINTER:
3385 {
3386 type_p tp;
3387
3388 if (!start_root_entry (f, v, name, line))
3389 return;
3390
3391 tp = type->u.p;
3392
3393 if (!has_length && UNION_OR_STRUCT_P (tp))
3394 {
3395 oprintf (f, " &gt_ggc_mx_%s,\n", tp->u.s.tag);
3396 if (emit_pch)
3397 oprintf (f, " &gt_pch_nx_%s", tp->u.s.tag);
3398 else
3399 oprintf (f, " NULL");
3400 }
3401 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
3402 {
3403 oprintf (f, " &gt_ggc_m_");
3404 output_mangled_typename (f, tp);
3405 if (emit_pch)
3406 {
3407 oprintf (f, ",\n &gt_pch_n_");
3408 output_mangled_typename (f, tp);
3409 }
3410 else
3411 oprintf (f, ",\n NULL");
3412 }
3413 else if (has_length
3414 && (tp->kind == TYPE_POINTER || UNION_OR_STRUCT_P (tp)))
3415 {
3416 oprintf (f, " &gt_ggc_ma_%s,\n", name);
3417 if (emit_pch)
3418 oprintf (f, " &gt_pch_na_%s", name);
3419 else
3420 oprintf (f, " NULL");
3421 }
3422 else
3423 {
3424 error_at_line (line,
3425 "global `%s' is pointer to unimplemented type",
3426 name);
3427 }
3428 if (if_marked)
3429 oprintf (f, ",\n &%s", if_marked);
3430 oprintf (f, "\n },\n");
3431 }
3432 break;
3433
3434 case TYPE_STRING:
3435 {
3436 if (!start_root_entry (f, v, name, line))
3437 return;
3438
3439 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
3440 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
3441 oprintf (f, " },\n");
3442 }
3443 break;
3444
3445 case TYPE_SCALAR:
3446 break;
3447
3448 default:
3449 error_at_line (line, "global `%s' is unimplemented type", name);
3450 }
3451 }
3452
3453 /* This generates a routine to walk an array. */
3454
3455 static void
3456 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
3457 {
3458 struct walk_type_data d;
3459 char *prevval3;
3460
3461 memset (&d, 0, sizeof (d));
3462 d.of = f;
3463 d.cookie = wtd;
3464 d.indent = 2;
3465 d.line = &v->line;
3466 d.opt = v->opt;
3467 d.bitmap = get_lang_bitmap (v->line.file);
3468 d.param = NULL;
3469
3470 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
3471
3472 if (wtd->param_prefix)
3473 {
3474 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
3475 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
3476 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
3477 wtd->param_prefix, v->name);
3478 oprintf (d.of,
3479 " ATTRIBUTE_UNUSED void *x_p,\n"
3480 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3481 " ATTRIBUTE_UNUSED void * cookie)\n");
3482 oprintf (d.of, "{\n");
3483 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3484 d.process_field = write_types_local_process_field;
3485 walk_type (v->type, &d);
3486 oprintf (f, "}\n\n");
3487 }
3488
3489 d.opt = v->opt;
3490 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
3491 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
3492 wtd->prefix, v->name);
3493 oprintf (f, "{\n");
3494 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3495 d.process_field = write_types_process_field;
3496 walk_type (v->type, &d);
3497 free (prevval3);
3498 oprintf (f, "}\n\n");
3499 }
3500
3501 /* Output a table describing the locations and types of VARIABLES. */
3502
3503 static void
3504 write_roots (pair_p variables, bool emit_pch)
3505 {
3506 pair_p v;
3507 struct flist *flp = NULL;
3508
3509 for (v = variables; v; v = v->next)
3510 {
3511 outf_p f = get_output_file_with_visibility (v->line.file);
3512 struct flist *fli;
3513 const char *length = NULL;
3514 int deletable_p = 0;
3515 options_p o;
3516
3517 for (o = v->opt; o; o = o->next)
3518 if (strcmp (o->name, "length") == 0)
3519 length = o->info;
3520 else if (strcmp (o->name, "deletable") == 0)
3521 deletable_p = 1;
3522 else if (strcmp (o->name, "param_is") == 0)
3523 ;
3524 else if (strncmp (o->name, "param", 5) == 0
3525 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
3526 ;
3527 else if (strcmp (o->name, "if_marked") == 0)
3528 ;
3529 else
3530 error_at_line (&v->line,
3531 "global `%s' has unknown option `%s'",
3532 v->name, o->name);
3533
3534 for (fli = flp; fli; fli = fli->next)
3535 if (fli->f == f && f)
3536 break;
3537 if (fli == NULL)
3538 {
3539 fli = XNEW (struct flist);
3540 fli->f = f;
3541 fli->next = flp;
3542 fli->started_p = 0;
3543 fli->name = v->line.file;
3544 gcc_assert (fli->name);
3545 flp = fli;
3546
3547 oprintf (f, "\n/* GC roots. */\n\n");
3548 }
3549
3550 if (!deletable_p
3551 && length
3552 && v->type->kind == TYPE_POINTER
3553 && (v->type->u.p->kind == TYPE_POINTER
3554 || v->type->u.p->kind == TYPE_STRUCT))
3555 {
3556 write_array (f, v, &ggc_wtd);
3557 write_array (f, v, &pch_wtd);
3558 }
3559 }
3560
3561 for (v = variables; v; v = v->next)
3562 {
3563 outf_p f = get_output_file_with_visibility (v->line.file);
3564 struct flist *fli;
3565 int skip_p = 0;
3566 int length_p = 0;
3567 options_p o;
3568
3569 for (o = v->opt; o; o = o->next)
3570 if (strcmp (o->name, "length") == 0)
3571 length_p = 1;
3572 else if (strcmp (o->name, "deletable") == 0
3573 || strcmp (o->name, "if_marked") == 0)
3574 skip_p = 1;
3575
3576 if (skip_p)
3577 continue;
3578
3579 for (fli = flp; fli; fli = fli->next)
3580 if (fli->f == f)
3581 break;
3582 if (!fli->started_p)
3583 {
3584 fli->started_p = 1;
3585
3586 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
3587 put_mangled_filename (f, v->line.file);
3588 oprintf (f, "[] = {\n");
3589 }
3590
3591 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
3592 }
3593
3594 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3595 "gt_ggc_rtab");
3596
3597 for (v = variables; v; v = v->next)
3598 {
3599 outf_p f = get_output_file_with_visibility (v->line.file);
3600 struct flist *fli;
3601 int skip_p = 1;
3602 options_p o;
3603
3604 for (o = v->opt; o; o = o->next)
3605 if (strcmp (o->name, "deletable") == 0)
3606 skip_p = 0;
3607 else if (strcmp (o->name, "if_marked") == 0)
3608 skip_p = 1;
3609
3610 if (skip_p)
3611 continue;
3612
3613 for (fli = flp; fli; fli = fli->next)
3614 if (fli->f == f)
3615 break;
3616 if (!fli->started_p)
3617 {
3618 fli->started_p = 1;
3619
3620 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
3621 put_mangled_filename (f, v->line.file);
3622 oprintf (f, "[] = {\n");
3623 }
3624
3625 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3626 v->name, v->name);
3627 }
3628
3629 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3630 "gt_ggc_deletable_rtab");
3631
3632 for (v = variables; v; v = v->next)
3633 {
3634 outf_p f = get_output_file_with_visibility (v->line.file);
3635 struct flist *fli;
3636 const char *if_marked = NULL;
3637 int length_p = 0;
3638 options_p o;
3639
3640 for (o = v->opt; o; o = o->next)
3641 if (strcmp (o->name, "length") == 0)
3642 length_p = 1;
3643 else if (strcmp (o->name, "if_marked") == 0)
3644 if_marked = o->info;
3645
3646 if (if_marked == NULL)
3647 continue;
3648
3649 if (v->type->kind != TYPE_POINTER
3650 || v->type->u.p->kind != TYPE_PARAM_STRUCT
3651 || v->type->u.p->u.param_struct.stru != find_structure ("htab", 0))
3652 {
3653 error_at_line (&v->line,
3654 "if_marked option used but not hash table");
3655 continue;
3656 }
3657
3658 for (fli = flp; fli; fli = fli->next)
3659 if (fli->f == f)
3660 break;
3661 if (!fli->started_p)
3662 {
3663 fli->started_p = 1;
3664
3665 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
3666 put_mangled_filename (f, v->line.file);
3667 oprintf (f, "[] = {\n");
3668 }
3669
3670 write_root (f, v, v->type->u.p->u.param_struct.param[0],
3671 v->name, length_p, &v->line, if_marked, emit_pch);
3672 }
3673
3674 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
3675 "gt_ggc_cache_rtab");
3676
3677 if (!emit_pch)
3678 return;
3679
3680 for (v = variables; v; v = v->next)
3681 {
3682 outf_p f = get_output_file_with_visibility (v->line.file);
3683 struct flist *fli;
3684 int length_p = 0;
3685 int if_marked_p = 0;
3686 options_p o;
3687
3688 for (o = v->opt; o; o = o->next)
3689 if (strcmp (o->name, "length") == 0)
3690 length_p = 1;
3691 else if (strcmp (o->name, "if_marked") == 0)
3692 if_marked_p = 1;
3693
3694 if (!if_marked_p)
3695 continue;
3696
3697 for (fli = flp; fli; fli = fli->next)
3698 if (fli->f == f)
3699 break;
3700 if (!fli->started_p)
3701 {
3702 fli->started_p = 1;
3703
3704 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
3705 put_mangled_filename (f, v->line.file);
3706 oprintf (f, "[] = {\n");
3707 }
3708
3709 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
3710 }
3711
3712 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3713 "gt_pch_cache_rtab");
3714
3715 for (v = variables; v; v = v->next)
3716 {
3717 outf_p f = get_output_file_with_visibility (v->line.file);
3718 struct flist *fli;
3719 int skip_p = 0;
3720 options_p o;
3721
3722 for (o = v->opt; o; o = o->next)
3723 if (strcmp (o->name, "deletable") == 0
3724 || strcmp (o->name, "if_marked") == 0)
3725 skip_p = 1;
3726
3727 if (skip_p)
3728 continue;
3729
3730 if (!contains_scalar_p (v->type))
3731 continue;
3732
3733 for (fli = flp; fli; fli = fli->next)
3734 if (fli->f == f)
3735 break;
3736 if (!fli->started_p)
3737 {
3738 fli->started_p = 1;
3739
3740 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
3741 put_mangled_filename (f, v->line.file);
3742 oprintf (f, "[] = {\n");
3743 }
3744
3745 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3746 v->name, v->name);
3747 }
3748
3749 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3750 "gt_pch_scalar_rtab");
3751 }
3752
3753 /* Record the definition of a generic VEC structure, as if we had expanded
3754 the macros in vec.h:
3755
3756 typedef struct VEC_<type>_base GTY(()) {
3757 unsigned num;
3758 unsigned alloc;
3759 <type> GTY((length ("%h.num"))) vec[1];
3760 } VEC_<type>_base
3761
3762 where the GTY(()) tags are only present if is_scalar is _false_. */
3763
3764 void
3765 note_def_vec (const char *type_name, bool is_scalar, struct fileloc *pos)
3766 {
3767 pair_p fields;
3768 type_p t;
3769 options_p o;
3770 type_p len_ty = create_scalar_type ("unsigned");
3771 const char *name = concat ("VEC_", type_name, "_base", (char *) 0);
3772
3773 if (is_scalar)
3774 {
3775 t = create_scalar_type (type_name);
3776 o = 0;
3777 }
3778 else
3779 {
3780 t = resolve_typedef (type_name, pos);
3781 o = create_option (0, "length", "%h.num");
3782 }
3783
3784 /* We assemble the field list in reverse order. */
3785 fields = create_field_at (0, create_array (t, "1"), "vec", o, pos);
3786 fields = create_field_at (fields, len_ty, "alloc", 0, pos);
3787 fields = create_field_at (fields, len_ty, "num", 0, pos);
3788
3789 do_typedef (name, new_structure (name, 0, pos, fields, 0), pos);
3790 }
3791
3792 /* Record the definition of an allocation-specific VEC structure, as if
3793 we had expanded the macros in vec.h:
3794
3795 typedef struct VEC_<type>_<astrat> {
3796 VEC_<type>_base base;
3797 } VEC_<type>_<astrat>;
3798 */
3799 void
3800 note_def_vec_alloc (const char *type, const char *astrat, struct fileloc *pos)
3801 {
3802 const char *astratname = concat ("VEC_", type, "_", astrat, (char *) 0);
3803 const char *basename = concat ("VEC_", type, "_base", (char *) 0);
3804
3805 pair_p field = create_field_at (0, resolve_typedef (basename, pos),
3806 "base", 0, pos);
3807
3808 do_typedef (astratname, new_structure (astratname, 0, pos, field, 0), pos);
3809 }
3810
3811 /* Returns the specifier keyword for a string or union type S, empty string
3812 otherwise. */
3813
3814 static const char *
3815 get_type_specifier (const type_p s)
3816 {
3817 if (s->kind == TYPE_STRUCT || s->kind == TYPE_LANG_STRUCT)
3818 return "struct ";
3819 if (s->kind == TYPE_UNION)
3820 return "union ";
3821 return "";
3822 }
3823
3824 /* TRUE if type S has the GTY variable_size annotation. */
3825
3826 static bool
3827 variable_size_p (const type_p s)
3828 {
3829 options_p o;
3830 for (o = s->u.s.opt; o; o = o->next)
3831 if (strcmp (o->name, "variable_size") == 0)
3832 return true;
3833 return false;
3834 }
3835
3836 enum alloc_quantity
3837 { single, vector };
3838 enum alloc_zone
3839 { any_zone, specific_zone };
3840
3841 /* Writes one typed allocator definition for type identifier TYPE_NAME with
3842 optional type specifier TYPE_SPECIFIER. The allocator name will contain
3843 ALLOCATOR_TYPE. If VARIABLE_SIZE is true, the allocator will have an extra
3844 parameter specifying number of bytes to allocate. If QUANTITY is set to
3845 VECTOR, a vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
3846 the allocator will be zone-specific. */
3847
3848 static void
3849 write_typed_alloc_def (bool variable_size, const char *type_specifier,
3850 const char *type_name, const char *allocator_type,
3851 enum alloc_quantity quantity, enum alloc_zone zone)
3852 {
3853 bool two_args = variable_size && (quantity == vector);
3854 bool third_arg = ((zone == specific_zone)
3855 && (variable_size || (quantity == vector)));
3856
3857 oprintf (header_file, "#define ggc_alloc_%s%s", allocator_type, type_name);
3858 oprintf (header_file, "(%s%s%s%s%s) ",
3859 (variable_size ? "SIZE" : ""),
3860 (two_args ? ", " : ""),
3861 (quantity == vector) ? "n" : "",
3862 (third_arg ? ", " : ""), (zone == specific_zone) ? "z" : "");
3863 oprintf (header_file, "((%s%s *)", type_specifier, type_name);
3864 oprintf (header_file, "(ggc_internal_%salloc_stat (", allocator_type);
3865 if (zone == specific_zone)
3866 oprintf (header_file, "z, ");
3867 if (variable_size)
3868 oprintf (header_file, "SIZE");
3869 else
3870 oprintf (header_file, "sizeof (%s%s)", type_specifier, type_name);
3871 if (quantity == vector)
3872 oprintf (header_file, ", n");
3873 oprintf (header_file, " MEM_STAT_INFO)))\n");
3874 }
3875
3876 /* Writes a typed allocator definition for a struct or union S. */
3877
3878 static void
3879 write_typed_struct_alloc_def (const type_p s, const char *allocator_type,
3880 enum alloc_quantity quantity,
3881 enum alloc_zone zone)
3882 {
3883 write_typed_alloc_def (variable_size_p (s), get_type_specifier (s),
3884 s->u.s.tag, allocator_type, quantity, zone);
3885 }
3886
3887 /* Writes a typed allocator definition for a typedef P. */
3888
3889 static void
3890 write_typed_typedef_alloc_def (const pair_p p, const char *allocator_type,
3891 enum alloc_quantity quantity,
3892 enum alloc_zone zone)
3893 {
3894 write_typed_alloc_def (variable_size_p (p->type), "", p->name,
3895 allocator_type, quantity, zone);
3896 }
3897
3898 /* Writes typed allocator definitions for the types in STRUCTURES and
3899 TYPEDEFS that are used by GC. */
3900
3901 static void
3902 write_typed_alloc_defns (const type_p structures, const pair_p typedefs)
3903 {
3904 type_p s;
3905 pair_p p;
3906
3907 oprintf (header_file,
3908 "\n/* Allocators for known structs and unions. */\n\n");
3909 for (s = structures; s; s = s->next)
3910 {
3911 if (!USED_BY_TYPED_GC_P (s))
3912 continue;
3913 write_typed_struct_alloc_def (s, "", single, any_zone);
3914 write_typed_struct_alloc_def (s, "cleared_", single, any_zone);
3915 write_typed_struct_alloc_def (s, "vec_", vector, any_zone);
3916 write_typed_struct_alloc_def (s, "cleared_vec_", vector, any_zone);
3917 write_typed_struct_alloc_def (s, "zone_", single, specific_zone);
3918 write_typed_struct_alloc_def (s, "zone_cleared_", single,
3919 specific_zone);
3920 write_typed_struct_alloc_def (s, "zone_vec_", vector, specific_zone);
3921 write_typed_struct_alloc_def (s, "zone_cleared_vec_", vector,
3922 specific_zone);
3923 }
3924
3925 oprintf (header_file, "\n/* Allocators for known typedefs. */\n");
3926 for (p = typedefs; p; p = p->next)
3927 {
3928 s = p->type;
3929 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
3930 continue;
3931 write_typed_typedef_alloc_def (p, "", single, any_zone);
3932 write_typed_typedef_alloc_def (p, "cleared_", single, any_zone);
3933 write_typed_typedef_alloc_def (p, "vec_", vector, any_zone);
3934 write_typed_typedef_alloc_def (p, "cleared_vec_", vector, any_zone);
3935 write_typed_typedef_alloc_def (p, "zone_", single, specific_zone);
3936 write_typed_typedef_alloc_def (p, "zone_cleared_", single,
3937 specific_zone);
3938 write_typed_typedef_alloc_def (p, "zone_cleared_vec_", vector,
3939 specific_zone);
3940 }
3941 }
3942
3943 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
3944 guaranteee for somewhat increased readability. If name conflicts do happen,
3945 this funcion will have to be adjusted to be more like
3946 output_mangled_typename. */
3947
3948 static void
3949 output_typename (outf_p of, const_type_p t)
3950 {
3951 switch (t->kind)
3952 {
3953 case TYPE_STRING:
3954 oprintf (of, "str");
3955 break;
3956 case TYPE_SCALAR:
3957 oprintf (of, "scalar");
3958 break;
3959 case TYPE_POINTER:
3960 output_typename (of, t->u.p);
3961 break;
3962 case TYPE_STRUCT:
3963 case TYPE_UNION:
3964 case TYPE_LANG_STRUCT:
3965 oprintf (of, "%s", t->u.s.tag);
3966 break;
3967 case TYPE_PARAM_STRUCT:
3968 {
3969 int i;
3970 for (i = 0; i < NUM_PARAM; i++)
3971 if (t->u.param_struct.param[i] != NULL)
3972 {
3973 output_typename (of, t->u.param_struct.param[i]);
3974 oprintf (of, "_");
3975 }
3976 output_typename (of, t->u.param_struct.stru);
3977 break;
3978 }
3979 default:
3980 gcc_unreachable ();
3981 }
3982 }
3983
3984 /* Writes a typed GC allocator for type S that is suitable as a callback for
3985 the splay tree implementation in libiberty. */
3986
3987 static void
3988 write_splay_tree_allocator_def (const_type_p s)
3989 {
3990 outf_p of = get_output_file_for_structure (s, NULL);
3991 oprintf (of, "void * ggc_alloc_splay_tree_");
3992 output_typename (of, s);
3993 oprintf (of, " (int sz, void * nl)\n");
3994 oprintf (of, "{\n");
3995 oprintf (of, " return ggc_splay_alloc (");
3996 oprintf (of, "gt_e_");
3997 output_mangled_typename (of, s);
3998 oprintf (of, ", sz, nl);\n");
3999 oprintf (of, "}\n\n");
4000 }
4001
4002 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4003 for the splay tree implementation in libiberty. */
4004
4005 static void
4006 write_splay_tree_allocators (const_type_p param_structs)
4007 {
4008 const_type_p s;
4009
4010 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4011 for (s = param_structs; s; s = s->next)
4012 if (s->gc_used == GC_POINTED_TO)
4013 {
4014 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4015 output_typename (header_file, s);
4016 oprintf (header_file, " (int, void *);\n");
4017 write_splay_tree_allocator_def (s);
4018 }
4019 }
4020
4021 static void dump_pair (int indent, pair_p p);
4022 static void dump_type (int indent, type_p p);
4023 static void dump_type_list (int indent, type_p p);
4024
4025 #define INDENT 2
4026
4027 /* Dumps the value of typekind KIND. */
4028
4029 static void
4030 dump_typekind (int indent, enum typekind kind)
4031 {
4032 printf ("%*ckind = ", indent, ' ');
4033 switch (kind)
4034 {
4035 case TYPE_SCALAR:
4036 printf ("TYPE_SCALAR");
4037 break;
4038 case TYPE_STRING:
4039 printf ("TYPE_STRING");
4040 break;
4041 case TYPE_STRUCT:
4042 printf ("TYPE_STRUCT");
4043 break;
4044 case TYPE_UNION:
4045 printf ("TYPE_UNION");
4046 break;
4047 case TYPE_POINTER:
4048 printf ("TYPE_POINTER");
4049 break;
4050 case TYPE_ARRAY:
4051 printf ("TYPE_ARRAY");
4052 break;
4053 case TYPE_LANG_STRUCT:
4054 printf ("TYPE_LANG_STRUCT");
4055 break;
4056 case TYPE_PARAM_STRUCT:
4057 printf ("TYPE_PARAM_STRUCT");
4058 break;
4059 default:
4060 gcc_unreachable ();
4061 }
4062 printf ("\n");
4063 }
4064
4065 /* Dumps the value of GC_USED flag. */
4066
4067 static void
4068 dump_gc_used (int indent, enum gc_used_enum gc_used)
4069 {
4070 printf ("%*cgc_used = ", indent, ' ');
4071 switch (gc_used)
4072 {
4073 case GC_UNUSED:
4074 printf ("GC_UNUSED");
4075 break;
4076 case GC_USED:
4077 printf ("GC_USED");
4078 break;
4079 case GC_MAYBE_POINTED_TO:
4080 printf ("GC_MAYBE_POINTED_TO");
4081 break;
4082 case GC_POINTED_TO:
4083 printf ("GC_POINTED_TO");
4084 break;
4085 default:
4086 gcc_unreachable ();
4087 }
4088 printf ("\n");
4089 }
4090
4091 /* Dumps the type options OPT. */
4092
4093 static void
4094 dump_options (int indent, options_p opt)
4095 {
4096 options_p o;
4097 printf ("%*coptions = ", indent, ' ');
4098 o = opt;
4099 while (o)
4100 {
4101 printf ("%s:%s ", o->name, o->info);
4102 o = o->next;
4103 }
4104 printf ("\n");
4105 }
4106
4107 /* Dumps the source file location in LINE. */
4108
4109 static void
4110 dump_fileloc (int indent, struct fileloc line)
4111 {
4112 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ', line.file,
4113 line.line);
4114 }
4115
4116 /* Recursively dumps the struct, union, or a language-specific
4117 struct T. */
4118
4119 static void
4120 dump_type_u_s (int indent, type_p t)
4121 {
4122 pair_p fields;
4123
4124 gcc_assert (t->kind == TYPE_STRUCT || t->kind == TYPE_UNION
4125 || t->kind == TYPE_LANG_STRUCT);
4126 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4127 dump_fileloc (indent, t->u.s.line);
4128 printf ("%*cu.s.fields =\n", indent, ' ');
4129 fields = t->u.s.fields;
4130 while (fields)
4131 {
4132 dump_pair (indent + INDENT, fields);
4133 fields = fields->next;
4134 }
4135 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4136 dump_options (indent, t->u.s.opt);
4137 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4138 if (t->kind == TYPE_LANG_STRUCT)
4139 {
4140 printf ("%*cu.s.lang_struct:\n", indent, ' ');
4141 dump_type_list (indent + INDENT, t->u.s.lang_struct);
4142 }
4143 }
4144
4145 /* Recursively dumps the array T. */
4146
4147 static void
4148 dump_type_u_a (int indent, type_p t)
4149 {
4150 gcc_assert (t->kind == TYPE_ARRAY);
4151 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4152 dump_type_list (indent + INDENT, t->u.a.p);
4153 }
4154
4155 /* Recursively dumps the parameterized struct T. */
4156
4157 static void
4158 dump_type_u_param_struct (int indent, type_p t)
4159 {
4160 int i;
4161 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
4162 printf ("%*cu.param_struct.stru:\n", indent, ' ');
4163 dump_type_list (indent, t->u.param_struct.stru);
4164 dump_fileloc (indent, t->u.param_struct.line);
4165 for (i = 0; i < NUM_PARAM; i++)
4166 {
4167 if (t->u.param_struct.param[i] == NULL)
4168 continue;
4169 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
4170 dump_type (indent + INDENT, t->u.param_struct.param[i]);
4171 }
4172 }
4173
4174 /* Recursively dumps the type list T. */
4175
4176 static void
4177 dump_type_list (int indent, type_p t)
4178 {
4179 type_p p = t;
4180 while (p)
4181 {
4182 dump_type (indent, p);
4183 p = p->next;
4184 }
4185 }
4186
4187 static htab_t seen_types;
4188
4189 /* Recursively dumps the type T if it was not dumped previously. */
4190
4191 static void
4192 dump_type (int indent, type_p t)
4193 {
4194 PTR *slot;
4195
4196 printf ("%*cType at %p: ", indent, ' ', (void *) t);
4197 slot = htab_find_slot (seen_types, t, INSERT);
4198 if (*slot != NULL)
4199 {
4200 printf ("already seen.\n");
4201 return;
4202 }
4203 *slot = t;
4204 printf ("\n");
4205
4206 dump_typekind (indent, t->kind);
4207 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4208 (void *) t->pointer_to);
4209 dump_gc_used (indent + INDENT, t->gc_used);
4210 switch (t->kind)
4211 {
4212 case TYPE_SCALAR:
4213 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4214 t->u.scalar_is_char ? "true" : "false");
4215 break;
4216 case TYPE_STRING:
4217 break;
4218 case TYPE_STRUCT:
4219 case TYPE_UNION:
4220 case TYPE_LANG_STRUCT:
4221 dump_type_u_s (indent + INDENT, t);
4222 break;
4223 case TYPE_POINTER:
4224 printf ("%*cp:\n", indent + INDENT, ' ');
4225 dump_type (indent + INDENT, t->u.p);
4226 break;
4227 case TYPE_ARRAY:
4228 dump_type_u_a (indent + INDENT, t);
4229 break;
4230 case TYPE_PARAM_STRUCT:
4231 dump_type_u_param_struct (indent + INDENT, t);
4232 break;
4233 default:
4234 gcc_unreachable ();
4235 }
4236 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4237 }
4238
4239 /* Dumps the pair P. */
4240
4241 static void
4242 dump_pair (int indent, pair_p p)
4243 {
4244 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4245 dump_type (indent, p->type);
4246 dump_fileloc (indent, p->line);
4247 dump_options (indent, p->opt);
4248 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4249 }
4250
4251 /* Dumps the list of pairs PP. */
4252
4253 static void
4254 dump_pair_list (const char *name, pair_p pp)
4255 {
4256 pair_p p;
4257 printf ("%s:\n", name);
4258 for (p = pp; p != NULL; p = p->next)
4259 dump_pair (0, p);
4260 printf ("End of %s\n\n", name);
4261 }
4262
4263 /* Dumps the STRUCTURES. */
4264
4265 static void
4266 dump_structures (const char *name, type_p structures)
4267 {
4268 printf ("%s:\n", name);
4269 dump_type_list (0, structures);
4270 printf ("End of %s\n\n", name);
4271 }
4272
4273 /* Dumps the internal structures of gengtype. */
4274
4275 static void
4276 dump_everything (void)
4277 {
4278 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4279 dump_pair_list ("typedefs", typedefs);
4280 dump_structures ("structures", structures);
4281 dump_structures ("param_structs", param_structs);
4282 dump_pair_list ("variables", variables);
4283 htab_delete (seen_types);
4284 }
4285 \f
4286
4287
4288 /* Option specification for getopt_long. */
4289 static const struct option gengtype_long_options[] = {
4290 {"help", no_argument, NULL, 'h'},
4291 {"version", no_argument, NULL, 'V'},
4292 {"dump", no_argument, NULL, 'd'},
4293 {"debug", no_argument, NULL, 'D'},
4294 {"plugin", required_argument, NULL, 'P'},
4295 {"srcdir", required_argument, NULL, 'S'},
4296 {"inputs", required_argument, NULL, 'I'},
4297 {"read-state", required_argument, NULL, 'r'},
4298 {"write-state", required_argument, NULL, 'w'},
4299 /* Terminating NULL placeholder. */
4300 {NULL, no_argument, NULL, 0},
4301 };
4302
4303
4304 static void
4305 print_usage (void)
4306 {
4307 printf ("Usage: %s\n", progname);
4308 printf ("\t -h | --help " " \t# Give this help.\n");
4309 printf ("\t -D | --debug "
4310 " \t# Give debug output to debug %s itself.\n", progname);
4311 printf ("\t -V | --version " " \t# Give version information.\n");
4312 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4313 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4314 " \t# Generate for plugin.\n");
4315 printf ("\t -S | --srcdir <GCC-directory> "
4316 " \t# Specify the GCC source directory.\n");
4317 printf ("\t -I | --inputs <input-list> "
4318 " \t# Specify the file with source files list.\n");
4319 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
4320 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
4321 }
4322
4323 static void
4324 print_version (void)
4325 {
4326 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
4327 printf ("Report bugs: %s\n", bug_report_url);
4328 }
4329
4330 /* Parse the program options using getopt_long... */
4331 static void
4332 parse_program_options (int argc, char **argv)
4333 {
4334 int opt = -1;
4335 while ((opt = getopt_long (argc, argv, "hVdP:S:I:w:r:D",
4336 gengtype_long_options, NULL)) >= 0)
4337 {
4338 switch (opt)
4339 {
4340 case 'h': /* --help */
4341 print_usage ();
4342 break;
4343 case 'V': /* --version */
4344 print_version ();
4345 break;
4346 case 'd': /* --dump */
4347 do_dump = 1;
4348 break;
4349 case 'D': /* --debug */
4350 do_debug = 1;
4351 break;
4352 case 'P': /* --plugin */
4353 if (optarg)
4354 plugin_output_filename = optarg;
4355 else
4356 fatal ("missing plugin output file name");
4357 break;
4358 case 'S': /* --srcdir */
4359 if (optarg)
4360 srcdir = optarg;
4361 else
4362 fatal ("missing source directory");
4363 srcdir_len = strlen (srcdir);
4364 break;
4365 case 'I': /* --inputs */
4366 if (optarg)
4367 inputlist = optarg;
4368 else
4369 fatal ("missing input list");
4370 break;
4371 case 'r': /* --read-state */
4372 if (optarg)
4373 read_state_filename = optarg;
4374 else
4375 fatal ("missing read state file");
4376 DBGPRINTF ("read state %s\n", optarg);
4377 break;
4378 case 'w': /* --write-state */
4379 DBGPRINTF ("write state %s\n", optarg);
4380 if (optarg)
4381 write_state_filename = optarg;
4382 else
4383 fatal ("missing write state file");
4384 break;
4385 default:
4386 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
4387 print_usage ();
4388 fatal ("unexpected flag");
4389 }
4390 };
4391 if (plugin_output_filename)
4392 {
4393 /* In plugin mode we require some input files. */
4394 int i = 0;
4395 if (optind >= argc)
4396 fatal ("no source files given in plugin mode");
4397 nb_plugin_files = argc - optind;
4398 for (i = 0; i < (int) nb_plugin_files; i++)
4399 {
4400 char *name = argv[i + optind];
4401 plugin_files[i] = name;
4402 }
4403 }
4404 }
4405
4406
4407 int
4408 main (int argc, char **argv)
4409 {
4410 size_t i;
4411 static struct fileloc pos = { NULL, 0 };
4412 outf_p output_header;
4413
4414 /* Mandatory common initializations. */
4415 progname = "gengtype"; /* For fatal and messages. */
4416 /* Set the scalar_is_char union number for predefined scalar types. */
4417 scalar_nonchar.u.scalar_is_char = FALSE;
4418 scalar_char.u.scalar_is_char = TRUE;
4419
4420 parse_program_options (argc, argv);
4421
4422 #if ENABLE_CHECKING
4423 if (do_debug)
4424 {
4425 time_t now = (time_t) 0;
4426 time (&now);
4427 DBGPRINTF ("gengtype started pid %d at %s",
4428 (int) getpid (), ctime (&now));
4429 }
4430 #endif /* ENABLE_CHECKING */
4431
4432 /* Parse the input list and the input files. */
4433 DBGPRINTF ("inputlist %s", inputlist);
4434 if (read_state_filename)
4435 {
4436 fatal ("read state %s not implemented yet", read_state_filename);
4437 /* TODO: implement read state. */
4438 }
4439 else if (inputlist)
4440 {
4441 /* These types are set up with #define or else outside of where
4442 we can see them. We should initialize them before calling
4443 read_input_list. */
4444 pos.file = this_file;
4445 pos.line = __LINE__ + 1;
4446 do_scalar_typedef ("CUMULATIVE_ARGS", &pos);
4447 pos.line++;
4448 do_scalar_typedef ("REAL_VALUE_TYPE", &pos);
4449 pos.line++;
4450 do_scalar_typedef ("FIXED_VALUE_TYPE", &pos);
4451 pos.line++;
4452 do_scalar_typedef ("double_int", &pos);
4453 pos.line++;
4454 do_scalar_typedef ("uint64_t", &pos);
4455 pos.line++;
4456 do_scalar_typedef ("uint8", &pos);
4457 pos.line++;
4458 do_scalar_typedef ("jword", &pos);
4459 pos.line++;
4460 do_scalar_typedef ("JCF_u2", &pos);
4461 pos.line++;
4462 do_scalar_typedef ("void", &pos);
4463 pos.line++;
4464 do_typedef ("PTR", create_pointer (resolve_typedef ("void", &pos)),
4465 &pos);
4466 read_input_list (inputlist);
4467 for (i = 0; i < num_gt_files; i++)
4468 {
4469 parse_file (gt_files[i]);
4470 DBGPRINTF ("parsed file #%d %s", (int) i, gt_files[i]);
4471 }
4472 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
4473 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
4474
4475 }
4476 else
4477 fatal ("either an input list or a read state file should be given");
4478 if (hit_error)
4479 return 1;
4480
4481
4482 if (plugin_output_filename)
4483 {
4484 size_t ix = 0;
4485 /* In plugin mode, we should have read a state file, and have
4486 given at least one plugin file. */
4487 if (!read_state_filename)
4488 fatal ("No read state given in plugin mode for %s",
4489 plugin_output_filename);
4490
4491 if (nb_plugin_files <= 0 || !plugin_files)
4492 fatal ("No plugin files given in plugin mode for %s",
4493 plugin_output_filename);
4494
4495 /* Parse our plugin files. */
4496 for (ix = 0; ix < nb_plugin_files; ix++)
4497 parse_file (plugin_files[ix]);
4498
4499 if (hit_error)
4500 return 1;
4501
4502 plugin_output = create_file ("GCC", plugin_output_filename);
4503 DBGPRINTF ("created plugin_output %p named %s",
4504 (void *) plugin_output, plugin_output->name);
4505 }
4506 else
4507 { /* No plugin files, we are in normal mode. */
4508 if (!srcdir)
4509 fatal ("gengtype needs a source directory in normal mode");
4510 }
4511 if (hit_error)
4512 return 1;
4513
4514 gen_rtx_next ();
4515
4516 /* The call to set_gc_used may indirectly call find_param_structure
4517 hence enlarge the param_structs list of types. */
4518 set_gc_used (variables);
4519
4520 /* We should write the state here, but it is not yet implemented. */
4521 if (write_state_filename)
4522 {
4523 fatal ("write state %s in not yet implemented", write_state_filename);
4524 /* TODO: implement write state. */
4525 }
4526
4527
4528 open_base_files ();
4529
4530 write_enum_defn (structures, param_structs);
4531 write_typed_alloc_defns (structures, typedefs);
4532 output_header = plugin_output ? plugin_output : header_file;
4533 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
4534 structures);
4535 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
4536 param_structs);
4537
4538 write_types (output_header, structures, param_structs, &ggc_wtd);
4539 if (plugin_files == NULL)
4540 {
4541 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
4542 structures);
4543 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
4544 param_structs);
4545 write_types (header_file, structures, param_structs, &pch_wtd);
4546 write_local (header_file, structures, param_structs);
4547 }
4548 write_splay_tree_allocators (param_structs);
4549 write_roots (variables, plugin_files == NULL);
4550 write_rtx_next ();
4551 close_output_files ();
4552
4553 if (do_dump)
4554 dump_everything ();
4555
4556 /* Don't bother about free-ing any input or plugin file, etc. */
4557
4558 if (hit_error)
4559 return 1;
4560 return 0;
4561 }