1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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
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/>. */
27 #include "errors.h" /* for fatal */
29 #include "double-int.h"
30 #include "version.h" /* for version_string & pkgversion_string. */
35 #include "filenames.h"
37 /* Data types, macros, etc. used only in this file. */
40 /* The list of output files. */
43 /* The output header file that is included into pretty much every
48 /* The name of the file containing the list of input files. */
49 static char *inputlist
;
51 /* The plugin input files and their number; in that case only
52 a single file is produced. */
53 static input_file
**plugin_files
;
54 static size_t nb_plugin_files
;
56 /* The generated plugin output file and name. */
57 static outf_p plugin_output
;
58 static char *plugin_output_filename
;
60 /* Our source directory and its length. */
64 /* Variables used for reading and writing the state. */
65 const char *read_state_filename
;
66 const char *write_state_filename
;
68 /* Variables to help debugging. */
72 /* Level for verbose messages. */
75 /* We have a type count and use it to set the state_number of newly
76 allocated types to some unique negative number. */
77 static int type_count
;
79 /* The backup directory should be in the same file system as the
80 generated files, otherwise the rename(2) system call would fail.
81 If NULL, no backup is made when overwriting a generated file. */
82 static const char* backup_dir
; /* (-B) program option. */
85 static outf_p
create_file (const char *, const char *);
87 static const char *get_file_basename (const input_file
*);
88 static const char *get_file_realbasename (const input_file
*);
90 static int get_prefix_langdir_index (const char *);
91 static const char *get_file_langdir (const input_file
*);
94 /* Nonzero iff an error has occurred. */
95 bool hit_error
= false;
97 static void gen_rtx_next (void);
98 static void write_rtx_next (void);
99 static void open_base_files (void);
100 static void close_output_files (void);
102 /* Report an error at POS, printing MSG. */
105 error_at_line (const struct fileloc
*pos
, const char *msg
, ...)
109 gcc_assert (pos
!= NULL
&& pos
->file
!= NULL
);
112 fprintf (stderr
, "%s:%d: ", get_input_file_name (pos
->file
), pos
->line
);
113 vfprintf (stderr
, msg
, ap
);
114 fputc ('\n', stderr
);
120 /* asprintf, but produces fatal message on out-of-memory. */
122 xasprintf (const char *format
, ...)
128 va_start (ap
, format
);
129 n
= vasprintf (&result
, format
, ap
);
130 if (result
== NULL
|| n
< 0)
131 fatal ("out of memory");
137 /* Input file handling. */
139 /* Table of all input files. */
140 const input_file
**gt_files
;
143 /* A number of places use the name of this "gengtype.c" file for a
144 location for things that we can't rely on the source to define.
145 Make sure we can still use pointer comparison on filenames. */
146 input_file
* this_file
;
147 /* The "system.h" file is likewise specially useful. */
148 input_file
* system_h_file
;
150 /* Vector of per-language directories. */
151 const char **lang_dir_names
;
152 size_t num_lang_dirs
;
154 /* An array of output files suitable for definitions. There is one
155 BASE_FILES entry for each language. */
156 static outf_p
*base_files
;
161 /* Utility debugging function, printing the various type counts within
162 a list of types. Called thru the DBGPRINT_COUNT_TYPE macro. */
164 dbgprint_count_type_at (const char *fil
, int lin
, const char *msg
, type_p t
)
166 int nb_types
= 0, nb_scalar
= 0, nb_string
= 0;
167 int nb_struct
= 0, nb_union
= 0, nb_array
= 0, nb_pointer
= 0;
168 int nb_lang_struct
= 0, nb_param_struct
= 0;
170 for (p
= t
; p
; p
= p
->next
)
193 case TYPE_LANG_STRUCT
:
196 case TYPE_PARAM_STRUCT
:
203 fprintf (stderr
, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
204 lbasename (fil
), lin
, msg
, nb_types
);
205 if (nb_scalar
> 0 || nb_string
> 0)
206 fprintf (stderr
, "@@%%@@ %d scalars, %d strings\n", nb_scalar
, nb_string
);
207 if (nb_struct
> 0 || nb_union
> 0)
208 fprintf (stderr
, "@@%%@@ %d structs, %d unions\n", nb_struct
, nb_union
);
209 if (nb_pointer
> 0 || nb_array
> 0)
210 fprintf (stderr
, "@@%%@@ %d pointers, %d arrays\n", nb_pointer
, nb_array
);
211 if (nb_lang_struct
> 0 || nb_param_struct
> 0)
212 fprintf (stderr
, "@@%%@@ %d lang_structs, %d param_structs\n",
213 nb_lang_struct
, nb_param_struct
);
214 fprintf (stderr
, "\n");
216 #endif /* ENABLE_CHECKING */
218 /* Scan the input file, LIST, and determine how much space we need to
219 store strings in. Also, count the number of language directories
220 and files. The numbers returned are overestimates as they does not
221 consider repeated files. */
223 measure_input_list (FILE *list
)
229 num_gt_files
= plugin_files
? nb_plugin_files
: 0;
230 while ((c
= getc (list
)) != EOF
)
239 /* Add space for a lang_bitmap before the input file name. */
240 n
+= sizeof (lang_bitmap
);
254 /* Read one input line from LIST to HEREP (which is updated). A
255 pointer to the string is returned via LINEP. If it was a language
256 subdirectory in square brackets, strip off the square brackets and
257 return true. Otherwise, leave space before the string for a
258 lang_bitmap, and return false. At EOF, returns false, does not
259 touch *HEREP, and sets *LINEP to NULL. POS is used for
262 read_input_line (FILE *list
, char **herep
, char **linep
, struct fileloc
*pos
)
268 /* Read over whitespace. */
269 while (c
== '\n' || c
== ' ')
279 /* No space for a lang_bitmap is necessary. Discard the '['. */
282 while (c
!= ']' && c
!= '\n' && c
!= EOF
)
291 c
= getc (list
); /* eat what should be a newline */
292 if (c
!= '\n' && c
!= EOF
)
293 error_at_line (pos
, "junk on line after language tag [%s]", line
);
296 error_at_line (pos
, "missing close bracket for language tag [%s",
305 /* Leave space for a lang_bitmap. */
306 memset (here
, 0, sizeof (lang_bitmap
));
307 here
+= sizeof (lang_bitmap
);
314 while (c
!= EOF
&& c
!= '\n');
322 /* Read the list of input files from LIST and compute all of the
323 relevant tables. There is one file per line of the list. At
324 first, all the files on the list are language-generic, but
325 eventually a line will appear which is the name of a language
326 subdirectory in square brackets, like this: [cp]. All subsequent
327 files are specific to that language, until another language
328 subdirectory tag appears. Files can appear more than once, if
329 they apply to more than one language. */
331 read_input_list (const char *listname
)
333 FILE *list
= fopen (listname
, "r");
335 fatal ("cannot open %s: %s", listname
, xstrerror (errno
));
339 size_t bufsz
= measure_input_list (list
);
340 char *buf
= XNEWVEC (char, bufsz
);
342 char *committed
= buf
;
343 char *limit
= buf
+ bufsz
;
348 lang_bitmap curlangs
= (1 << num_lang_dirs
) - 1;
350 epos
.file
= input_file_by_name (listname
);
353 lang_dir_names
= XNEWVEC (const char *, num_lang_dirs
);
354 gt_files
= XNEWVEC (const input_file
*, num_gt_files
);
361 is_language
= read_input_line (list
, &here
, &line
, &epos
);
362 gcc_assert (here
<= limit
);
365 else if (is_language
)
368 gcc_assert (langno
<= num_lang_dirs
);
369 for (i
= 0; i
< langno
; i
++)
370 if (strcmp (lang_dir_names
[i
], line
) == 0)
372 error_at_line (&epos
, "duplicate language tag [%s]",
379 curlangs
= 1 << langno
;
380 lang_dir_names
[langno
++] = line
;
385 input_file
*inpf
= input_file_by_name (line
);
386 gcc_assert (nfiles
<= num_gt_files
);
387 for (i
= 0; i
< nfiles
; i
++)
388 /* Since the input_file-s are uniquely hash-consed, we
389 can just compare pointers! */
390 if (gt_files
[i
] == inpf
)
392 /* Throw away the string we just read, and add the
393 current language to the existing string's bitmap. */
394 lang_bitmap bmap
= get_lang_bitmap (inpf
);
396 error_at_line (&epos
,
397 "file %s specified more than once "
398 "for language %s", line
,
400 0 ? "(all)" : lang_dir_names
[langno
-
404 set_lang_bitmap (inpf
, bmap
);
409 set_lang_bitmap (inpf
, curlangs
);
410 gt_files
[nfiles
++] = inpf
;
413 /* Update the global counts now that we know accurately how many
414 things there are. (We do not bother resizing the arrays down.) */
415 num_lang_dirs
= langno
;
416 /* Add the plugin files if provided. */
420 for (i
= 0; i
< nb_plugin_files
; i
++)
421 gt_files
[nfiles
++] = plugin_files
[i
];
423 num_gt_files
= nfiles
;
426 /* Sanity check: any file that resides in a language subdirectory
427 (e.g. 'cp') ought to belong to the corresponding language.
428 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
429 (Can you even do that? Should you be allowed to?) */
432 for (f
= 0; f
< num_gt_files
; f
++)
434 lang_bitmap bitmap
= get_lang_bitmap (gt_files
[f
]);
435 const char *basename
= get_file_basename (gt_files
[f
]);
436 const char *slashpos
= strchr (basename
, '/');
437 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
438 const char *slashpos2
= strchr (basename
, '\\');
440 if (!slashpos
|| (slashpos2
&& slashpos2
< slashpos
))
441 slashpos
= slashpos2
;
447 for (l
= 0; l
< num_lang_dirs
; l
++)
448 if ((size_t) (slashpos
- basename
) == strlen (lang_dir_names
[l
])
449 && memcmp (basename
, lang_dir_names
[l
],
450 strlen (lang_dir_names
[l
])) == 0)
452 if (!(bitmap
& (1 << l
)))
453 error ("%s is in language directory '%s' but is not "
454 "tagged for that language",
455 basename
, lang_dir_names
[l
]);
463 fatal ("error reading %s: %s", listname
, xstrerror (errno
));
470 /* The one and only TYPE_STRING. */
472 struct type string_type
= {
473 TYPE_STRING
, 0, 0, 0, GC_USED
, {0}
476 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
477 set early in main. */
479 struct type scalar_nonchar
= {
480 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
483 struct type scalar_char
= {
484 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
487 /* Lists of various things. */
491 type_p param_structs
;
494 static type_p
find_param_structure (type_p t
, type_p param
[NUM_PARAM
]);
495 static type_p
adjust_field_tree_exp (type_p t
, options_p opt
);
496 static type_p
adjust_field_rtx_def (type_p t
, options_p opt
);
498 /* Define S as a typedef to T at POS. */
501 do_typedef (const char *s
, type_p t
, struct fileloc
*pos
)
505 /* temporary kludge - gengtype doesn't handle conditionals or
506 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
507 is coming from this file (main() sets them up with safe dummy
509 if (!strcmp (s
, "CUMULATIVE_ARGS") && pos
->file
!= this_file
)
512 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
513 if (strcmp (p
->name
, s
) == 0)
517 error_at_line (pos
, "type `%s' previously defined", s
);
518 error_at_line (&p
->line
, "previously defined here");
523 p
= XNEW (struct pair
);
532 /* Define S as a typename of a scalar. Cannot be used to define
533 typedefs of 'char'. Note: is also used for pointer-to-function
534 typedefs (which are therefore not treated as pointers). */
537 do_scalar_typedef (const char *s
, struct fileloc
*pos
)
539 do_typedef (s
, &scalar_nonchar
, pos
);
542 /* Return the type previously defined for S. Use POS to report errors. */
545 resolve_typedef (const char *s
, struct fileloc
*pos
)
548 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
549 if (strcmp (p
->name
, s
) == 0)
551 error_at_line (pos
, "unidentified type `%s'", s
);
552 return &scalar_nonchar
; /* treat as "int" */
555 /* Create and return a new structure with tag NAME (or a union iff
556 ISUNION is nonzero), at POS with fields FIELDS and options O. */
559 new_structure (const char *name
, int isunion
, struct fileloc
*pos
,
560 pair_p fields
, options_p o
)
564 lang_bitmap bitmap
= get_lang_bitmap (pos
->file
);
566 for (si
= structures
; si
!= NULL
; si
= si
->next
)
567 if (strcmp (name
, si
->u
.s
.tag
) == 0 && UNION_P (si
) == isunion
)
570 if (si
->kind
== TYPE_LANG_STRUCT
)
574 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
575 if (si
->u
.s
.bitmap
== bitmap
)
578 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
582 si
= XCNEW (struct type
);
583 memcpy (si
, ls
, sizeof (struct type
));
584 ls
->kind
= TYPE_LANG_STRUCT
;
585 ls
->u
.s
.lang_struct
= si
;
586 ls
->u
.s
.fields
= NULL
;
588 si
->state_number
= -type_count
;
589 si
->pointer_to
= NULL
;
590 si
->u
.s
.lang_struct
= ls
;
595 if (ls
!= NULL
&& s
== NULL
)
598 s
= XCNEW (struct type
);
599 s
->state_number
= -type_count
;
600 s
->next
= ls
->u
.s
.lang_struct
;
601 ls
->u
.s
.lang_struct
= s
;
602 s
->u
.s
.lang_struct
= ls
;
610 s
= XCNEW (struct type
);
611 s
->state_number
= -type_count
;
612 s
->next
= structures
;
616 if (s
->u
.s
.line
.file
!= NULL
617 || (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
)))
619 error_at_line (pos
, "duplicate definition of '%s %s'",
620 isunion
? "union" : "struct", s
->u
.s
.tag
);
621 error_at_line (&s
->u
.s
.line
, "previous definition here");
624 s
->kind
= isunion
? TYPE_UNION
: TYPE_STRUCT
;
627 s
->u
.s
.fields
= fields
;
629 s
->u
.s
.bitmap
= bitmap
;
630 if (s
->u
.s
.lang_struct
)
631 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
636 /* Return the previously-defined structure with tag NAME (or a union
637 iff ISUNION is nonzero), or a new empty structure or union if none
638 was defined previously. */
641 find_structure (const char *name
, int isunion
)
645 for (s
= structures
; s
!= NULL
; s
= s
->next
)
646 if (strcmp (name
, s
->u
.s
.tag
) == 0 && UNION_P (s
) == isunion
)
650 s
= XCNEW (struct type
);
651 s
->next
= structures
;
652 s
->state_number
= -type_count
;
654 s
->kind
= isunion
? TYPE_UNION
: TYPE_STRUCT
;
660 /* Return the previously-defined parameterized structure for structure
661 T and parameters PARAM, or a new parameterized empty structure or
662 union if none was defined previously. */
665 find_param_structure (type_p t
, type_p param
[NUM_PARAM
])
669 for (res
= param_structs
; res
; res
= res
->next
)
670 if (res
->u
.param_struct
.stru
== t
671 && memcmp (res
->u
.param_struct
.param
, param
,
672 sizeof (type_p
) * NUM_PARAM
) == 0)
677 res
= XCNEW (struct type
);
678 res
->kind
= TYPE_PARAM_STRUCT
;
679 res
->next
= param_structs
;
680 res
->state_number
= -type_count
;
682 res
->u
.param_struct
.stru
= t
;
683 memcpy (res
->u
.param_struct
.param
, param
, sizeof (type_p
) * NUM_PARAM
);
688 /* Return a scalar type with name NAME. */
691 create_scalar_type (const char *name
)
693 if (!strcmp (name
, "char") || !strcmp (name
, "unsigned char"))
696 return &scalar_nonchar
;
699 /* Return a pointer to T. */
702 create_pointer (type_p t
)
706 type_p r
= XCNEW (struct type
);
708 r
->state_number
= -type_count
;
709 r
->kind
= TYPE_POINTER
;
713 return t
->pointer_to
;
716 /* Return an array of length LEN. */
719 create_array (type_p t
, const char *len
)
724 v
= XCNEW (struct type
);
725 v
->kind
= TYPE_ARRAY
;
726 v
->state_number
= -type_count
;
732 /* Return a string options structure with name NAME and info INFO.
733 NEXT is the next option in the chain. */
735 create_string_option (options_p next
, const char *name
, const char *info
)
737 options_p o
= XNEW (struct options
);
738 o
->kind
= OPTION_STRING
;
741 o
->info
.string
= info
;
745 /* Create a type options structure with name NAME and info INFO. NEXT
746 is the next option in the chain. */
748 create_type_option (options_p next
, const char* name
, type_p info
)
750 options_p o
= XNEW (struct options
);
753 o
->kind
= OPTION_TYPE
;
758 /* Create a nested pointer options structure with name NAME and info
759 INFO. NEXT is the next option in the chain. */
761 create_nested_option (options_p next
, const char* name
,
762 struct nested_ptr_data
* info
)
765 o
= XNEW (struct options
);
768 o
->kind
= OPTION_NESTED
;
769 o
->info
.nested
= info
;
773 /* Return an options structure for a "nested_ptr" option. */
775 create_nested_ptr_option (options_p next
, type_p t
,
776 const char *to
, const char *from
)
778 struct nested_ptr_data
*d
= XNEW (struct nested_ptr_data
);
780 d
->type
= adjust_field_type (t
, 0);
782 d
->convert_from
= from
;
783 return create_nested_option (next
, "nested_ptr", d
);
786 /* Add a variable named S of type T with options O defined at POS,
789 note_variable (const char *s
, type_p t
, options_p o
, struct fileloc
*pos
)
792 n
= XNEW (struct pair
);
801 /* Most-general structure field creator. */
803 create_field_all (pair_p next
, type_p type
, const char *name
, options_p opt
,
804 const input_file
*inpf
, int line
)
808 field
= XNEW (struct pair
);
813 field
->line
.file
= inpf
;
814 field
->line
.line
= line
;
818 /* Create a field that came from the source code we are scanning,
819 i.e. we have a 'struct fileloc', and possibly options; also,
820 adjust_field_type should be called. */
822 create_field_at (pair_p next
, type_p type
, const char *name
, options_p opt
,
825 return create_field_all (next
, adjust_field_type (type
, opt
),
826 name
, opt
, pos
->file
, pos
->line
);
829 /* Create a fake field with the given type and name. NEXT is the next
830 field in the chain. */
831 #define create_field(next,type,name) \
832 create_field_all(next,type,name, 0, this_file, __LINE__)
834 /* Like create_field, but the field is only valid when condition COND
838 create_optional_field_ (pair_p next
, type_p type
, const char *name
,
839 const char *cond
, int line
)
845 /* Create a fake union type with a single nameless field of type TYPE.
846 The field has a tag of "1". This allows us to make the presence
847 of a field of type TYPE depend on some boolean "desc" being true. */
848 union_fields
= create_field (NULL
, type
, "");
850 create_string_option (union_fields
->opt
, "dot", "");
852 create_string_option (union_fields
->opt
, "tag", "1");
854 new_structure (xasprintf ("%s_%d", "fake_union", id
++), 1,
855 &lexer_line
, union_fields
, NULL
);
857 /* Create the field and give it the new fake union type. Add a "desc"
858 tag that specifies the condition under which the field is valid. */
859 return create_field_all (next
, union_type
, name
,
860 create_string_option (0, "desc", cond
),
864 #define create_optional_field(next,type,name,cond) \
865 create_optional_field_(next,type,name,cond,__LINE__)
867 /* Reverse a linked list of 'struct pair's in place. */
869 nreverse_pairs (pair_p list
)
871 pair_p prev
= 0, p
, next
;
872 for (p
= list
; p
; p
= next
)
882 /* We don't care how long a CONST_DOUBLE is. */
883 #define CONST_DOUBLE_FORMAT "ww"
884 /* We don't want to see codes that are only for generator files. */
885 #undef GENERATOR_FILE
889 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
895 static const char *const rtx_name
[NUM_RTX_CODE
] = {
896 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
901 static const char *const rtx_format
[NUM_RTX_CODE
] = {
902 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
907 static int rtx_next_new
[NUM_RTX_CODE
];
909 /* We also need codes and names for insn notes (not register notes).
910 Note that we do *not* bias the note values here. */
913 #define DEF_INSN_NOTE(NAME) NAME,
914 #include "insn-notes.def"
920 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
921 default field for line number notes. */
922 static const char *const note_insn_name
[NOTE_INSN_MAX
+ 1] = {
923 #define DEF_INSN_NOTE(NAME) #NAME,
924 #include "insn-notes.def"
928 #undef CONST_DOUBLE_FORMAT
929 #define GENERATOR_FILE
931 /* Generate the contents of the rtx_next array. This really doesn't belong
932 in gengtype at all, but it's needed for adjust_field_rtx_def. */
938 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
942 rtx_next_new
[i
] = -1;
943 if (strncmp (rtx_format
[i
], "iuu", 3) == 0)
945 else if (i
== COND_EXEC
|| i
== SET
|| i
== EXPR_LIST
|| i
== INSN_LIST
)
948 for (k
= strlen (rtx_format
[i
]) - 1; k
>= 0; k
--)
949 if (rtx_format
[i
][k
] == 'e' || rtx_format
[i
][k
] == 'u')
954 /* Write out the contents of the rtx_next array. */
956 write_rtx_next (void)
958 outf_p f
= get_output_file_with_visibility (NULL
);
963 oprintf (f
, "\n/* Used to implement the RTX_NEXT macro. */\n");
964 oprintf (f
, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
965 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
966 if (rtx_next_new
[i
] == -1)
967 oprintf (f
, " 0,\n");
970 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new
[i
]);
974 /* Handle `special("rtx_def")'. This is a special case for field
975 `fld' of struct rtx_def, which is an array of unions whose values
976 are based in a complex way on the type of RTL. */
979 adjust_field_rtx_def (type_p t
, options_p
ARG_UNUSED (opt
))
984 type_p rtx_tp
, rtvec_tp
, tree_tp
, mem_attrs_tp
, note_union_tp
, scalar_tp
;
985 type_p basic_block_tp
, reg_attrs_tp
, constant_tp
, symbol_union_tp
;
987 if (t
->kind
!= TYPE_UNION
)
989 error_at_line (&lexer_line
,
990 "special `rtx_def' must be applied to a union");
994 nodot
= create_string_option (NULL
, "dot", "");
996 rtx_tp
= create_pointer (find_structure ("rtx_def", 0));
997 rtvec_tp
= create_pointer (find_structure ("rtvec_def", 0));
998 tree_tp
= create_pointer (find_structure ("tree_node", 1));
999 mem_attrs_tp
= create_pointer (find_structure ("mem_attrs", 0));
1001 create_pointer (find_structure ("reg_attrs", 0));
1003 create_pointer (find_structure ("basic_block_def", 0));
1005 create_pointer (find_structure ("constant_descriptor_rtx", 0));
1006 scalar_tp
= &scalar_nonchar
; /* rtunion int */
1009 pair_p note_flds
= NULL
;
1012 for (c
= 0; c
<= NOTE_INSN_MAX
; c
++)
1017 case NOTE_INSN_DELETED_LABEL
:
1018 note_flds
= create_field (note_flds
, &string_type
, "rt_str");
1021 case NOTE_INSN_BLOCK_BEG
:
1022 case NOTE_INSN_BLOCK_END
:
1023 note_flds
= create_field (note_flds
, tree_tp
, "rt_tree");
1026 case NOTE_INSN_VAR_LOCATION
:
1027 case NOTE_INSN_CALL_ARG_LOCATION
:
1028 note_flds
= create_field (note_flds
, rtx_tp
, "rt_rtx");
1032 note_flds
= create_field (note_flds
, scalar_tp
, "rt_int");
1035 /* NOTE_INSN_MAX is used as the default field for line
1037 if (c
== NOTE_INSN_MAX
)
1039 create_string_option (nodot
, "default", "");
1042 create_string_option (nodot
, "tag", note_insn_name
[c
]);
1044 note_union_tp
= new_structure ("rtx_def_note_subunion", 1,
1045 &lexer_line
, note_flds
, NULL
);
1047 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1050 sym_flds
= create_field (NULL
, tree_tp
, "rt_tree");
1051 sym_flds
->opt
= create_string_option (nodot
, "default", "");
1052 sym_flds
= create_field (sym_flds
, constant_tp
, "rt_constant");
1053 sym_flds
->opt
= create_string_option (nodot
, "tag", "1");
1054 symbol_union_tp
= new_structure ("rtx_def_symbol_subunion", 1,
1055 &lexer_line
, sym_flds
, NULL
);
1057 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1059 pair_p subfields
= NULL
;
1060 size_t aindex
, nmindex
;
1065 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
1068 const char *subname
;
1070 switch (rtx_format
[i
][aindex
])
1081 if (i
== MEM
&& aindex
== 1)
1082 t
= mem_attrs_tp
, subname
= "rt_mem";
1083 else if (i
== JUMP_INSN
&& aindex
== 8)
1084 t
= rtx_tp
, subname
= "rt_rtx";
1085 else if (i
== CODE_LABEL
&& aindex
== 5)
1086 t
= scalar_tp
, subname
= "rt_int";
1087 else if (i
== CODE_LABEL
&& aindex
== 4)
1088 t
= rtx_tp
, subname
= "rt_rtx";
1089 else if (i
== LABEL_REF
&& (aindex
== 1 || aindex
== 2))
1090 t
= rtx_tp
, subname
= "rt_rtx";
1091 else if (i
== NOTE
&& aindex
== 4)
1092 t
= note_union_tp
, subname
= "";
1093 else if (i
== NOTE
&& aindex
== 5)
1094 t
= scalar_tp
, subname
= "rt_int";
1095 else if (i
== NOTE
&& aindex
>= 7)
1096 t
= scalar_tp
, subname
= "rt_int";
1097 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
1098 t
= scalar_tp
, subname
= "rt_int";
1099 else if (i
== VALUE
&& aindex
== 0)
1100 t
= scalar_tp
, subname
= "rt_int";
1101 else if (i
== DEBUG_EXPR
&& aindex
== 0)
1102 t
= tree_tp
, subname
= "rt_tree";
1103 else if (i
== REG
&& aindex
== 1)
1104 t
= scalar_tp
, subname
= "rt_int";
1105 else if (i
== REG
&& aindex
== 2)
1106 t
= reg_attrs_tp
, subname
= "rt_reg";
1107 else if (i
== SCRATCH
&& aindex
== 0)
1108 t
= scalar_tp
, subname
= "rt_int";
1109 else if (i
== SYMBOL_REF
&& aindex
== 1)
1110 t
= scalar_tp
, subname
= "rt_int";
1111 else if (i
== SYMBOL_REF
&& aindex
== 2)
1112 t
= symbol_union_tp
, subname
= "";
1113 else if (i
== BARRIER
&& aindex
>= 3)
1114 t
= scalar_tp
, subname
= "rt_int";
1115 else if (i
== ENTRY_VALUE
&& aindex
== 0)
1116 t
= rtx_tp
, subname
= "rt_rtx";
1121 "rtx type `%s' has `0' in position %lu, can't handle",
1122 rtx_name
[i
], (unsigned long) aindex
);
1144 subname
= "rt_rtvec";
1149 subname
= "rt_tree";
1160 "rtx type `%s' has `%c' in position %lu, can't handle",
1161 rtx_name
[i
], rtx_format
[i
][aindex
],
1162 (unsigned long) aindex
);
1168 subfields
= create_field (subfields
, t
,
1169 xasprintf (".fld[%lu].%s",
1170 (unsigned long) aindex
,
1172 subfields
->opt
= nodot
;
1173 if (t
== note_union_tp
)
1175 create_string_option (subfields
->opt
, "desc",
1177 if (t
== symbol_union_tp
)
1179 create_string_option (subfields
->opt
, "desc",
1180 "CONSTANT_POOL_ADDRESS_P (&%0)");
1183 if (i
== SYMBOL_REF
)
1185 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1187 type_p field_tp
= find_structure ("block_symbol", 0);
1189 = create_optional_field (subfields
, field_tp
, "block_sym",
1190 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1193 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
1194 substruct
= new_structure (sname
, 0, &lexer_line
, subfields
, NULL
);
1196 ftag
= xstrdup (rtx_name
[i
]);
1197 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
1198 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
1199 flds
= create_field (flds
, substruct
, "");
1200 flds
->opt
= create_string_option (nodot
, "tag", ftag
);
1202 return new_structure ("rtx_def_subunion", 1, &lexer_line
, flds
, nodot
);
1205 /* Handle `special("tree_exp")'. This is a special case for
1206 field `operands' of struct tree_exp, which although it claims to contain
1207 pointers to trees, actually sometimes contains pointers to RTL too.
1208 Passed T, the old type of the field, and OPT its options. Returns
1209 a new type for the field. */
1212 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
1217 if (t
->kind
!= TYPE_ARRAY
)
1219 error_at_line (&lexer_line
,
1220 "special `tree_exp' must be applied to an array");
1221 return &string_type
;
1224 nodot
= create_string_option (NULL
, "dot", "");
1226 flds
= create_field (NULL
, t
, "");
1227 flds
->opt
= create_string_option (nodot
, "length",
1228 "TREE_OPERAND_LENGTH ((tree) &%0)");
1229 flds
->opt
= create_string_option (flds
->opt
, "default", "");
1231 return new_structure ("tree_exp_subunion", 1, &lexer_line
, flds
, nodot
);
1234 /* Perform any special processing on a type T, about to become the type
1235 of a field. Return the appropriate type for the field.
1237 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1238 - Similarly for arrays of pointer-to-char;
1239 - Converts structures for which a parameter is provided to
1241 - Handles "special" options.
1245 adjust_field_type (type_p t
, options_p opt
)
1248 const int pointer_p
= t
->kind
== TYPE_POINTER
;
1249 type_p params
[NUM_PARAM
];
1253 for (i
= 0; i
< NUM_PARAM
; i
++)
1256 for (; opt
; opt
= opt
->next
)
1257 if (strcmp (opt
->name
, "length") == 0)
1259 else if ((strcmp (opt
->name
, "param_is") == 0
1260 || (strncmp (opt
->name
, "param", 5) == 0
1261 && ISDIGIT (opt
->name
[5])
1262 && strcmp (opt
->name
+ 6, "_is") == 0))
1263 && opt
->kind
== OPTION_TYPE
)
1265 int num
= ISDIGIT (opt
->name
[5]) ? opt
->name
[5] - '0' : 0;
1267 if (!UNION_OR_STRUCT_P (t
)
1268 && (t
->kind
!= TYPE_POINTER
|| !UNION_OR_STRUCT_P (t
->u
.p
)))
1270 error_at_line (&lexer_line
,
1271 "option `%s' may only be applied to structures or structure pointers",
1277 if (params
[num
] != NULL
)
1278 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1279 if (!ISDIGIT (opt
->name
[5]))
1280 params
[num
] = create_pointer (opt
->info
.type
);
1282 params
[num
] = opt
->info
.type
;
1284 else if (strcmp (opt
->name
, "special") == 0
1285 && opt
->kind
== OPTION_STRING
)
1287 const char *special_name
= opt
->info
.string
;
1288 if (strcmp (special_name
, "tree_exp") == 0)
1289 t
= adjust_field_tree_exp (t
, opt
);
1290 else if (strcmp (special_name
, "rtx_def") == 0)
1291 t
= adjust_field_rtx_def (t
, opt
);
1293 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
1302 realt
= find_param_structure (t
, params
);
1303 t
= pointer_p
? create_pointer (realt
) : realt
;
1307 && pointer_p
&& t
->u
.p
->kind
== TYPE_SCALAR
&& t
->u
.p
->u
.scalar_is_char
)
1308 return &string_type
;
1309 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
1310 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
1311 && t
->u
.a
.p
->u
.p
->u
.scalar_is_char
)
1312 return create_array (&string_type
, t
->u
.a
.len
);
1318 static void set_gc_used_type (type_p
, enum gc_used_enum
, type_p
*);
1319 static void set_gc_used (pair_p
);
1321 /* Handle OPT for set_gc_used_type. */
1324 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
1325 int *pass_param
, int *length
, int *skip
,
1329 for (o
= opt
; o
; o
= o
->next
)
1330 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
1331 && o
->kind
== OPTION_TYPE
)
1332 set_gc_used_type (o
->info
.type
,
1333 GC_POINTED_TO
, NULL
);
1334 else if (strcmp (o
->name
, "maybe_undef") == 0)
1336 else if (strcmp (o
->name
, "use_params") == 0)
1338 else if (strcmp (o
->name
, "length") == 0)
1340 else if (strcmp (o
->name
, "skip") == 0)
1342 else if (strcmp (o
->name
, "nested_ptr") == 0
1343 && o
->kind
== OPTION_NESTED
)
1344 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
.nested
)->type
;
1348 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1350 set_gc_used_type (type_p t
, enum gc_used_enum level
, type_p param
[NUM_PARAM
])
1352 if (t
->gc_used
>= level
)
1366 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
, &dummy
,
1369 for (f
= t
->u
.s
.fields
; f
; f
= f
->next
)
1371 int maybe_undef
= 0;
1375 type_p nested_ptr
= NULL
;
1376 process_gc_options (f
->opt
, level
, &maybe_undef
, &pass_param
,
1377 &length
, &skip
, &nested_ptr
);
1379 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
1380 set_gc_used_type (nested_ptr
, GC_POINTED_TO
,
1381 pass_param
? param
: NULL
);
1382 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
1383 set_gc_used_type (f
->type
->u
.p
, GC_USED
, NULL
);
1384 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
1385 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
, NULL
);
1386 else if (pass_param
&& f
->type
->kind
== TYPE_POINTER
&& param
)
1387 set_gc_used_type (find_param_structure (f
->type
->u
.p
, param
),
1388 GC_POINTED_TO
, NULL
);
1390 ; /* target type is not used through this field */
1392 set_gc_used_type (f
->type
, GC_USED
, pass_param
? param
: NULL
);
1398 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
, NULL
);
1402 set_gc_used_type (t
->u
.a
.p
, GC_USED
, param
);
1405 case TYPE_LANG_STRUCT
:
1406 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
1407 set_gc_used_type (t
, level
, param
);
1410 case TYPE_PARAM_STRUCT
:
1413 for (i
= 0; i
< NUM_PARAM
; i
++)
1414 if (t
->u
.param_struct
.param
[i
] != 0)
1415 set_gc_used_type (t
->u
.param_struct
.param
[i
], GC_USED
, NULL
);
1417 if (t
->u
.param_struct
.stru
->gc_used
== GC_POINTED_TO
)
1418 level
= GC_POINTED_TO
;
1421 t
->u
.param_struct
.stru
->gc_used
= GC_UNUSED
;
1422 set_gc_used_type (t
->u
.param_struct
.stru
, level
,
1423 t
->u
.param_struct
.param
);
1431 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1434 set_gc_used (pair_p variables
)
1438 for (p
= variables
; p
; p
= p
->next
)
1440 set_gc_used_type (p
->type
, GC_USED
, NULL
);
1443 if (verbosity_level
>= 2)
1444 printf ("%s used %d GTY-ed variables\n", progname
, nbvars
);
1447 /* File mapping routines. For each input file, there is one output .c file
1448 (but some output files have many input files), and there is one .h file
1449 for the whole build. */
1451 /* Output file handling. */
1453 /* Create and return an outf_p for a new file for NAME, to be called
1457 create_file (const char *name
, const char *oname
)
1459 static const char *const hdr
[] = {
1460 " Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc.\n",
1462 "This file is part of GCC.\n",
1464 "GCC is free software; you can redistribute it and/or modify it under\n",
1465 "the terms of the GNU General Public License as published by the Free\n",
1466 "Software Foundation; either version 3, or (at your option) any later\n",
1469 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1470 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1471 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1472 "for more details.\n",
1474 "You should have received a copy of the GNU General Public License\n",
1475 "along with GCC; see the file COPYING3. If not see\n",
1476 "<http://www.gnu.org/licenses/>. */\n",
1478 "/* This file is machine generated. Do not edit. */\n"
1483 gcc_assert (name
!= NULL
);
1484 gcc_assert (oname
!= NULL
);
1485 f
= XCNEW (struct outf
);
1486 f
->next
= output_files
;
1490 oprintf (f
, "/* Type information for %s.\n", name
);
1491 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1492 oprintf (f
, "%s", hdr
[i
]);
1496 /* Print, like fprintf, to O.
1497 N.B. You might think this could be implemented more efficiently
1498 with vsnprintf(). Unfortunately, there are C libraries that
1499 provide that function but without the C99 semantics for its return
1500 value, making it impossible to know how much space is required. */
1502 oprintf (outf_p o
, const char *format
, ...)
1508 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1513 va_start (ap
, format
);
1514 slength
= vasprintf (&s
, format
, ap
);
1515 if (s
== NULL
|| (int) slength
< 0)
1516 fatal ("out of memory");
1519 if (o
->bufused
+ slength
> o
->buflength
)
1521 size_t new_len
= o
->buflength
;
1528 while (o
->bufused
+ slength
>= new_len
);
1529 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1530 o
->buflength
= new_len
;
1532 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1533 o
->bufused
+= slength
;
1537 /* Open the global header file and the language-specific header files. */
1540 open_base_files (void)
1544 if (nb_plugin_files
> 0 && plugin_files
)
1547 header_file
= create_file ("GCC", "gtype-desc.h");
1549 base_files
= XNEWVEC (outf_p
, num_lang_dirs
);
1551 for (i
= 0; i
< num_lang_dirs
; i
++)
1552 base_files
[i
] = create_file (lang_dir_names
[i
],
1553 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1555 /* gtype-desc.c is a little special, so we create it here. */
1557 /* The order of files here matters very much. */
1558 static const char *const ifiles
[] = {
1559 "config.h", "system.h", "coretypes.h", "tm.h",
1560 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1561 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1562 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1563 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1564 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1565 "cfglayout.h", "except.h", "output.h", "gimple.h", "cfgloop.h",
1566 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1567 "ipa-inline.h", "dwarf2out.h", NULL
1569 const char *const *ifp
;
1570 outf_p gtype_desc_c
;
1572 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1573 for (ifp
= ifiles
; *ifp
; ifp
++)
1574 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1576 /* Make sure we handle "cfun" specially. */
1577 oprintf (gtype_desc_c
, "\n/* See definition in function.h. */\n");
1578 oprintf (gtype_desc_c
, "#undef cfun\n");
1582 /* For INPF an input file, return the real basename of INPF, with all
1583 the directory components skipped. */
1586 get_file_realbasename (const input_file
*inpf
)
1588 return lbasename (get_input_file_name (inpf
));
1591 /* For INPF a filename, return the relative path to INPF from
1592 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1595 get_file_srcdir_relative_path (const input_file
*inpf
)
1597 const char *f
= get_input_file_name (inpf
);
1598 if (strlen (f
) > srcdir_len
1599 && IS_DIR_SEPARATOR (f
[srcdir_len
])
1600 && strncmp (f
, srcdir
, srcdir_len
) == 0)
1601 return f
+ srcdir_len
+ 1;
1606 /* For INPF an input_file, return the relative path to INPF from
1607 $(srcdir) if the latter is a prefix in INPF, or the real basename
1608 of INPF otherwise. */
1611 get_file_basename (const input_file
*inpf
)
1613 const char *srcdir_path
= get_file_srcdir_relative_path (inpf
);
1615 return (srcdir_path
!= NULL
) ? srcdir_path
: get_file_realbasename (inpf
);
1618 /* For F a filename, return the lang_dir_names relative index of the language
1619 directory that is a prefix in F, if any, -1 otherwise. */
1622 get_prefix_langdir_index (const char *f
)
1624 size_t f_len
= strlen (f
);
1627 for (lang_index
= 0; lang_index
< num_lang_dirs
; lang_index
++)
1629 const char *langdir
= lang_dir_names
[lang_index
];
1630 size_t langdir_len
= strlen (langdir
);
1632 if (f_len
> langdir_len
1633 && IS_DIR_SEPARATOR (f
[langdir_len
])
1634 && memcmp (f
, langdir
, langdir_len
) == 0)
1641 /* For INPF an input file, return the name of language directory where
1642 F is located, if any, NULL otherwise. */
1645 get_file_langdir (const input_file
*inpf
)
1647 /* Get the relative path to INPF from $(srcdir) and find the
1648 language by comparing the prefix with language directory names.
1649 If INPF is not even srcdir relative, no point in looking
1653 const char *srcdir_relative_path
= get_file_srcdir_relative_path (inpf
);
1656 if (!srcdir_relative_path
)
1659 lang_index
= get_prefix_langdir_index (srcdir_relative_path
);
1660 if (lang_index
< 0 && strncmp (srcdir_relative_path
, "c-family", 8) == 0)
1662 else if (lang_index
>= 0)
1663 r
= lang_dir_names
[lang_index
];
1670 /* The gt- output file name for INPF. */
1673 get_file_gtfilename (const input_file
*inpf
)
1675 /* Cook up an initial version of the gt- file name from the file real
1676 basename and the language name, if any. */
1678 const char *basename
= get_file_realbasename (inpf
);
1679 const char *langdir
= get_file_langdir (inpf
);
1682 (langdir
? xasprintf ("gt-%s-%s", langdir
, basename
)
1683 : xasprintf ("gt-%s", basename
));
1685 /* Then replace all non alphanumerics characters by '-' and change the
1686 extension to ".h". We expect the input filename extension was at least
1687 one character long. */
1691 for (; *s
!= '.'; s
++)
1692 if (!ISALNUM (*s
) && *s
!= '-')
1695 memcpy (s
, ".h", sizeof (".h"));
1700 /* Each input_file has its associated output file outf_p. The
1701 association is computed by the function
1702 get_output_file_with_visibility. The associated file is cached
1703 inside input_file in its inpoutf field, so is really computed only
1704 once. Associated output file paths (i.e. output_name-s) are
1705 computed by a rule based regexp machinery, using the files_rules
1706 array of struct file_rule_st. A for_name is also computed, giving
1707 the source file name for which the output_file is generated; it is
1708 often the last component of the input_file path. */
1712 Regexpr machinery to compute the output_name and for_name-s of each
1713 input_file. We have a sequence of file rules which gives the POSIX
1714 extended regular expression to match an input file path, and two
1715 transformed strings for the corresponding output_name and the
1716 corresponding for_name. The transformed string contain dollars: $0
1717 is replaced by the entire match, $1 is replaced by the substring
1718 matching the first parenthesis in the regexp, etc. And $$ is replaced
1719 by a single verbatim dollar. The rule order is important. The
1720 general case is last, and the particular cases should come before.
1721 An action routine can, when needed, update the out_name & for_name
1722 and/or return the appropriate output file. It is invoked only when a
1723 rule is triggered. When a rule is triggered, the output_name and
1724 for_name are computed using their transform string in while $$, $0,
1725 $1, ... are suitably replaced. If there is an action, it is called.
1726 In some few cases, the action can directly return the outf_p, but
1727 usually it just updates the output_name and for_name so should free
1728 them before replacing them. The get_output_file_with_visibility
1729 function creates an outf_p only once per each output_name, so it
1730 scans the output_files list for previously seen output file names.
1733 /* Signature of actions in file rules. */
1734 typedef outf_p (frul_actionrout_t
) (input_file
*, char**, char**);
1737 struct file_rule_st
{
1738 const char* frul_srcexpr
; /* Source string for regexp. */
1739 int frul_rflags
; /* Flags passed to regcomp, usually
1741 regex_t
* frul_re
; /* Compiled regular expression
1742 obtained by regcomp. */
1743 const char* frul_tr_out
; /* Transformation string for making
1744 * the output_name, with $1 ... $9 for
1745 * subpatterns and $0 for the whole
1746 * matched filename. */
1747 const char* frul_tr_for
; /* Tranformation string for making the
1749 frul_actionrout_t
* frul_action
; /* The action, if non null, is
1750 * called once the rule matches, on
1751 * the transformed out_name &
1752 * for_name. It could change them
1753 * and/or give the output file. */
1756 /* File rule action handling *.h files. */
1757 static outf_p
header_dot_h_frul (input_file
*, char**, char**);
1759 /* File rule action handling *.c files. */
1760 static outf_p
source_dot_c_frul (input_file
*, char**, char**);
1762 #define NULL_REGEX (regex_t*)0
1764 /* The prefix in our regexp-s matching the directory. */
1765 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1767 #define NULL_FRULACT (frul_actionrout_t*)0
1769 /* The array of our rules governing file name generation. Rules order
1770 matters, so change with extreme care! */
1772 struct file_rule_st files_rules
[] = {
1773 /* The general rule assumes that files in subdirectories belong to a
1774 particular front-end, and files not in subdirectories are shared.
1775 The following rules deal with exceptions - files that are in
1776 subdirectories and yet are shared, and files that are top-level,
1777 but are not shared. */
1779 /* the c-family/ source directory is special. */
1780 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.c$",
1781 REG_EXTENDED
, NULL_REGEX
,
1782 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT
},
1784 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.h$",
1785 REG_EXTENDED
, NULL_REGEX
,
1786 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT
},
1788 /* Both c-lang.h & c-tree.h gives gt-c-decl.h for c-decl.c ! */
1789 { DIR_PREFIX_REGEX
"c-lang\\.h$",
1790 REG_EXTENDED
, NULL_REGEX
, "gt-c-decl.h", "c-decl.c", NULL_FRULACT
},
1792 { DIR_PREFIX_REGEX
"c-tree\\.h$",
1793 REG_EXTENDED
, NULL_REGEX
, "gt-c-decl.h", "c-decl.c", NULL_FRULACT
},
1795 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1796 { DIR_PREFIX_REGEX
"cp/cp-tree\\.h$",
1797 REG_EXTENDED
, NULL_REGEX
,
1798 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT
},
1800 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1801 { DIR_PREFIX_REGEX
"cp/decl\\.[ch]$",
1802 REG_EXTENDED
, NULL_REGEX
,
1803 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT
},
1805 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1806 { DIR_PREFIX_REGEX
"cp/name-lookup\\.h$",
1807 REG_EXTENDED
, NULL_REGEX
,
1808 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT
},
1810 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1811 { DIR_PREFIX_REGEX
"cp/parser\\.h$",
1812 REG_EXTENDED
, NULL_REGEX
,
1813 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT
},
1815 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1816 { DIR_PREFIX_REGEX
"objc/objc-act\\.h$",
1817 REG_EXTENDED
, NULL_REGEX
,
1818 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT
},
1820 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1821 { DIR_PREFIX_REGEX
"objc/objc-map\\.h$",
1822 REG_EXTENDED
, NULL_REGEX
,
1823 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT
},
1825 /* General cases. For header *.h and source *.c files, we need
1826 * special actions to handle the language. */
1828 /* Source *.c files are using get_file_gtfilename to compute their
1829 output_name and get_file_basename to compute their for_name
1830 thru the source_dot_c_frul action. */
1831 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.c$",
1832 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.c", source_dot_c_frul
},
1833 /* Common header files get "gtype-desc.c" as their output_name,
1834 * while language specific header files are handled specially. So
1835 * we need the header_dot_h_frul action. */
1836 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.h$",
1837 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.h", header_dot_h_frul
},
1839 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.in$",
1840 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.in", NULL_FRULACT
},
1842 /* Mandatory null last entry signaling end of rules. */
1843 {NULL
, 0, NULL_REGEX
, NULL
, NULL
, NULL_FRULACT
}
1846 /* Special file rules action for handling *.h header files. It gives
1847 "gtype-desc.c" for common headers and corresponding output
1848 files for language-specific header files. */
1850 header_dot_h_frul (input_file
* inpf
, char**poutname
,
1851 char**pforname ATTRIBUTE_UNUSED
)
1853 const char *basename
= 0;
1855 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
1856 (void*) inpf
, get_input_file_name (inpf
),
1857 *poutname
, *pforname
);
1858 basename
= get_file_basename (inpf
);
1859 lang_index
= get_prefix_langdir_index (basename
);
1860 DBGPRINTF ("basename %s lang_index %d", basename
, lang_index
);
1862 if (lang_index
>= 0)
1864 /* The header is language specific. Given output_name &
1865 for_name remains unchanged. The base_files array gives the
1867 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
1868 (void*) base_files
[lang_index
],
1869 (base_files
[lang_index
])->name
);
1870 return base_files
[lang_index
];
1874 /* The header is common to all front-end languages. So
1875 output_name is "gtype-desc.c" file. The calling function
1876 get_output_file_with_visibility will find its outf_p. */
1878 *poutname
= xstrdup ("gtype-desc.c");
1879 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
1880 get_input_file_name (inpf
));
1886 /* Special file rules action for handling *.c source files using
1887 * get_file_gtfilename to compute their output_name and
1888 * get_file_basename to compute their for_name. The output_name is
1889 * gt-<LANG>-<BASE>.h for language specific source files, and
1890 * gt-<BASE>.h for common source files. */
1892 source_dot_c_frul (input_file
* inpf
, char**poutname
, char**pforname
)
1894 char *newbasename
= CONST_CAST (char*, get_file_basename (inpf
));
1895 char *newoutname
= CONST_CAST (char*, get_file_gtfilename (inpf
));
1896 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
1897 (void*) inpf
, get_input_file_name (inpf
),
1898 *poutname
, *pforname
);
1899 DBGPRINTF ("newoutname %s", newoutname
);
1900 DBGPRINTF ("newbasename %s", newbasename
);
1903 *poutname
= newoutname
;
1904 *pforname
= newbasename
;
1908 /* Utility function for get_output_file_with_visibility which returns
1909 * a malloc-ed substituted string using TRS on matching of the FILNAM
1910 * file name, using the PMATCH array. */
1912 matching_file_name_substitute (const char *filnam
, regmatch_t pmatch
[10],
1915 struct obstack str_obstack
;
1917 char *rawstr
= NULL
;
1918 const char *pt
= NULL
;
1919 DBGPRINTF ("filnam %s", filnam
);
1920 obstack_init (&str_obstack
);
1921 for (pt
= trs
; *pt
; pt
++) {
1927 /* A double dollar $$ is substituted by a single verbatim
1928 dollar, but who really uses dollar signs in file
1930 obstack_1grow (&str_obstack
, '$');
1932 else if (ISDIGIT (pt
[1]))
1934 /* Handle $0 $1 ... $9 by appropriate substitution. */
1935 int dolnum
= pt
[1] - '0';
1936 int so
= pmatch
[dolnum
].rm_so
;
1937 int eo
= pmatch
[dolnum
].rm_eo
;
1938 DBGPRINTF ("so=%d eo=%d dolnum=%d", so
, eo
, dolnum
);
1939 if (so
>=0 && eo
>=so
)
1940 obstack_grow (&str_obstack
, filnam
+ so
, eo
- so
);
1944 /* This can happen only when files_rules is buggy! */
1947 /* Always skip the character after the dollar. */
1951 obstack_1grow (&str_obstack
, c
);
1953 obstack_1grow (&str_obstack
, '\0');
1954 rawstr
= XOBFINISH (&str_obstack
, char *);
1955 str
= xstrdup (rawstr
);
1956 obstack_free (&str_obstack
, NULL
);
1957 DBGPRINTF ("matched replacement %s", str
);
1963 /* An output file, suitable for definitions, that can see declarations
1964 made in INPF and is linked into every language that uses INPF.
1965 Since the result is cached inside INPF, that argument cannot be
1966 declared constant, but is "almost" constant. */
1969 get_output_file_with_visibility (input_file
*inpf
)
1972 char *for_name
= NULL
;
1973 char *output_name
= NULL
;
1974 const char* inpfname
;
1976 /* This can happen when we need a file with visibility on a
1977 structure that we've never seen. We have to just hope that it's
1978 globally visible. */
1980 inpf
= system_h_file
;
1982 /* The result is cached in INPF, so return it if already known. */
1984 return inpf
->inpoutf
;
1986 /* In plugin mode, return NULL unless the input_file is one of the
1991 for (i
= 0; i
< nb_plugin_files
; i
++)
1992 if (inpf
== plugin_files
[i
])
1994 inpf
->inpoutf
= plugin_output
;
1995 return plugin_output
;
2001 inpfname
= get_input_file_name (inpf
);
2003 /* Try each rule in sequence in files_rules until one is triggered. */
2006 DBGPRINTF ("passing input file @ %p named %s thru the files_rules",
2007 (void*) inpf
, inpfname
);
2009 for (; files_rules
[rulix
].frul_srcexpr
!= NULL
; rulix
++)
2011 DBGPRINTF ("rulix#%d srcexpr %s",
2012 rulix
, files_rules
[rulix
].frul_srcexpr
);
2014 if (!files_rules
[rulix
].frul_re
)
2016 /* Compile the regexpr lazily. */
2018 files_rules
[rulix
].frul_re
= XCNEW (regex_t
);
2019 err
= regcomp (files_rules
[rulix
].frul_re
,
2020 files_rules
[rulix
].frul_srcexpr
,
2021 files_rules
[rulix
].frul_rflags
);
2024 /* The regular expression compilation fails only when
2025 file_rules is buggy. */
2033 /* Match the regexpr and trigger the rule if matched. */
2035 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2037 regmatch_t pmatch
[10];
2038 memset (pmatch
, 0, sizeof (pmatch
));
2039 if (!regexec (files_rules
[rulix
].frul_re
,
2040 inpfname
, 10, pmatch
, 0))
2042 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2043 (void*) inpf
, inpfname
, rulix
,
2044 files_rules
[rulix
].frul_srcexpr
);
2046 matching_file_name_substitute (inpfname
, pmatch
,
2047 files_rules
[rulix
].frul_tr_for
);
2048 DBGPRINTF ("for_name %s", for_name
);
2050 matching_file_name_substitute (inpfname
, pmatch
,
2051 files_rules
[rulix
].frul_tr_out
);
2052 DBGPRINTF ("output_name %s", output_name
);
2053 if (files_rules
[rulix
].frul_action
)
2055 /* Invoke our action routine. */
2057 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2058 rulix
, output_name
, for_name
);
2060 (files_rules
[rulix
].frul_action
) (inpf
,
2061 &output_name
, &for_name
);
2062 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2063 rulix
, (void*)of
, output_name
, for_name
);
2064 /* If the action routine returned something, give it back
2065 immediately and cache it in inpf. */
2072 /* The rule matched, and had no action, or that action did
2073 not return any output file but could have changed the
2074 output_name or for_name. We break out of the loop on the
2080 /* The regexpr did not match. */
2081 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2082 rulix
, inpfname
, files_rules
[rulix
].frul_srcexpr
);
2088 if (!output_name
|| !for_name
)
2090 /* This is impossible, and could only happen if the files_rules is
2091 incomplete or buggy. */
2095 /* Look through to see if we've ever seen this output filename
2096 before. If found, cache the result in inpf. */
2097 for (r
= output_files
; r
; r
= r
->next
)
2098 if (filename_cmp (r
->name
, output_name
) == 0)
2101 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r
,
2102 output_name
, for_name
);
2106 /* If not found, create it, and cache it in inpf. */
2107 r
= create_file (for_name
, output_name
);
2109 gcc_assert (r
&& r
->name
);
2110 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r
,
2111 output_name
, for_name
);
2118 /* The name of an output file, suitable for definitions, that can see
2119 declarations made in INPF and is linked into every language that
2123 get_output_file_name (input_file
* inpf
)
2125 outf_p o
= get_output_file_with_visibility (inpf
);
2131 /* Check if existing file is equal to the in memory buffer. */
2134 is_file_equal (outf_p of
)
2136 FILE *newfile
= fopen (of
->name
, "r");
2139 if (newfile
== NULL
)
2143 for (i
= 0; i
< of
->bufused
; i
++)
2146 ch
= fgetc (newfile
);
2147 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
2157 /* Copy the output to its final destination,
2158 but don't unnecessarily change modification times. */
2161 close_output_files (void)
2163 int nbwrittenfiles
= 0;
2166 for (of
= output_files
; of
; of
= of
->next
)
2169 if (!is_file_equal (of
))
2171 FILE *newfile
= NULL
;
2172 char *backupname
= NULL
;
2173 /* Back up the old version of the output file gt-FOO.c as
2174 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2177 backupname
= concat (backup_dir
, "/",
2178 lbasename (of
->name
), "~", NULL
);
2179 if (!access (of
->name
, F_OK
) && rename (of
->name
, backupname
))
2180 fatal ("failed to back up %s as %s: %s",
2181 of
->name
, backupname
, xstrerror (errno
));
2184 newfile
= fopen (of
->name
, "w");
2185 if (newfile
== NULL
)
2186 fatal ("opening output file %s: %s", of
->name
, xstrerror (errno
));
2187 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
2188 fatal ("writing output file %s: %s", of
->name
, xstrerror (errno
));
2189 if (fclose (newfile
) != 0)
2190 fatal ("closing output file %s: %s", of
->name
, xstrerror (errno
));
2192 if (verbosity_level
>= 2 && backupname
)
2193 printf ("%s wrote #%-3d %s backed-up in %s\n",
2194 progname
, nbwrittenfiles
, of
->name
, backupname
);
2195 else if (verbosity_level
>= 1)
2196 printf ("%s write #%-3d %s\n", progname
, nbwrittenfiles
, of
->name
);
2201 /* output file remains unchanged. */
2202 if (verbosity_level
>= 2)
2203 printf ("%s keep %s\n", progname
, of
->name
);
2207 of
->bufused
= of
->buflength
= 0;
2209 if (verbosity_level
>= 1)
2210 printf ("%s wrote %d files.\n", progname
, nbwrittenfiles
);
2217 const input_file
* file
;
2221 struct walk_type_data
;
2223 /* For scalars and strings, given the item in 'val'.
2224 For structures, given a pointer to the item in 'val'.
2225 For misc. pointers, given the item in 'val'.
2227 typedef void (*process_field_fn
) (type_p f
, const struct walk_type_data
* p
);
2228 typedef void (*func_name_fn
) (type_p s
, const struct walk_type_data
* p
);
2230 /* Parameters for write_types. */
2232 struct write_types_data
2235 const char *param_prefix
;
2236 const char *subfield_marker_routine
;
2237 const char *marker_routine
;
2238 const char *reorder_note_routine
;
2239 const char *comment
;
2240 int skip_hooks
; /* skip hook generation if non zero */
2243 static void output_escaped_param (struct walk_type_data
*d
,
2244 const char *, const char *);
2245 static void output_mangled_typename (outf_p
, const_type_p
);
2246 static void walk_type (type_p t
, struct walk_type_data
*d
);
2247 static void write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
2248 const struct write_types_data
*wtd
);
2249 static void write_types_process_field
2250 (type_p f
, const struct walk_type_data
*d
);
2251 static void write_types (outf_p output_header
,
2253 type_p param_structs
,
2254 const struct write_types_data
*wtd
);
2255 static void write_types_local_process_field
2256 (type_p f
, const struct walk_type_data
*d
);
2257 static void write_local_func_for_structure
2258 (const_type_p orig_s
, type_p s
, type_p
*param
);
2259 static void write_local (outf_p output_header
,
2260 type_p structures
, type_p param_structs
);
2261 static void write_enum_defn (type_p structures
, type_p param_structs
);
2262 static int contains_scalar_p (type_p t
);
2263 static void put_mangled_filename (outf_p
, const input_file
*);
2264 static void finish_root_table (struct flist
*flp
, const char *pfx
,
2265 const char *tname
, const char *lastname
,
2267 static void write_root (outf_p
, pair_p
, type_p
, const char *, int,
2268 struct fileloc
*, const char *, bool);
2269 static void write_array (outf_p f
, pair_p v
,
2270 const struct write_types_data
*wtd
);
2271 static void write_roots (pair_p
, bool);
2273 /* Parameters for walk_type. */
2275 struct walk_type_data
2277 process_field_fn process_field
;
2282 const char *prev_val
[4];
2285 const struct fileloc
*line
;
2290 const char *reorder_fn
;
2292 bool fn_wants_lvalue
;
2295 /* Print a mangled name representing T to OF. */
2298 output_mangled_typename (outf_p of
, const_type_p t
)
2310 output_mangled_typename (of
, t
->u
.p
);
2320 case TYPE_LANG_STRUCT
:
2321 oprintf (of
, "%lu%s", (unsigned long) strlen (t
->u
.s
.tag
),
2324 case TYPE_PARAM_STRUCT
:
2327 for (i
= 0; i
< NUM_PARAM
; i
++)
2328 if (t
->u
.param_struct
.param
[i
] != NULL
)
2329 output_mangled_typename (of
, t
->u
.param_struct
.param
[i
]);
2330 output_mangled_typename (of
, t
->u
.param_struct
.stru
);
2338 /* Print PARAM to D->OF processing escapes. D->VAL references the
2339 current object, D->PREV_VAL the object containing the current
2340 object, ONAME is the name of the option and D->LINE is used to
2341 print error messages. */
2344 output_escaped_param (struct walk_type_data
*d
, const char *param
,
2349 for (p
= param
; *p
; p
++)
2351 oprintf (d
->of
, "%c", *p
);
2356 oprintf (d
->of
, "(%s)", d
->prev_val
[2]);
2359 oprintf (d
->of
, "(%s)", d
->prev_val
[0]);
2362 oprintf (d
->of
, "(%s)", d
->prev_val
[1]);
2366 const char *pp
= d
->val
+ strlen (d
->val
);
2367 while (pp
[-1] == ']')
2370 oprintf (d
->of
, "%s", pp
);
2374 error_at_line (d
->line
, "`%s' option contains bad escape %c%c",
2379 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2380 which is of type T. Write code to D->OF to constrain execution (at
2381 the point that D->PROCESS_FIELD is called) to the appropriate
2382 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2383 pointers to those objects. D->PREV_VAL lists the objects
2384 containing the current object, D->OPT is a list of options to
2385 apply, D->INDENT is the current indentation level, D->LINE is used
2386 to print error messages, D->BITMAP indicates which languages to
2387 print the structure for, and D->PARAM is the current parameter
2388 (from an enclosing param_is option). */
2391 walk_type (type_p t
, struct walk_type_data
*d
)
2393 const char *length
= NULL
;
2394 const char *desc
= NULL
;
2395 int maybe_undef_p
= 0;
2396 int use_param_num
= -1;
2397 int use_params_p
= 0;
2400 const struct nested_ptr_data
*nested_ptr_d
= NULL
;
2402 d
->needs_cast_p
= false;
2403 for (oo
= d
->opt
; oo
; oo
= oo
->next
)
2404 if (strcmp (oo
->name
, "length") == 0 && oo
->kind
== OPTION_STRING
)
2405 length
= oo
->info
.string
;
2406 else if (strcmp (oo
->name
, "maybe_undef") == 0)
2408 else if (strncmp (oo
->name
, "use_param", 9) == 0
2409 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2410 use_param_num
= oo
->name
[9] == '\0' ? 0 : oo
->name
[9] - '0';
2411 else if (strcmp (oo
->name
, "use_params") == 0)
2413 else if (strcmp (oo
->name
, "desc") == 0 && oo
->kind
== OPTION_STRING
)
2414 desc
= oo
->info
.string
;
2415 else if (strcmp (oo
->name
, "mark_hook") == 0)
2417 else if (strcmp (oo
->name
, "nested_ptr") == 0
2418 && oo
->kind
== OPTION_NESTED
)
2419 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
.nested
;
2420 else if (strcmp (oo
->name
, "dot") == 0)
2422 else if (strcmp (oo
->name
, "tag") == 0)
2424 else if (strcmp (oo
->name
, "special") == 0)
2426 else if (strcmp (oo
->name
, "skip") == 0)
2428 else if (strcmp (oo
->name
, "atomic") == 0)
2430 else if (strcmp (oo
->name
, "default") == 0)
2432 else if (strcmp (oo
->name
, "param_is") == 0)
2434 else if (strncmp (oo
->name
, "param", 5) == 0
2435 && ISDIGIT (oo
->name
[5]) && strcmp (oo
->name
+ 6, "_is") == 0)
2437 else if (strcmp (oo
->name
, "chain_next") == 0)
2439 else if (strcmp (oo
->name
, "chain_prev") == 0)
2441 else if (strcmp (oo
->name
, "chain_circular") == 0)
2443 else if (strcmp (oo
->name
, "reorder") == 0)
2445 else if (strcmp (oo
->name
, "variable_size") == 0)
2448 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
2455 int pointer_p
= t
->kind
== TYPE_POINTER
;
2459 if (!UNION_OR_STRUCT_P (t
))
2460 error_at_line (d
->line
, "`use_params' option on unimplemented type");
2462 t
= find_param_structure (t
, d
->param
);
2464 t
= create_pointer (t
);
2467 if (use_param_num
!= -1)
2469 if (d
->param
!= NULL
&& d
->param
[use_param_num
] != NULL
)
2471 type_p nt
= d
->param
[use_param_num
];
2473 if (t
->kind
== TYPE_ARRAY
)
2474 nt
= create_array (nt
, t
->u
.a
.len
);
2475 else if (length
!= NULL
&& t
->kind
== TYPE_POINTER
)
2476 nt
= create_pointer (nt
);
2477 d
->needs_cast_p
= (t
->kind
!= TYPE_POINTER
2478 && (nt
->kind
== TYPE_POINTER
2479 || nt
->kind
== TYPE_STRING
));
2483 error_at_line (d
->line
, "no parameter defined for `%s'", d
->val
);
2487 && (t
->kind
!= TYPE_POINTER
|| !UNION_OR_STRUCT_P (t
->u
.p
)))
2489 error_at_line (d
->line
,
2490 "field `%s' has invalid option `maybe_undef_p'\n",
2495 if (atomic_p
&& (t
->kind
!= TYPE_POINTER
))
2497 error_at_line (d
->line
, "field `%s' has invalid option `atomic'\n", d
->val
);
2505 d
->process_field (t
, d
);
2510 if (maybe_undef_p
&& t
->u
.p
->u
.s
.line
.file
== NULL
)
2512 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
2516 /* If a pointer type is marked as "atomic", we process the
2517 field itself, but we don't walk the data that they point to.
2519 There are two main cases where we walk types: to mark
2520 pointers that are reachable, and to relocate pointers when
2521 writing a PCH file. In both cases, an atomic pointer is
2522 itself marked or relocated, but the memory that it points
2523 to is left untouched. In the case of PCH, that memory will
2524 be read/written unchanged to the PCH file. */
2527 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2529 d
->process_field (t
, d
);
2531 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2537 if (!UNION_OR_STRUCT_P (t
->u
.p
)
2538 && t
->u
.p
->kind
!= TYPE_PARAM_STRUCT
)
2540 error_at_line (d
->line
,
2541 "field `%s' is pointer to unimplemented type",
2548 const char *oldprevval2
= d
->prev_val
[2];
2550 if (!UNION_OR_STRUCT_P (nested_ptr_d
->type
))
2552 error_at_line (d
->line
,
2553 "field `%s' has invalid "
2554 "option `nested_ptr'\n", d
->val
);
2558 d
->prev_val
[2] = d
->val
;
2559 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2561 d
->val
= xasprintf ("x%d", d
->counter
++);
2562 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
2563 (nested_ptr_d
->type
->kind
== TYPE_UNION
2564 ? "union" : "struct"),
2565 nested_ptr_d
->type
->u
.s
.tag
,
2566 d
->fn_wants_lvalue
? "" : "const ", d
->val
);
2567 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
2568 output_escaped_param (d
, nested_ptr_d
->convert_from
,
2570 oprintf (d
->of
, ";\n");
2572 d
->process_field (nested_ptr_d
->type
, d
);
2574 if (d
->fn_wants_lvalue
)
2576 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
2578 d
->prev_val
[2] = d
->val
;
2579 output_escaped_param (d
, nested_ptr_d
->convert_to
,
2581 oprintf (d
->of
, ";\n");
2585 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2586 d
->val
= d
->prev_val
[2];
2587 d
->prev_val
[2] = oldprevval2
;
2590 d
->process_field (t
->u
.p
, d
);
2594 int loopcounter
= d
->counter
++;
2595 const char *oldval
= d
->val
;
2596 const char *oldprevval3
= d
->prev_val
[3];
2599 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2601 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2602 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
,
2603 "", loopcounter
, loopcounter
);
2604 output_escaped_param (d
, length
, "length");
2605 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
2607 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2609 d
->prev_val
[3] = oldval
;
2610 walk_type (t
->u
.p
, d
);
2613 d
->prev_val
[3] = oldprevval3
;
2616 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2617 d
->process_field (t
, d
);
2619 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2626 int loopcounter
= d
->counter
++;
2627 const char *oldval
= d
->val
;
2630 /* If it's an array of scalars, we optimize by not generating
2632 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
2635 /* When walking an array, compute the length and store it in a
2636 local variable before walking the array elements, instead of
2637 recomputing the length expression each time through the loop.
2638 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2639 where the length is stored in the first array element,
2640 because otherwise that operand can get overwritten on the
2642 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2644 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2645 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
2646 d
->indent
, "", loopcounter
);
2648 output_escaped_param (d
, length
, "length");
2650 oprintf (d
->of
, "%s", t
->u
.a
.len
);
2651 oprintf (d
->of
, ");\n");
2653 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2655 loopcounter
, loopcounter
, loopcounter
, loopcounter
);
2657 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2659 walk_type (t
->u
.a
.p
, d
);
2664 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2666 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2674 const char *oldval
= d
->val
;
2675 const char *oldprevval1
= d
->prev_val
[1];
2676 const char *oldprevval2
= d
->prev_val
[2];
2677 const int union_p
= t
->kind
== TYPE_UNION
;
2678 int seen_default_p
= 0;
2681 if (!t
->u
.s
.line
.file
)
2682 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
2684 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
2686 error_at_line (d
->line
,
2687 "structure `%s' defined for mismatching languages",
2689 error_at_line (&t
->u
.s
.line
, "one structure defined here");
2692 /* Some things may also be defined in the structure's options. */
2693 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
2694 if (!desc
&& strcmp (o
->name
, "desc") == 0
2695 && o
->kind
== OPTION_STRING
)
2696 desc
= o
->info
.string
;
2698 d
->prev_val
[2] = oldval
;
2699 d
->prev_val
[1] = oldprevval2
;
2704 error_at_line (d
->line
,
2705 "missing `desc' option for union `%s'",
2709 oprintf (d
->of
, "%*sswitch (", d
->indent
, "");
2710 output_escaped_param (d
, desc
, "desc");
2711 oprintf (d
->of
, ")\n");
2713 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2715 for (f
= t
->u
.s
.fields
; f
; f
= f
->next
)
2718 const char *dot
= ".";
2719 const char *tagid
= NULL
;
2722 int use_param_p
= 0;
2725 d
->reorder_fn
= NULL
;
2726 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
2727 if (strcmp (oo
->name
, "dot") == 0
2728 && oo
->kind
== OPTION_STRING
)
2729 dot
= oo
->info
.string
;
2730 else if (strcmp (oo
->name
, "tag") == 0
2731 && oo
->kind
== OPTION_STRING
)
2732 tagid
= oo
->info
.string
;
2733 else if (strcmp (oo
->name
, "skip") == 0)
2735 else if (strcmp (oo
->name
, "default") == 0)
2737 else if (strcmp (oo
->name
, "reorder") == 0
2738 && oo
->kind
== OPTION_STRING
)
2739 d
->reorder_fn
= oo
->info
.string
;
2740 else if (strncmp (oo
->name
, "use_param", 9) == 0
2741 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2747 if (union_p
&& tagid
)
2749 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
2752 else if (union_p
&& default_p
)
2754 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
2758 else if (!union_p
&& (default_p
|| tagid
))
2759 error_at_line (d
->line
,
2760 "can't use `%s' outside a union on field `%s'",
2761 default_p
? "default" : "tag", f
->name
);
2762 else if (union_p
&& !(default_p
|| tagid
)
2763 && f
->type
->kind
== TYPE_SCALAR
)
2766 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2767 get_input_file_name (d
->line
->file
), d
->line
->line
,
2771 else if (union_p
&& !(default_p
|| tagid
))
2772 error_at_line (d
->line
,
2773 "field `%s' is missing `tag' or `default' option",
2777 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
2779 d
->used_length
= false;
2781 if (union_p
&& use_param_p
&& d
->param
== NULL
)
2782 oprintf (d
->of
, "%*sgcc_unreachable ();\n", d
->indent
, "");
2784 walk_type (f
->type
, d
);
2790 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
2794 d
->reorder_fn
= NULL
;
2797 d
->prev_val
[1] = oldprevval1
;
2798 d
->prev_val
[2] = oldprevval2
;
2800 if (union_p
&& !seen_default_p
)
2802 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
2803 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
2807 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2813 case TYPE_LANG_STRUCT
:
2816 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
2817 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
2820 error_at_line (d
->line
, "structure `%s' differs between languages",
2827 case TYPE_PARAM_STRUCT
:
2829 type_p
*oldparam
= d
->param
;
2831 d
->param
= t
->u
.param_struct
.param
;
2832 walk_type (t
->u
.param_struct
.stru
, d
);
2833 d
->param
= oldparam
;
2842 /* process_field routine for marking routines. */
2845 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
2847 const struct write_types_data
*wtd
;
2848 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
2849 wtd
= (const struct write_types_data
*) d
->cookie
;
2856 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
2857 wtd
->subfield_marker_routine
, cast
, d
->val
);
2858 if (wtd
->param_prefix
)
2860 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
2863 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
2864 output_mangled_typename (d
->of
, d
->orig_s
);
2867 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
2869 if (f
->u
.p
->kind
== TYPE_PARAM_STRUCT
2870 && f
->u
.p
->u
.s
.line
.file
!= NULL
)
2872 oprintf (d
->of
, ", gt_e_");
2873 output_mangled_typename (d
->of
, f
);
2875 else if (UNION_OR_STRUCT_P (f
) && f
->u
.p
->u
.s
.line
.file
!= NULL
)
2877 oprintf (d
->of
, ", gt_ggc_e_");
2878 output_mangled_typename (d
->of
, f
);
2881 oprintf (d
->of
, ", gt_types_enum_last");
2883 oprintf (d
->of
, ");\n");
2884 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
2885 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
2886 wtd
->reorder_note_routine
, cast
, d
->val
,
2887 d
->prev_val
[3], d
->reorder_fn
);
2893 case TYPE_LANG_STRUCT
:
2894 case TYPE_PARAM_STRUCT
:
2895 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
2896 output_mangled_typename (d
->of
, f
);
2897 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
2898 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
2899 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
2900 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
2912 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
2915 output_type_enum (outf_p of
, type_p s
)
2917 if (s
->kind
== TYPE_PARAM_STRUCT
&& s
->u
.param_struct
.line
.file
!= NULL
)
2919 oprintf (of
, ", gt_e_");
2920 output_mangled_typename (of
, s
);
2922 else if (UNION_OR_STRUCT_P (s
) && s
->u
.s
.line
.file
!= NULL
)
2924 oprintf (of
, ", gt_ggc_e_");
2925 output_mangled_typename (of
, s
);
2928 oprintf (of
, ", gt_types_enum_last");
2931 /* Return an output file that is suitable for definitions which can
2932 reference struct S */
2935 get_output_file_for_structure (const_type_p s
, type_p
*param
)
2937 const input_file
*fn
;
2940 gcc_assert (UNION_OR_STRUCT_P (s
));
2941 fn
= s
->u
.s
.line
.file
;
2943 /* This is a hack, and not the good kind either. */
2944 for (i
= NUM_PARAM
- 1; i
>= 0; i
--)
2945 if (param
&& param
[i
] && param
[i
]->kind
== TYPE_POINTER
2946 && UNION_OR_STRUCT_P (param
[i
]->u
.p
))
2947 fn
= param
[i
]->u
.p
->u
.s
.line
.file
;
2949 /* The call to get_output_file_with_visibility may update fn by
2950 caching its result inside, so we need the CONST_CAST. */
2951 return get_output_file_with_visibility (CONST_CAST (input_file
*, fn
));
2954 /* For S, a structure that's part of ORIG_S, and using parameters
2955 PARAM, write out a routine that:
2956 - Takes a parameter, a void * but actually of type *S
2957 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
2958 field of S or its substructures and (in some cases) things
2959 that are pointed to by S.
2963 write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
2964 const struct write_types_data
*wtd
)
2966 const char *chain_next
= NULL
;
2967 const char *chain_prev
= NULL
;
2968 const char *chain_circular
= NULL
;
2969 const char *mark_hook_name
= NULL
;
2971 struct walk_type_data d
;
2973 memset (&d
, 0, sizeof (d
));
2974 d
.of
= get_output_file_for_structure (s
, param
);
2975 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2976 if (strcmp (opt
->name
, "chain_next") == 0
2977 && opt
->kind
== OPTION_STRING
)
2978 chain_next
= opt
->info
.string
;
2979 else if (strcmp (opt
->name
, "chain_prev") == 0
2980 && opt
->kind
== OPTION_STRING
)
2981 chain_prev
= opt
->info
.string
;
2982 else if (strcmp (opt
->name
, "chain_circular") == 0
2983 && opt
->kind
== OPTION_STRING
)
2984 chain_circular
= opt
->info
.string
;
2985 else if (strcmp (opt
->name
, "mark_hook") == 0
2986 && opt
->kind
== OPTION_STRING
)
2987 mark_hook_name
= opt
->info
.string
;
2988 if (chain_prev
!= NULL
&& chain_next
== NULL
)
2989 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
2990 if (chain_circular
!= NULL
&& chain_next
!= NULL
)
2991 error_at_line (&s
->u
.s
.line
, "chain_circular with chain_next");
2992 if (chain_circular
!= NULL
)
2993 chain_next
= chain_circular
;
2995 d
.process_field
= write_types_process_field
;
2999 d
.line
= &s
->u
.s
.line
;
3000 d
.bitmap
= s
->u
.s
.bitmap
;
3002 d
.prev_val
[0] = "*x";
3003 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3004 d
.prev_val
[3] = "x";
3007 oprintf (d
.of
, "\n");
3008 oprintf (d
.of
, "void\n");
3010 oprintf (d
.of
, "gt_%sx_%s", wtd
->prefix
, orig_s
->u
.s
.tag
);
3013 oprintf (d
.of
, "gt_%s_", wtd
->prefix
);
3014 output_mangled_typename (d
.of
, orig_s
);
3016 oprintf (d
.of
, " (void *x_p)\n");
3017 oprintf (d
.of
, "{\n");
3018 oprintf (d
.of
, " %s %s * %sx = (%s %s *)x_p;\n",
3019 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
3020 chain_next
== NULL
? "const " : "",
3021 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3022 if (chain_next
!= NULL
)
3023 oprintf (d
.of
, " %s %s * xlimit = x;\n",
3024 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3025 if (chain_next
== NULL
)
3027 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
3028 if (wtd
->param_prefix
)
3030 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
3031 output_mangled_typename (d
.of
, orig_s
);
3032 output_type_enum (d
.of
, orig_s
);
3034 oprintf (d
.of
, "))\n");
3038 if (chain_circular
!= NULL
)
3039 oprintf (d
.of
, " if (!%s (xlimit", wtd
->marker_routine
);
3041 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3042 if (wtd
->param_prefix
)
3044 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3045 output_mangled_typename (d
.of
, orig_s
);
3046 output_type_enum (d
.of
, orig_s
);
3048 oprintf (d
.of
, "))\n");
3049 if (chain_circular
!= NULL
)
3050 oprintf (d
.of
, " return;\n do\n");
3051 if (mark_hook_name
&& !wtd
->skip_hooks
)
3053 oprintf (d
.of
, " {\n");
3054 oprintf (d
.of
, " %s (xlimit);\n ", mark_hook_name
);
3056 oprintf (d
.of
, " xlimit = (");
3057 d
.prev_val
[2] = "*xlimit";
3058 output_escaped_param (&d
, chain_next
, "chain_next");
3059 oprintf (d
.of
, ");\n");
3060 if (mark_hook_name
&& !wtd
->skip_hooks
)
3061 oprintf (d
.of
, " }\n");
3062 if (chain_prev
!= NULL
)
3064 oprintf (d
.of
, " if (x != xlimit)\n");
3065 oprintf (d
.of
, " for (;;)\n");
3066 oprintf (d
.of
, " {\n");
3067 oprintf (d
.of
, " %s %s * const xprev = (",
3068 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3070 d
.prev_val
[2] = "*x";
3071 output_escaped_param (&d
, chain_prev
, "chain_prev");
3072 oprintf (d
.of
, ");\n");
3073 oprintf (d
.of
, " if (xprev == NULL) break;\n");
3074 oprintf (d
.of
, " x = xprev;\n");
3075 oprintf (d
.of
, " (void) %s (xprev", wtd
->marker_routine
);
3076 if (wtd
->param_prefix
)
3078 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
3079 output_mangled_typename (d
.of
, orig_s
);
3080 output_type_enum (d
.of
, orig_s
);
3082 oprintf (d
.of
, ");\n");
3083 oprintf (d
.of
, " }\n");
3085 if (chain_circular
!= NULL
)
3087 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3088 if (wtd
->param_prefix
)
3090 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3091 output_mangled_typename (d
.of
, orig_s
);
3092 output_type_enum (d
.of
, orig_s
);
3094 oprintf (d
.of
, "));\n");
3095 if (mark_hook_name
&& !wtd
->skip_hooks
)
3096 oprintf (d
.of
, " %s (xlimit);\n", mark_hook_name
);
3097 oprintf (d
.of
, " do\n");
3100 oprintf (d
.of
, " while (x != xlimit)\n");
3102 oprintf (d
.of
, " {\n");
3103 if (mark_hook_name
&& chain_next
== NULL
&& !wtd
->skip_hooks
)
3105 oprintf (d
.of
, " %s (x);\n", mark_hook_name
);
3107 d
.prev_val
[2] = "*x";
3111 if (chain_next
!= NULL
)
3113 oprintf (d
.of
, " x = (");
3114 output_escaped_param (&d
, chain_next
, "chain_next");
3115 oprintf (d
.of
, ");\n");
3118 oprintf (d
.of
, " }\n");
3119 if (chain_circular
!= NULL
)
3120 oprintf (d
.of
, " while (x != xlimit);\n");
3121 oprintf (d
.of
, "}\n");
3124 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3127 write_types (outf_p output_header
, type_p structures
, type_p param_structs
,
3128 const struct write_types_data
*wtd
)
3130 int nbfun
= 0; /* Count the emitted functions. */
3133 oprintf (output_header
, "\n/* %s*/\n", wtd
->comment
);
3134 /* We first emit the macros and the declarations. Functions' code is
3135 emitted afterwards. This is needed in plugin mode. */
3136 oprintf (output_header
, "/* macros and declarations */\n");
3137 for (s
= structures
; s
; s
= s
->next
)
3138 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3142 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3145 oprintf (output_header
, "#define gt_%s_", wtd
->prefix
);
3146 output_mangled_typename (output_header
, s
);
3147 oprintf (output_header
, "(X) do { \\\n");
3148 oprintf (output_header
,
3149 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd
->prefix
,
3151 oprintf (output_header
, " } while (0)\n");
3153 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3154 if (strcmp (opt
->name
, "ptr_alias") == 0
3155 && opt
->kind
== OPTION_TYPE
)
3157 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
3158 if (t
->kind
== TYPE_STRUCT
3159 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3160 oprintf (output_header
,
3161 "#define gt_%sx_%s gt_%sx_%s\n",
3162 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t
->u
.s
.tag
);
3164 error_at_line (&s
->u
.s
.line
,
3165 "structure alias is not a structure");
3171 /* Declare the marker procedure only once. */
3172 oprintf (output_header
,
3173 "extern void gt_%sx_%s (void *);\n",
3174 wtd
->prefix
, s
->u
.s
.tag
);
3176 if (s
->u
.s
.line
.file
== NULL
)
3178 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3184 for (s
= param_structs
; s
; s
= s
->next
)
3185 if (s
->gc_used
== GC_POINTED_TO
)
3187 type_p stru
= s
->u
.param_struct
.stru
;
3189 /* Declare the marker procedure. */
3190 oprintf (output_header
, "extern void gt_%s_", wtd
->prefix
);
3191 output_mangled_typename (output_header
, s
);
3192 oprintf (output_header
, " (void *);\n");
3194 if (stru
->u
.s
.line
.file
== NULL
)
3196 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3202 /* At last we emit the functions code. */
3203 oprintf (output_header
, "\n/* functions code */\n");
3204 for (s
= structures
; s
; s
= s
->next
)
3205 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3209 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3211 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3212 if (strcmp (opt
->name
, "ptr_alias") == 0)
3217 if (s
->kind
== TYPE_LANG_STRUCT
)
3220 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3223 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3224 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3225 write_func_for_structure (s
, ss
, NULL
, wtd
);
3231 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3232 nbfun
, (void*) s
, s
->u
.s
.tag
);
3233 write_func_for_structure (s
, s
, NULL
, wtd
);
3238 /* Structure s is not possibly pointed to, so can be ignored. */
3239 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3240 (void*)s
, s
->u
.s
.tag
,
3244 for (s
= param_structs
; s
; s
= s
->next
)
3245 if (s
->gc_used
== GC_POINTED_TO
)
3247 type_p
*param
= s
->u
.param_struct
.param
;
3248 type_p stru
= s
->u
.param_struct
.stru
;
3249 if (stru
->u
.s
.line
.file
== NULL
)
3251 if (stru
->kind
== TYPE_LANG_STRUCT
)
3254 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3257 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3258 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3259 write_func_for_structure (s
, ss
, param
, wtd
);
3265 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3267 (void*) stru
, stru
->u
.s
.tag
);
3268 write_func_for_structure (s
, stru
, param
, wtd
);
3273 /* Param structure s is not pointed to, so should be ignored. */
3274 DBGPRINTF ("ignored s @ %p", (void*)s
);
3276 if (verbosity_level
>= 2)
3277 printf ("%s emitted %d routines for %s\n",
3278 progname
, nbfun
, wtd
->comment
);
3281 static const struct write_types_data ggc_wtd
= {
3282 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
3283 "GC marker procedures. ",
3287 static const struct write_types_data pch_wtd
= {
3288 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3289 "gt_pch_note_reorder",
3290 "PCH type-walking procedures. ",
3294 /* Write out the local pointer-walking routines. */
3296 /* process_field routine for local pointer-walking. */
3299 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
3306 case TYPE_LANG_STRUCT
:
3307 case TYPE_PARAM_STRUCT
:
3309 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
3311 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3322 /* For S, a structure that's part of ORIG_S, and using parameters
3323 PARAM, write out a routine that:
3324 - Is of type gt_note_pointers
3325 - Calls PROCESS_FIELD on each field of S or its substructures.
3329 write_local_func_for_structure (const_type_p orig_s
, type_p s
, type_p
*param
)
3331 struct walk_type_data d
;
3333 memset (&d
, 0, sizeof (d
));
3334 d
.of
= get_output_file_for_structure (s
, param
);
3335 d
.process_field
= write_types_local_process_field
;
3337 d
.line
= &s
->u
.s
.line
;
3338 d
.bitmap
= s
->u
.s
.bitmap
;
3340 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
3341 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3342 d
.prev_val
[3] = "x";
3344 d
.fn_wants_lvalue
= true;
3346 oprintf (d
.of
, "\n");
3347 oprintf (d
.of
, "void\n");
3348 oprintf (d
.of
, "gt_pch_p_");
3349 output_mangled_typename (d
.of
, orig_s
);
3350 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3352 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3353 "\tATTRIBUTE_UNUSED void *cookie)\n");
3354 oprintf (d
.of
, "{\n");
3355 oprintf (d
.of
, " %s %s * const x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3356 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
3357 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3360 oprintf (d
.of
, "}\n");
3363 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3366 write_local (outf_p output_header
, type_p structures
, type_p param_structs
)
3372 oprintf (output_header
, "\n/* Local pointer-walking routines. */\n");
3373 for (s
= structures
; s
; s
= s
->next
)
3374 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3378 if (s
->u
.s
.line
.file
== NULL
)
3380 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3381 if (strcmp (opt
->name
, "ptr_alias") == 0
3382 && opt
->kind
== OPTION_TYPE
)
3384 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
3385 if (t
->kind
== TYPE_STRUCT
3386 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3388 oprintf (output_header
, "#define gt_pch_p_");
3389 output_mangled_typename (output_header
, s
);
3390 oprintf (output_header
, " gt_pch_p_");
3391 output_mangled_typename (output_header
, t
);
3392 oprintf (output_header
, "\n");
3395 error_at_line (&s
->u
.s
.line
,
3396 "structure alias is not a structure");
3402 /* Declare the marker procedure only once. */
3403 oprintf (output_header
, "extern void gt_pch_p_");
3404 output_mangled_typename (output_header
, s
);
3405 oprintf (output_header
,
3406 "\n (void *, void *, gt_pointer_operator, void *);\n");
3408 if (s
->kind
== TYPE_LANG_STRUCT
)
3411 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3412 write_local_func_for_structure (s
, ss
, NULL
);
3415 write_local_func_for_structure (s
, s
, NULL
);
3418 for (s
= param_structs
; s
; s
= s
->next
)
3419 if (s
->gc_used
== GC_POINTED_TO
)
3421 type_p
*param
= s
->u
.param_struct
.param
;
3422 type_p stru
= s
->u
.param_struct
.stru
;
3424 /* Declare the marker procedure. */
3425 oprintf (output_header
, "extern void gt_pch_p_");
3426 output_mangled_typename (output_header
, s
);
3427 oprintf (output_header
,
3428 "\n (void *, void *, gt_pointer_operator, void *);\n");
3430 if (stru
->u
.s
.line
.file
== NULL
)
3432 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3437 if (stru
->kind
== TYPE_LANG_STRUCT
)
3440 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3441 write_local_func_for_structure (s
, ss
, param
);
3444 write_local_func_for_structure (s
, stru
, param
);
3448 /* Nonzero if S is a type for which typed GC allocators should be output. */
3450 #define USED_BY_TYPED_GC_P(s) \
3451 (((s->kind == TYPE_POINTER) \
3452 && ((s->u.p->gc_used == GC_POINTED_TO) \
3453 || (s->u.p->gc_used == GC_USED))) \
3454 || (UNION_OR_STRUCT_P (s) && \
3455 (((s)->gc_used == GC_POINTED_TO) \
3456 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
3457 && s->u.s.line.file != NULL) \
3458 || ((s)->gc_used == GC_USED \
3459 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
3462 /* Write out the 'enum' definition for gt_types_enum. */
3465 write_enum_defn (type_p structures
, type_p param_structs
)
3469 int nbparamstruct
= 0;
3473 oprintf (header_file
, "\n/* Enumeration of types known. */\n");
3474 oprintf (header_file
, "enum gt_types_enum {\n");
3475 for (s
= structures
; s
; s
= s
->next
)
3476 if (USED_BY_TYPED_GC_P (s
))
3479 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
3480 (void*) s
, nbstruct
);
3481 if (UNION_OR_STRUCT_P (s
))
3482 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
3483 (void*) s
, nbstruct
, s
->u
.s
.tag
);
3484 oprintf (header_file
, " gt_ggc_e_");
3485 output_mangled_typename (header_file
, s
);
3486 oprintf (header_file
, ",\n");
3488 for (s
= param_structs
; s
; s
= s
->next
)
3489 if (s
->gc_used
== GC_POINTED_TO
)
3492 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
3493 (void*) s
, nbparamstruct
);
3494 oprintf (header_file
, " gt_e_");
3495 output_mangled_typename (header_file
, s
);
3496 oprintf (header_file
, ",\n");
3498 oprintf (header_file
, " gt_types_enum_last\n");
3499 oprintf (header_file
, "};\n");
3500 if (verbosity_level
>= 2)
3501 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
3502 progname
, nbstruct
, nbparamstruct
);
3506 /* Might T contain any non-pointer elements? */
3509 contains_scalar_p (type_p t
)
3517 return contains_scalar_p (t
->u
.a
.p
);
3519 /* Could also check for structures that have no non-pointer
3520 fields, but there aren't enough of those to worry about. */
3525 /* Mangle INPF and print it to F. */
3528 put_mangled_filename (outf_p f
, const input_file
*inpf
)
3530 /* The call to get_output_file_name may indirectly update fn since
3531 get_output_file_with_visibility caches its result inside, so we
3532 need the CONST_CAST. */
3533 const char *name
= get_output_file_name (CONST_CAST (input_file
*, inpf
));
3536 for (; *name
!= 0; name
++)
3537 if (ISALNUM (*name
))
3538 oprintf (f
, "%c", *name
);
3540 oprintf (f
, "%c", '_');
3543 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
3544 LASTNAME, and NAME are all strings to insert in various places in
3545 the resulting code. */
3548 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
3549 const char *tname
, const char *name
)
3553 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
3554 if (fli2
->started_p
)
3556 oprintf (fli2
->f
, " %s\n", lastname
);
3557 oprintf (fli2
->f
, "};\n\n");
3560 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
3561 if (fli2
->started_p
)
3563 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
3566 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
3569 oprintf (base_files
[fnum
],
3570 "extern const struct %s gt_%s_", tname
, pfx
);
3571 put_mangled_filename (base_files
[fnum
], fli2
->file
);
3572 oprintf (base_files
[fnum
], "[];\n");
3578 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
3579 oprintf (base_files
[fnum
],
3580 "EXPORTED_CONST struct %s * const %s[] = {\n", tname
, name
);
3584 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
3585 if (fli2
->started_p
)
3587 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
3590 fli2
->started_p
= 0;
3592 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
3595 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
3596 put_mangled_filename (base_files
[fnum
], fli2
->file
);
3597 oprintf (base_files
[fnum
], ",\n");
3603 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
3605 oprintf (base_files
[fnum
], " NULL\n");
3606 oprintf (base_files
[fnum
], "};\n");
3611 /* Write the first three fields (pointer, count and stride) for
3612 root NAME to F. V and LINE are as for write_root.
3614 Return true if the entry could be written; return false on error. */
3617 start_root_entry (outf_p f
, pair_p v
, const char *name
, struct fileloc
*line
)
3623 error_at_line (line
, "`%s' is too complex to be a root", name
);
3627 oprintf (f
, " {\n");
3628 oprintf (f
, " &%s,\n", name
);
3631 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
3633 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
3634 else if (ap
== v
->type
)
3635 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
3637 oprintf (f
, " sizeof (%s", v
->name
);
3638 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
3640 oprintf (f
, "),\n");
3644 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
3645 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
3649 write_field_root (outf_p f
, pair_p v
, type_p type
, const char *name
,
3650 int has_length
, struct fileloc
*line
, const char *if_marked
,
3651 bool emit_pch
, type_p field_type
, const char *field_name
)
3653 /* If the field reference is relative to V, rather than to some
3654 subcomponent of V, we can mark any subarrays with a single stride.
3655 We're effectively treating the field as a global variable in its
3657 if (v
&& type
== v
->type
)
3662 newv
.type
= field_type
;
3663 newv
.name
= ACONCAT ((v
->name
, ".", field_name
, NULL
));
3666 /* Otherwise, any arrays nested in the structure are too complex to
3668 else if (field_type
->kind
== TYPE_ARRAY
)
3670 write_root (f
, v
, field_type
, ACONCAT ((name
, ".", field_name
, NULL
)),
3671 has_length
, line
, if_marked
, emit_pch
);
3674 /* Write out to F the table entry and any marker routines needed to
3675 mark NAME as TYPE. V can be one of three values:
3677 - null, if NAME is too complex to represent using a single
3678 count and stride. In this case, it is an error for NAME to
3679 contain any gc-ed data.
3681 - the outermost array that contains NAME, if NAME is part of an array.
3683 - the C variable that contains NAME, if NAME is not part of an array.
3685 LINE is the line of the C source that declares the root variable.
3686 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
3687 is nonzero iff we are building the root table for hash table caches. */
3690 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
3691 struct fileloc
*line
, const char *if_marked
, bool emit_pch
)
3698 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
3701 const char *desc
= NULL
;
3704 for (o
= fld
->opt
; o
; o
= o
->next
)
3705 if (strcmp (o
->name
, "skip") == 0)
3707 else if (strcmp (o
->name
, "desc") == 0
3708 && o
->kind
== OPTION_STRING
)
3709 desc
= o
->info
.string
;
3710 else if (strcmp (o
->name
, "param_is") == 0)
3713 error_at_line (line
,
3714 "field `%s' of global `%s' has unknown option `%s'",
3715 fld
->name
, name
, o
->name
);
3719 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
3721 pair_p validf
= NULL
;
3724 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
3726 const char *tag
= NULL
;
3728 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
3729 if (strcmp (oo
->name
, "tag") == 0
3730 && oo
->kind
== OPTION_STRING
)
3731 tag
= oo
->info
.string
;
3732 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
3735 error_at_line (line
,
3736 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
3737 name
, fld
->name
, validf
->name
,
3738 name
, fld
->name
, ufld
->name
, tag
);
3742 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
3743 emit_pch
, validf
->type
,
3744 ACONCAT ((fld
->name
, ".",
3745 validf
->name
, NULL
)));
3748 error_at_line (line
,
3749 "global `%s.%s' has `desc' option but is not union",
3752 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
3753 emit_pch
, fld
->type
, fld
->name
);
3761 newname
= xasprintf ("%s[0]", name
);
3762 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, if_marked
,
3772 if (!start_root_entry (f
, v
, name
, line
))
3777 if (!has_length
&& UNION_OR_STRUCT_P (tp
))
3779 oprintf (f
, " >_ggc_mx_%s,\n", tp
->u
.s
.tag
);
3781 oprintf (f
, " >_pch_nx_%s", tp
->u
.s
.tag
);
3783 oprintf (f
, " NULL");
3785 else if (!has_length
&& tp
->kind
== TYPE_PARAM_STRUCT
)
3787 oprintf (f
, " >_ggc_m_");
3788 output_mangled_typename (f
, tp
);
3791 oprintf (f
, ",\n >_pch_n_");
3792 output_mangled_typename (f
, tp
);
3795 oprintf (f
, ",\n NULL");
3798 && (tp
->kind
== TYPE_POINTER
|| UNION_OR_STRUCT_P (tp
)))
3800 oprintf (f
, " >_ggc_ma_%s,\n", name
);
3802 oprintf (f
, " >_pch_na_%s", name
);
3804 oprintf (f
, " NULL");
3808 error_at_line (line
,
3809 "global `%s' is pointer to unimplemented type",
3813 oprintf (f
, ",\n &%s", if_marked
);
3814 oprintf (f
, "\n },\n");
3820 if (!start_root_entry (f
, v
, name
, line
))
3823 oprintf (f
, " (gt_pointer_walker) >_ggc_m_S,\n");
3824 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
3825 oprintf (f
, " },\n");
3833 error_at_line (line
, "global `%s' is unimplemented type", name
);
3837 /* This generates a routine to walk an array. */
3840 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
3842 struct walk_type_data d
;
3845 memset (&d
, 0, sizeof (d
));
3851 d
.bitmap
= get_lang_bitmap (v
->line
.file
);
3854 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
3856 if (wtd
->param_prefix
)
3858 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
3859 oprintf (f
, " (void *, void *, gt_pointer_operator, void *);\n");
3860 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
3861 wtd
->param_prefix
, v
->name
);
3863 " ATTRIBUTE_UNUSED void *x_p,\n"
3864 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3865 " ATTRIBUTE_UNUSED void * cookie)\n");
3866 oprintf (d
.of
, "{\n");
3867 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
3868 d
.process_field
= write_types_local_process_field
;
3869 walk_type (v
->type
, &d
);
3870 oprintf (f
, "}\n\n");
3874 oprintf (f
, "static void gt_%sa_%s (void *);\n", wtd
->prefix
, v
->name
);
3875 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
3876 wtd
->prefix
, v
->name
);
3878 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
3879 d
.process_field
= write_types_process_field
;
3880 walk_type (v
->type
, &d
);
3882 oprintf (f
, "}\n\n");
3885 /* Output a table describing the locations and types of VARIABLES. */
3888 write_roots (pair_p variables
, bool emit_pch
)
3891 struct flist
*flp
= NULL
;
3893 for (v
= variables
; v
; v
= v
->next
)
3896 get_output_file_with_visibility (CONST_CAST (input_file
*,
3899 const char *length
= NULL
;
3900 int deletable_p
= 0;
3902 for (o
= v
->opt
; o
; o
= o
->next
)
3903 if (strcmp (o
->name
, "length") == 0
3904 && o
->kind
== OPTION_STRING
)
3905 length
= o
->info
.string
;
3906 else if (strcmp (o
->name
, "deletable") == 0)
3908 else if (strcmp (o
->name
, "param_is") == 0)
3910 else if (strncmp (o
->name
, "param", 5) == 0
3911 && ISDIGIT (o
->name
[5]) && strcmp (o
->name
+ 6, "_is") == 0)
3913 else if (strcmp (o
->name
, "if_marked") == 0)
3916 error_at_line (&v
->line
,
3917 "global `%s' has unknown option `%s'",
3920 for (fli
= flp
; fli
; fli
= fli
->next
)
3921 if (fli
->f
== f
&& f
)
3925 fli
= XNEW (struct flist
);
3929 fli
->file
= v
->line
.file
;
3930 gcc_assert (fli
->file
);
3933 oprintf (f
, "\n/* GC roots. */\n\n");
3938 && v
->type
->kind
== TYPE_POINTER
3939 && (v
->type
->u
.p
->kind
== TYPE_POINTER
3940 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
3942 write_array (f
, v
, &ggc_wtd
);
3943 write_array (f
, v
, &pch_wtd
);
3947 for (v
= variables
; v
; v
= v
->next
)
3949 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
3956 for (o
= v
->opt
; o
; o
= o
->next
)
3957 if (strcmp (o
->name
, "length") == 0)
3959 else if (strcmp (o
->name
, "deletable") == 0
3960 || strcmp (o
->name
, "if_marked") == 0)
3966 for (fli
= flp
; fli
; fli
= fli
->next
)
3969 if (!fli
->started_p
)
3973 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
3974 put_mangled_filename (f
, v
->line
.file
);
3975 oprintf (f
, "[] = {\n");
3978 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
3981 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3984 for (v
= variables
; v
; v
= v
->next
)
3986 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
3992 for (o
= v
->opt
; o
; o
= o
->next
)
3993 if (strcmp (o
->name
, "deletable") == 0)
3995 else if (strcmp (o
->name
, "if_marked") == 0)
4001 for (fli
= flp
; fli
; fli
= fli
->next
)
4004 if (!fli
->started_p
)
4008 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4009 put_mangled_filename (f
, v
->line
.file
);
4010 oprintf (f
, "[] = {\n");
4013 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4017 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4018 "gt_ggc_deletable_rtab");
4020 for (v
= variables
; v
; v
= v
->next
)
4022 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4025 const char *if_marked
= NULL
;
4029 for (o
= v
->opt
; o
; o
= o
->next
)
4030 if (strcmp (o
->name
, "length") == 0)
4032 else if (strcmp (o
->name
, "if_marked") == 0
4033 && o
->kind
== OPTION_STRING
)
4034 if_marked
= o
->info
.string
;
4035 if (if_marked
== NULL
)
4037 if (v
->type
->kind
!= TYPE_POINTER
4038 || v
->type
->u
.p
->kind
!= TYPE_PARAM_STRUCT
4039 || v
->type
->u
.p
->u
.param_struct
.stru
!= find_structure ("htab", 0))
4041 error_at_line (&v
->line
,
4042 "if_marked option used but not hash table");
4046 for (fli
= flp
; fli
; fli
= fli
->next
)
4049 if (!fli
->started_p
)
4053 oprintf (f
, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4054 put_mangled_filename (f
, v
->line
.file
);
4055 oprintf (f
, "[] = {\n");
4058 write_root (f
, v
, v
->type
->u
.p
->u
.param_struct
.param
[0],
4059 v
->name
, length_p
, &v
->line
, if_marked
, emit_pch
);
4062 finish_root_table (flp
, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4063 "gt_ggc_cache_rtab");
4068 for (v
= variables
; v
; v
= v
->next
)
4070 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4074 int if_marked_p
= 0;
4077 for (o
= v
->opt
; o
; o
= o
->next
)
4078 if (strcmp (o
->name
, "length") == 0)
4080 else if (strcmp (o
->name
, "if_marked") == 0)
4086 for (fli
= flp
; fli
; fli
= fli
->next
)
4089 if (!fli
->started_p
)
4093 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4094 put_mangled_filename (f
, v
->line
.file
);
4095 oprintf (f
, "[] = {\n");
4098 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
4101 finish_root_table (flp
, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4102 "gt_pch_cache_rtab");
4104 for (v
= variables
; v
; v
= v
->next
)
4106 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4112 for (o
= v
->opt
; o
; o
= o
->next
)
4113 if (strcmp (o
->name
, "deletable") == 0
4114 || strcmp (o
->name
, "if_marked") == 0)
4120 if (!contains_scalar_p (v
->type
))
4123 for (fli
= flp
; fli
; fli
= fli
->next
)
4126 if (!fli
->started_p
)
4130 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4131 put_mangled_filename (f
, v
->line
.file
);
4132 oprintf (f
, "[] = {\n");
4135 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4139 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4140 "gt_pch_scalar_rtab");
4142 /* Record the definition of the vec_prefix structure, as defined in vec.h:
4144 struct vec_prefix GTY(()) {
4149 vec_prefix_type (void)
4151 static type_p prefix_type
= NULL
;
4152 if (prefix_type
== NULL
)
4155 static struct fileloc pos
= { NULL
, 0 };
4156 type_p len_ty
= create_scalar_type ("unsigned");
4157 pos
.file
= input_file_by_name (__FILE__
); pos
.line
= __LINE__
;
4158 fields
= create_field_at (0, len_ty
, "alloc", 0, &pos
);
4159 fields
= create_field_at (fields
, len_ty
, "num", 0, &pos
);
4160 prefix_type
= new_structure ("vec_prefix", 0, &pos
, fields
, 0);
4161 prefix_type
->u
.s
.bitmap
= -1;
4166 /* Record the definition of a generic VEC structure, as if we had expanded
4167 the macros in vec.h:
4169 typedef struct VEC_<type>_base GTY(()) {
4170 struct vec_prefix prefix;
4171 <type> GTY((length ("%h.prefix.num"))) vec[1];
4174 where the GTY(()) tags are only present if is_scalar is _false_. */
4177 note_def_vec (const char *type_name
, bool is_scalar
, struct fileloc
*pos
)
4182 const char *name
= concat ("VEC_", type_name
, "_base", (char *) 0);
4186 t
= create_scalar_type (type_name
);
4191 t
= resolve_typedef (type_name
, pos
);
4192 o
= create_string_option (0, "length", "%h.prefix.num");
4194 /* We assemble the field list in reverse order. */
4195 fields
= create_field_at (0, create_array (t
, "1"), "vec", o
, pos
);
4196 fields
= create_field_at (fields
, vec_prefix_type (), "prefix", 0, pos
);
4198 do_typedef (name
, new_structure (name
, 0, pos
, fields
, 0), pos
);
4201 /* Record the definition of an allocation-specific VEC structure, as if
4202 we had expanded the macros in vec.h:
4204 typedef struct VEC_<type>_<astrat> {
4205 VEC_<type>_base base;
4206 } VEC_<type>_<astrat>;
4209 note_def_vec_alloc (const char *type
, const char *astrat
, struct fileloc
*pos
)
4211 const char *astratname
= concat ("VEC_", type
, "_", astrat
, (char *) 0);
4212 const char *basename
= concat ("VEC_", type
, "_base", (char *) 0);
4214 pair_p field
= create_field_at (0, resolve_typedef (basename
, pos
),
4217 do_typedef (astratname
, new_structure (astratname
, 0, pos
, field
, 0), pos
);
4220 /* Returns the specifier keyword for a string or union type S, empty string
4224 get_type_specifier (const type_p s
)
4226 if (s
->kind
== TYPE_STRUCT
|| s
->kind
== TYPE_LANG_STRUCT
)
4228 if (s
->kind
== TYPE_UNION
)
4233 /* TRUE if type S has the GTY variable_size annotation. */
4236 variable_size_p (const type_p s
)
4239 for (o
= s
->u
.s
.opt
; o
; o
= o
->next
)
4240 if (strcmp (o
->name
, "variable_size") == 0)
4248 { any_zone
, specific_zone
};
4250 /* Writes one typed allocator definition into output F for type
4251 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4252 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4253 is true, the allocator will have an extra parameter specifying
4254 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4255 vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
4256 the allocator will be zone-specific. */
4259 write_typed_alloc_def (outf_p f
,
4260 bool variable_size
, const char *type_specifier
,
4261 const char *type_name
, const char *allocator_type
,
4262 enum alloc_quantity quantity
, enum alloc_zone zone
)
4264 bool two_args
= variable_size
&& (quantity
== vector
);
4265 bool third_arg
= ((zone
== specific_zone
)
4266 && (variable_size
|| (quantity
== vector
)));
4267 gcc_assert (f
!= NULL
);
4268 oprintf (f
, "#define ggc_alloc_%s%s", allocator_type
, type_name
);
4269 oprintf (f
, "(%s%s%s%s%s) ",
4270 (variable_size
? "SIZE" : ""),
4271 (two_args
? ", " : ""),
4272 (quantity
== vector
) ? "n" : "",
4273 (third_arg
? ", " : ""), (zone
== specific_zone
) ? "z" : "");
4274 oprintf (f
, "((%s%s *)", type_specifier
, type_name
);
4275 oprintf (f
, "(ggc_internal_%salloc_stat (", allocator_type
);
4276 if (zone
== specific_zone
)
4279 oprintf (f
, "SIZE");
4281 oprintf (f
, "sizeof (%s%s)", type_specifier
, type_name
);
4282 if (quantity
== vector
)
4284 oprintf (f
, " MEM_STAT_INFO)))\n");
4287 /* Writes a typed allocator definition into output F for a struct or
4288 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4291 write_typed_struct_alloc_def (outf_p f
,
4292 const type_p s
, const char *allocator_type
,
4293 enum alloc_quantity quantity
,
4294 enum alloc_zone zone
)
4296 gcc_assert (UNION_OR_STRUCT_P (s
));
4297 write_typed_alloc_def (f
, variable_size_p (s
), get_type_specifier (s
),
4298 s
->u
.s
.tag
, allocator_type
, quantity
, zone
);
4301 /* Writes a typed allocator definition into output F for a typedef P,
4302 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4305 write_typed_typedef_alloc_def (outf_p f
,
4306 const pair_p p
, const char *allocator_type
,
4307 enum alloc_quantity quantity
,
4308 enum alloc_zone zone
)
4310 write_typed_alloc_def (f
, variable_size_p (p
->type
), "", p
->name
,
4311 allocator_type
, quantity
, zone
);
4314 /* Writes typed allocator definitions into output F for the types in
4315 STRUCTURES and TYPEDEFS that are used by GC. */
4318 write_typed_alloc_defns (outf_p f
,
4319 const type_p structures
, const pair_p typedefs
)
4324 gcc_assert (f
!= NULL
);
4326 "\n/* Allocators for known structs and unions. */\n\n");
4327 for (s
= structures
; s
; s
= s
->next
)
4329 if (!USED_BY_TYPED_GC_P (s
))
4331 gcc_assert (UNION_OR_STRUCT_P (s
));
4332 /* In plugin mode onput output ggc_alloc macro definitions
4333 relevant to plugin input files. */
4334 if (nb_plugin_files
> 0
4335 && ((s
->u
.s
.line
.file
== NULL
) || !s
->u
.s
.line
.file
->inpisplugin
))
4337 write_typed_struct_alloc_def (f
, s
, "", single
, any_zone
);
4338 write_typed_struct_alloc_def (f
, s
, "cleared_", single
, any_zone
);
4339 write_typed_struct_alloc_def (f
, s
, "vec_", vector
, any_zone
);
4340 write_typed_struct_alloc_def (f
, s
, "cleared_vec_", vector
, any_zone
);
4341 write_typed_struct_alloc_def (f
, s
, "zone_", single
, specific_zone
);
4342 write_typed_struct_alloc_def (f
, s
, "zone_cleared_", single
,
4344 write_typed_struct_alloc_def (f
, s
, "zone_vec_", vector
, specific_zone
);
4345 write_typed_struct_alloc_def (f
, s
, "zone_cleared_vec_", vector
,
4349 oprintf (f
, "\n/* Allocators for known typedefs. */\n");
4350 for (p
= typedefs
; p
; p
= p
->next
)
4353 if (!USED_BY_TYPED_GC_P (s
) || (strcmp (p
->name
, s
->u
.s
.tag
) == 0))
4355 /* In plugin mode onput output ggc_alloc macro definitions
4356 relevant to plugin input files. */
4357 if (nb_plugin_files
> 0)
4359 struct fileloc
* filoc
= type_fileloc(s
);
4360 if (!filoc
|| !filoc
->file
->inpisplugin
)
4363 write_typed_typedef_alloc_def (f
, p
, "", single
, any_zone
);
4364 write_typed_typedef_alloc_def (f
, p
, "cleared_", single
, any_zone
);
4365 write_typed_typedef_alloc_def (f
, p
, "vec_", vector
, any_zone
);
4366 write_typed_typedef_alloc_def (f
, p
, "cleared_vec_", vector
, any_zone
);
4367 write_typed_typedef_alloc_def (f
, p
, "zone_", single
, specific_zone
);
4368 write_typed_typedef_alloc_def (f
, p
, "zone_cleared_", single
,
4370 write_typed_typedef_alloc_def (f
, p
, "zone_cleared_vec_", vector
,
4375 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4376 guaranteee for somewhat increased readability. If name conflicts do happen,
4377 this funcion will have to be adjusted to be more like
4378 output_mangled_typename. */
4381 output_typename (outf_p of
, const_type_p t
)
4386 oprintf (of
, "str");
4389 oprintf (of
, "scalar");
4392 output_typename (of
, t
->u
.p
);
4396 case TYPE_LANG_STRUCT
:
4397 oprintf (of
, "%s", t
->u
.s
.tag
);
4399 case TYPE_PARAM_STRUCT
:
4402 for (i
= 0; i
< NUM_PARAM
; i
++)
4403 if (t
->u
.param_struct
.param
[i
] != NULL
)
4405 output_typename (of
, t
->u
.param_struct
.param
[i
]);
4408 output_typename (of
, t
->u
.param_struct
.stru
);
4416 /* Writes a typed GC allocator for type S that is suitable as a callback for
4417 the splay tree implementation in libiberty. */
4420 write_splay_tree_allocator_def (const_type_p s
)
4422 outf_p of
= get_output_file_with_visibility (NULL
);
4423 oprintf (of
, "void * ggc_alloc_splay_tree_");
4424 output_typename (of
, s
);
4425 oprintf (of
, " (int sz, void * nl)\n");
4426 oprintf (of
, "{\n");
4427 oprintf (of
, " return ggc_splay_alloc (");
4428 oprintf (of
, "gt_e_");
4429 output_mangled_typename (of
, s
);
4430 oprintf (of
, ", sz, nl);\n");
4431 oprintf (of
, "}\n\n");
4434 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4435 for the splay tree implementation in libiberty. */
4438 write_splay_tree_allocators (const_type_p param_structs
)
4442 oprintf (header_file
, "\n/* Splay tree callback allocators. */\n");
4443 for (s
= param_structs
; s
; s
= s
->next
)
4444 if (s
->gc_used
== GC_POINTED_TO
)
4446 oprintf (header_file
, "extern void * ggc_alloc_splay_tree_");
4447 output_typename (header_file
, s
);
4448 oprintf (header_file
, " (int, void *);\n");
4449 write_splay_tree_allocator_def (s
);
4453 static void dump_pair (int indent
, pair_p p
);
4454 static void dump_type (int indent
, type_p p
);
4455 static void dump_type_list (int indent
, type_p p
);
4459 /* Dumps the value of typekind KIND. */
4462 dump_typekind (int indent
, enum typekind kind
)
4464 printf ("%*ckind = ", indent
, ' ');
4468 printf ("TYPE_SCALAR");
4471 printf ("TYPE_STRING");
4474 printf ("TYPE_STRUCT");
4477 printf ("TYPE_UNION");
4480 printf ("TYPE_POINTER");
4483 printf ("TYPE_ARRAY");
4485 case TYPE_LANG_STRUCT
:
4486 printf ("TYPE_LANG_STRUCT");
4488 case TYPE_PARAM_STRUCT
:
4489 printf ("TYPE_PARAM_STRUCT");
4497 /* Dumps the value of GC_USED flag. */
4500 dump_gc_used (int indent
, enum gc_used_enum gc_used
)
4502 printf ("%*cgc_used = ", indent
, ' ');
4506 printf ("GC_UNUSED");
4511 case GC_MAYBE_POINTED_TO
:
4512 printf ("GC_MAYBE_POINTED_TO");
4515 printf ("GC_POINTED_TO");
4523 /* Dumps the type options OPT. */
4526 dump_options (int indent
, options_p opt
)
4529 printf ("%*coptions = ", indent
, ' ');
4536 printf ("%s:string %s ", o
->name
, o
->info
.string
);
4539 printf ("%s:type ", o
->name
);
4540 dump_type (indent
+1, o
->info
.type
);
4543 printf ("%s:nested ", o
->name
);
4553 /* Dumps the source file location in LINE. */
4556 dump_fileloc (int indent
, struct fileloc line
)
4558 printf ("%*cfileloc: file = %s, line = %d\n", indent
, ' ',
4559 get_input_file_name (line
.file
),
4563 /* Recursively dumps the struct, union, or a language-specific
4567 dump_type_u_s (int indent
, type_p t
)
4571 gcc_assert (t
->kind
== TYPE_STRUCT
|| t
->kind
== TYPE_UNION
4572 || t
->kind
== TYPE_LANG_STRUCT
);
4573 printf ("%*cu.s.tag = %s\n", indent
, ' ', t
->u
.s
.tag
);
4574 dump_fileloc (indent
, t
->u
.s
.line
);
4575 printf ("%*cu.s.fields =\n", indent
, ' ');
4576 fields
= t
->u
.s
.fields
;
4579 dump_pair (indent
+ INDENT
, fields
);
4580 fields
= fields
->next
;
4582 printf ("%*cend of fields of type %p\n", indent
, ' ', (void *) t
);
4583 dump_options (indent
, t
->u
.s
.opt
);
4584 printf ("%*cu.s.bitmap = %X\n", indent
, ' ', t
->u
.s
.bitmap
);
4585 if (t
->kind
== TYPE_LANG_STRUCT
)
4587 printf ("%*cu.s.lang_struct:\n", indent
, ' ');
4588 dump_type_list (indent
+ INDENT
, t
->u
.s
.lang_struct
);
4592 /* Recursively dumps the array T. */
4595 dump_type_u_a (int indent
, type_p t
)
4597 gcc_assert (t
->kind
== TYPE_ARRAY
);
4598 printf ("%*clen = %s, u.a.p:\n", indent
, ' ', t
->u
.a
.len
);
4599 dump_type_list (indent
+ INDENT
, t
->u
.a
.p
);
4602 /* Recursively dumps the parameterized struct T. */
4605 dump_type_u_param_struct (int indent
, type_p t
)
4608 gcc_assert (t
->kind
== TYPE_PARAM_STRUCT
);
4609 printf ("%*cu.param_struct.stru:\n", indent
, ' ');
4610 dump_type_list (indent
, t
->u
.param_struct
.stru
);
4611 dump_fileloc (indent
, t
->u
.param_struct
.line
);
4612 for (i
= 0; i
< NUM_PARAM
; i
++)
4614 if (t
->u
.param_struct
.param
[i
] == NULL
)
4616 printf ("%*cu.param_struct.param[%d]:\n", indent
, ' ', i
);
4617 dump_type (indent
+ INDENT
, t
->u
.param_struct
.param
[i
]);
4621 /* Recursively dumps the type list T. */
4624 dump_type_list (int indent
, type_p t
)
4629 dump_type (indent
, p
);
4634 static htab_t seen_types
;
4636 /* Recursively dumps the type T if it was not dumped previously. */
4639 dump_type (int indent
, type_p t
)
4643 printf ("%*cType at %p: ", indent
, ' ', (void *) t
);
4644 slot
= htab_find_slot (seen_types
, t
, INSERT
);
4647 printf ("already seen.\n");
4653 dump_typekind (indent
, t
->kind
);
4654 printf ("%*cpointer_to = %p\n", indent
+ INDENT
, ' ',
4655 (void *) t
->pointer_to
);
4656 dump_gc_used (indent
+ INDENT
, t
->gc_used
);
4660 printf ("%*cscalar_is_char = %s\n", indent
+ INDENT
, ' ',
4661 t
->u
.scalar_is_char
? "true" : "false");
4667 case TYPE_LANG_STRUCT
:
4668 dump_type_u_s (indent
+ INDENT
, t
);
4671 printf ("%*cp:\n", indent
+ INDENT
, ' ');
4672 dump_type (indent
+ INDENT
, t
->u
.p
);
4675 dump_type_u_a (indent
+ INDENT
, t
);
4677 case TYPE_PARAM_STRUCT
:
4678 dump_type_u_param_struct (indent
+ INDENT
, t
);
4683 printf ("%*cEnd of type at %p\n", indent
, ' ', (void *) t
);
4686 /* Dumps the pair P. */
4689 dump_pair (int indent
, pair_p p
)
4691 printf ("%*cpair: name = %s\n", indent
, ' ', p
->name
);
4692 dump_type (indent
, p
->type
);
4693 dump_fileloc (indent
, p
->line
);
4694 dump_options (indent
, p
->opt
);
4695 printf ("%*cEnd of pair %s\n", indent
, ' ', p
->name
);
4698 /* Dumps the list of pairs PP. */
4701 dump_pair_list (const char *name
, pair_p pp
)
4704 printf ("%s:\n", name
);
4705 for (p
= pp
; p
!= NULL
; p
= p
->next
)
4707 printf ("End of %s\n\n", name
);
4710 /* Dumps the STRUCTURES. */
4713 dump_structures (const char *name
, type_p structures
)
4715 printf ("%s:\n", name
);
4716 dump_type_list (0, structures
);
4717 printf ("End of %s\n\n", name
);
4720 /* Dumps the internal structures of gengtype. This is useful to debug
4721 gengtype itself, or to understand what it does, e.g. for plugin
4725 dump_everything (void)
4727 seen_types
= htab_create (100, htab_hash_pointer
, htab_eq_pointer
, NULL
);
4728 dump_pair_list ("typedefs", typedefs
);
4729 dump_structures ("structures", structures
);
4730 dump_structures ("param_structs", param_structs
);
4731 dump_pair_list ("variables", variables
);
4732 htab_delete (seen_types
);
4737 /* Option specification for getopt_long. */
4738 static const struct option gengtype_long_options
[] = {
4739 {"help", no_argument
, NULL
, 'h'},
4740 {"version", no_argument
, NULL
, 'V'},
4741 {"verbose", no_argument
, NULL
, 'v'},
4742 {"dump", no_argument
, NULL
, 'd'},
4743 {"debug", no_argument
, NULL
, 'D'},
4744 {"plugin", required_argument
, NULL
, 'P'},
4745 {"srcdir", required_argument
, NULL
, 'S'},
4746 {"backupdir", required_argument
, NULL
, 'B'},
4747 {"inputs", required_argument
, NULL
, 'I'},
4748 {"read-state", required_argument
, NULL
, 'r'},
4749 {"write-state", required_argument
, NULL
, 'w'},
4750 /* Terminating NULL placeholder. */
4751 {NULL
, no_argument
, NULL
, 0},
4758 printf ("Usage: %s\n", progname
);
4759 printf ("\t -h | --help " " \t# Give this help.\n");
4760 printf ("\t -D | --debug "
4761 " \t# Give debug output to debug %s itself.\n", progname
);
4762 printf ("\t -V | --version " " \t# Give version information.\n");
4763 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
4764 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4765 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4766 " \t# Generate for plugin.\n");
4767 printf ("\t -S | --srcdir <GCC-directory> "
4768 " \t# Specify the GCC source directory.\n");
4769 printf ("\t -B | --backupdir <directory> "
4770 " \t# Specify the backup directory for updated files.\n");
4771 printf ("\t -I | --inputs <input-list> "
4772 " \t# Specify the file with source files list.\n");
4773 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
4774 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
4778 print_version (void)
4780 printf ("%s %s%s\n", progname
, pkgversion_string
, version_string
);
4781 printf ("Report bugs: %s\n", bug_report_url
);
4784 /* Parse the program options using getopt_long... */
4786 parse_program_options (int argc
, char **argv
)
4789 while ((opt
= getopt_long (argc
, argv
, "hVvdP:S:B:I:w:r:D",
4790 gengtype_long_options
, NULL
)) >= 0)
4794 case 'h': /* --help */
4797 case 'V': /* --version */
4800 case 'd': /* --dump */
4803 case 'D': /* --debug */
4806 case 'v': /* --verbose */
4809 case 'P': /* --plugin */
4811 plugin_output_filename
= optarg
;
4813 fatal ("missing plugin output file name");
4815 case 'S': /* --srcdir */
4819 fatal ("missing source directory");
4820 srcdir_len
= strlen (srcdir
);
4822 case 'B': /* --backupdir */
4824 backup_dir
= optarg
;
4826 fatal ("missing backup directory");
4828 case 'I': /* --inputs */
4832 fatal ("missing input list");
4834 case 'r': /* --read-state */
4836 read_state_filename
= optarg
;
4838 fatal ("missing read state file");
4839 DBGPRINTF ("read state %s\n", optarg
);
4841 case 'w': /* --write-state */
4842 DBGPRINTF ("write state %s\n", optarg
);
4844 write_state_filename
= optarg
;
4846 fatal ("missing write state file");
4849 fprintf (stderr
, "%s: unknown flag '%c'\n", progname
, opt
);
4851 fatal ("unexpected flag");
4854 if (plugin_output_filename
)
4856 /* In plugin mode we require some input files. */
4859 fatal ("no source files given in plugin mode");
4860 nb_plugin_files
= argc
- optind
;
4861 plugin_files
= XNEWVEC (input_file
*, nb_plugin_files
);
4862 for (i
= 0; i
< (int) nb_plugin_files
; i
++)
4864 char *name
= argv
[i
+ optind
];
4865 plugin_files
[i
] = input_file_by_name (name
);
4872 /******* Manage input files. ******/
4874 /* Hash table of unique input file names. */
4875 static htab_t input_file_htab
;
4877 /* Find or allocate a new input_file by hash-consing it. */
4879 input_file_by_name (const char* name
)
4882 input_file
* f
= NULL
;
4886 namlen
= strlen (name
);
4887 f
= XCNEWVAR (input_file
, sizeof (input_file
)+namlen
+2);
4890 f
->inpisplugin
= false;
4891 strcpy (f
->inpname
, name
);
4892 slot
= htab_find_slot (input_file_htab
, f
, INSERT
);
4893 gcc_assert (slot
!= NULL
);
4896 /* Already known input file. */
4898 return (input_file
*)(*slot
);
4900 /* New input file. */
4905 /* Hash table support routines for input_file-s. */
4907 htab_hash_inputfile (const void *p
)
4909 const input_file
*inpf
= (const input_file
*) p
;
4911 return htab_hash_string (get_input_file_name (inpf
));
4915 htab_eq_inputfile (const void *x
, const void *y
)
4917 const input_file
*inpfx
= (const input_file
*) x
;
4918 const input_file
*inpfy
= (const input_file
*) y
;
4919 gcc_assert (inpfx
!= NULL
&& inpfy
!= NULL
);
4920 return !filename_cmp (get_input_file_name (inpfx
), get_input_file_name (inpfy
));
4925 main (int argc
, char **argv
)
4928 static struct fileloc pos
= { NULL
, 0 };
4929 outf_p output_header
;
4931 /* Mandatory common initializations. */
4932 progname
= "gengtype"; /* For fatal and messages. */
4933 /* Create the hash-table used to hash-cons input files. */
4935 htab_create (800, htab_hash_inputfile
, htab_eq_inputfile
, NULL
);
4936 /* Initialize our special input files. */
4937 this_file
= input_file_by_name (__FILE__
);
4938 system_h_file
= input_file_by_name ("system.h");
4939 /* Set the scalar_is_char union number for predefined scalar types. */
4940 scalar_nonchar
.u
.scalar_is_char
= FALSE
;
4941 scalar_char
.u
.scalar_is_char
= TRUE
;
4943 parse_program_options (argc
, argv
);
4948 time_t now
= (time_t) 0;
4950 DBGPRINTF ("gengtype started pid %d at %s",
4951 (int) getpid (), ctime (&now
));
4953 #endif /* ENABLE_CHECKING */
4955 /* Parse the input list and the input files. */
4956 DBGPRINTF ("inputlist %s", inputlist
);
4957 if (read_state_filename
)
4960 fatal ("input list %s cannot be given with a read state file %s",
4961 inputlist
, read_state_filename
);
4962 read_state (read_state_filename
);
4963 DBGPRINT_COUNT_TYPE ("structures after read_state", structures
);
4964 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs
);
4968 /* These types are set up with #define or else outside of where
4969 we can see them. We should initialize them before calling
4971 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
4973 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos
));
4974 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos
));
4975 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos
));
4976 POS_HERE (do_scalar_typedef ("double_int", &pos
));
4977 POS_HERE (do_scalar_typedef ("uint64_t", &pos
));
4978 POS_HERE (do_scalar_typedef ("uint8", &pos
));
4979 POS_HERE (do_scalar_typedef ("jword", &pos
));
4980 POS_HERE (do_scalar_typedef ("JCF_u2", &pos
));
4981 POS_HERE (do_scalar_typedef ("void", &pos
));
4982 POS_HERE (do_typedef ("PTR",
4983 create_pointer (resolve_typedef ("void", &pos
)),
4986 read_input_list (inputlist
);
4987 for (i
= 0; i
< num_gt_files
; i
++)
4989 parse_file (get_input_file_name (gt_files
[i
]));
4990 DBGPRINTF ("parsed file #%d %s",
4991 (int) i
, get_input_file_name (gt_files
[i
]));
4993 if (verbosity_level
>= 1)
4994 printf ("%s parsed %d files with %d GTY types\n",
4995 progname
, (int) num_gt_files
, type_count
);
4997 DBGPRINT_COUNT_TYPE ("structures after parsing", structures
);
4998 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs
);
5002 fatal ("either an input list or a read state file should be given");
5007 if (plugin_output_filename
)
5010 /* In plugin mode, we should have read a state file, and have
5011 given at least one plugin file. */
5012 if (!read_state_filename
)
5013 fatal ("No read state given in plugin mode for %s",
5014 plugin_output_filename
);
5016 if (nb_plugin_files
== 0 || !plugin_files
)
5017 fatal ("No plugin files given in plugin mode for %s",
5018 plugin_output_filename
);
5020 /* Parse our plugin files and augment the state. */
5021 for (ix
= 0; ix
< nb_plugin_files
; ix
++)
5023 input_file
* pluginput
= plugin_files
[ix
];
5024 pluginput
->inpisplugin
= true;
5025 parse_file (get_input_file_name (pluginput
));
5030 plugin_output
= create_file ("GCC", plugin_output_filename
);
5031 DBGPRINTF ("created plugin_output %p named %s",
5032 (void *) plugin_output
, plugin_output
->name
);
5035 { /* No plugin files, we are in normal mode. */
5037 fatal ("gengtype needs a source directory in normal mode");
5044 /* The call to set_gc_used may indirectly call find_param_structure
5045 hence enlarge the param_structs list of types. */
5046 set_gc_used (variables
);
5048 /* The state at this point is read from the state input file or by
5049 parsing source files and optionally augmented by parsing plugin
5050 source files. Write it now. */
5051 if (write_state_filename
)
5053 DBGPRINT_COUNT_TYPE ("structures before write_state", structures
);
5054 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs
);
5057 fatal ("didn't write state file %s after errors",
5058 write_state_filename
);
5060 DBGPRINTF ("before write_state %s", write_state_filename
);
5061 write_state (write_state_filename
);
5066 /* After having written the state file we return immediately to
5067 avoid generating any output file. */
5077 write_enum_defn (structures
, param_structs
);
5078 output_header
= plugin_output
? plugin_output
: header_file
;
5079 write_typed_alloc_defns (output_header
, structures
, typedefs
);
5080 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5082 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5085 write_types (output_header
, structures
, param_structs
, &ggc_wtd
);
5086 if (plugin_files
== NULL
)
5088 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5090 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5092 write_types (header_file
, structures
, param_structs
, &pch_wtd
);
5093 write_local (header_file
, structures
, param_structs
);
5095 write_splay_tree_allocators (param_structs
);
5096 write_roots (variables
, plugin_files
== NULL
);
5098 close_output_files ();
5103 /* Don't bother about free-ing any input or plugin file, etc. */