annotate gcc/gencfn-macros.c @ 16:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
kono
parents:
diff changeset
1 /* Generate macros based on the combined_fn enum.
kono
parents:
diff changeset
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 /* Automatically generate code fragments related to combined_fn.
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 The program looks for math built-in functions that have float, double
kono
parents:
diff changeset
23 and long double variants, such as {sqrtf, sqrt, sqrtl}, and that may
kono
parents:
diff changeset
24 or may not have an associated internal function as well. It also looks
kono
parents:
diff changeset
25 for integer built-in functions that have int, long, long long and
kono
parents:
diff changeset
26 intmax_t variants, such as {clz, clzl, clzll, clzimax}, and that
kono
parents:
diff changeset
27 again may or may not have an associated internal function as well.
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 When run with -c, the generator prints a list of macros such as:
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 CASE_CFN_SQRT
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 for each group of functions described above, with 'case CFN_*'
kono
parents:
diff changeset
34 statements for each built-in and internal function in the group.
kono
parents:
diff changeset
35 For example, there are both built-in and internal implementations
kono
parents:
diff changeset
36 of SQRT, so "CASE_CFN_SQRT:" is equivalent to:
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 case CFN_BUILT_IN_SQRTF:
kono
parents:
diff changeset
39 case CFN_BUILT_IN_SQRT:
kono
parents:
diff changeset
40 case CFN_BUILT_IN_SQRTL:
kono
parents:
diff changeset
41 case CFN_SQRT:
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 The macros for groups with no internal function drop the last line.
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 When run with -o, the generator prints a similar list of
kono
parents:
diff changeset
46 define_operator_list directives, for use by match.pd. Each operator
kono
parents:
diff changeset
47 list starts with the built-in functions, in order of ascending type width.
kono
parents:
diff changeset
48 This is followed by an entry for the internal function, or "null" if there
kono
parents:
diff changeset
49 is no internal function for the group. For example:
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 (define_operator_list SQRT
kono
parents:
diff changeset
52 BUILT_IN_SQRTF
kono
parents:
diff changeset
53 BUILT_IN_SQRT
kono
parents:
diff changeset
54 BUILT_IN_SQRTL
kono
parents:
diff changeset
55 IFN_SQRT)
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 and:
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 (define_operator_list CABS
kono
parents:
diff changeset
60 BUILT_IN_CABSF
kono
parents:
diff changeset
61 BUILT_IN_CABS
kono
parents:
diff changeset
62 BUILT_IN_CABSL
kono
parents:
diff changeset
63 null) */
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 #include "bconfig.h"
kono
parents:
diff changeset
66 #include "system.h"
kono
parents:
diff changeset
67 #include "coretypes.h"
kono
parents:
diff changeset
68 #include "hash-table.h"
kono
parents:
diff changeset
69 #include "hash-set.h"
kono
parents:
diff changeset
70 #include "errors.h"
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 typedef hash_set <nofree_string_hash> string_set;
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 /* Add all names in null-terminated list NAMES to SET. */
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 static void
kono
parents:
diff changeset
77 add_to_set (string_set *set, const char *const *names)
kono
parents:
diff changeset
78 {
kono
parents:
diff changeset
79 for (unsigned int i = 0; names[i]; ++i)
kono
parents:
diff changeset
80 set->add (names[i]);
kono
parents:
diff changeset
81 }
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 /* Return true if *BUILTINS contains BUILT_IN_<NAME><SUFFIX> for all
kono
parents:
diff changeset
84 suffixes in null-terminated list SUFFIXES. */
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 static bool
kono
parents:
diff changeset
87 is_group (string_set *builtins, const char *name, const char *const *suffixes)
kono
parents:
diff changeset
88 {
kono
parents:
diff changeset
89 for (unsigned int i = 0; suffixes[i]; ++i)
kono
parents:
diff changeset
90 if (!builtins->contains (ACONCAT (("BUILT_IN_", name, suffixes[i], NULL))))
kono
parents:
diff changeset
91 return false;
kono
parents:
diff changeset
92 return true;
kono
parents:
diff changeset
93 }
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 /* Print a macro for all combined functions related to NAME, with the
kono
parents:
diff changeset
96 null-terminated list of suffixes in SUFFIXES. INTERNAL_P says whether
kono
parents:
diff changeset
97 CFN_<NAME> also exists. */
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 static void
kono
parents:
diff changeset
100 print_case_cfn (const char *name, bool internal_p,
kono
parents:
diff changeset
101 const char *const *suffixes)
kono
parents:
diff changeset
102 {
kono
parents:
diff changeset
103 printf ("#define CASE_CFN_%s", name);
kono
parents:
diff changeset
104 if (internal_p)
kono
parents:
diff changeset
105 printf (" \\\n case CFN_%s", name);
kono
parents:
diff changeset
106 for (unsigned int i = 0; suffixes[i]; ++i)
kono
parents:
diff changeset
107 printf ("%s \\\n case CFN_BUILT_IN_%s%s",
kono
parents:
diff changeset
108 internal_p || i > 0 ? ":" : "", name, suffixes[i]);
kono
parents:
diff changeset
109 printf ("\n");
kono
parents:
diff changeset
110 }
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 /* Print an operator list for all combined functions related to NAME,
kono
parents:
diff changeset
113 with the null-terminated list of suffixes in SUFFIXES. INTERNAL_P
kono
parents:
diff changeset
114 says whether CFN_<NAME> also exists. */
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 static void
kono
parents:
diff changeset
117 print_define_operator_list (const char *name, bool internal_p,
kono
parents:
diff changeset
118 const char *const *suffixes)
kono
parents:
diff changeset
119 {
kono
parents:
diff changeset
120 printf ("(define_operator_list %s\n", name);
kono
parents:
diff changeset
121 for (unsigned int i = 0; suffixes[i]; ++i)
kono
parents:
diff changeset
122 printf (" BUILT_IN_%s%s\n", name, suffixes[i]);
kono
parents:
diff changeset
123 if (internal_p)
kono
parents:
diff changeset
124 printf (" IFN_%s)\n", name);
kono
parents:
diff changeset
125 else
kono
parents:
diff changeset
126 printf (" null)\n");
kono
parents:
diff changeset
127 }
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 const char *const builtin_names[] = {
kono
parents:
diff changeset
130 #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
kono
parents:
diff changeset
131 #ENUM,
kono
parents:
diff changeset
132 #include "builtins.def"
kono
parents:
diff changeset
133 NULL
kono
parents:
diff changeset
134 };
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 const char *const internal_fn_flt_names[] = {
kono
parents:
diff changeset
137 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
kono
parents:
diff changeset
138 #NAME,
kono
parents:
diff changeset
139 #include "internal-fn.def"
kono
parents:
diff changeset
140 NULL
kono
parents:
diff changeset
141 };
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 const char *const internal_fn_int_names[] = {
kono
parents:
diff changeset
144 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
kono
parents:
diff changeset
145 #NAME,
kono
parents:
diff changeset
146 #include "internal-fn.def"
kono
parents:
diff changeset
147 NULL
kono
parents:
diff changeset
148 };
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 static const char *const flt_suffixes[] = { "F", "", "L", NULL };
kono
parents:
diff changeset
151 static const char *const int_suffixes[] = { "", "L", "LL", "IMAX", NULL };
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 static const char *const *const suffix_lists[] = {
kono
parents:
diff changeset
154 flt_suffixes,
kono
parents:
diff changeset
155 int_suffixes,
kono
parents:
diff changeset
156 NULL
kono
parents:
diff changeset
157 };
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 int
kono
parents:
diff changeset
160 main (int argc, char **argv)
kono
parents:
diff changeset
161 {
kono
parents:
diff changeset
162 /* Check arguments. */
kono
parents:
diff changeset
163 progname = argv[0];
kono
parents:
diff changeset
164 if (argc != 2
kono
parents:
diff changeset
165 || argv[1][0] != '-'
kono
parents:
diff changeset
166 || !strchr ("co", argv[1][1])
kono
parents:
diff changeset
167 || argv[1][2])
kono
parents:
diff changeset
168 fatal ("usage: %s [-c|-o] > file", progname);
kono
parents:
diff changeset
169 int type = argv[1][1];
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 /* Collect the set of built-in and internal functions. */
kono
parents:
diff changeset
172 string_set builtins;
kono
parents:
diff changeset
173 string_set internal_fns;
kono
parents:
diff changeset
174 add_to_set (&builtins, builtin_names);
kono
parents:
diff changeset
175 add_to_set (&internal_fns, internal_fn_flt_names);
kono
parents:
diff changeset
176 add_to_set (&internal_fns, internal_fn_int_names);
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 /* Check the functions. */
kono
parents:
diff changeset
179 for (unsigned int i = 0; internal_fn_flt_names[i]; ++i)
kono
parents:
diff changeset
180 {
kono
parents:
diff changeset
181 const char *name = internal_fn_flt_names[i];
kono
parents:
diff changeset
182 if (!is_group (&builtins, name, flt_suffixes))
kono
parents:
diff changeset
183 error ("DEF_INTERNAL_FLT_FN (%s) has no associated built-in"
kono
parents:
diff changeset
184 " functions", name);
kono
parents:
diff changeset
185 }
kono
parents:
diff changeset
186 for (unsigned int i = 0; internal_fn_int_names[i]; ++i)
kono
parents:
diff changeset
187 {
kono
parents:
diff changeset
188 const char *name = internal_fn_int_names[i];
kono
parents:
diff changeset
189 if (!is_group (&builtins, name, int_suffixes))
kono
parents:
diff changeset
190 error ("DEF_INTERNAL_INT_FN (%s) has no associated built-in"
kono
parents:
diff changeset
191 " functions", name);
kono
parents:
diff changeset
192 }
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 /* Go through the built-in functions in declaration order, outputting
kono
parents:
diff changeset
195 definitions as appropriate. */
kono
parents:
diff changeset
196 for (unsigned int i = 0; builtin_names[i]; ++i)
kono
parents:
diff changeset
197 {
kono
parents:
diff changeset
198 const char *name = builtin_names[i];
kono
parents:
diff changeset
199 if (strncmp (name, "BUILT_IN_", 9) == 0)
kono
parents:
diff changeset
200 {
kono
parents:
diff changeset
201 const char *root = name + 9;
kono
parents:
diff changeset
202 for (unsigned int j = 0; suffix_lists[j]; ++j)
kono
parents:
diff changeset
203 if (is_group (&builtins, root, suffix_lists[j]))
kono
parents:
diff changeset
204 {
kono
parents:
diff changeset
205 bool internal_p = internal_fns.contains (root);
kono
parents:
diff changeset
206 if (type == 'c')
kono
parents:
diff changeset
207 print_case_cfn (root, internal_p, suffix_lists[j]);
kono
parents:
diff changeset
208 else
kono
parents:
diff changeset
209 print_define_operator_list (root, internal_p,
kono
parents:
diff changeset
210 suffix_lists[j]);
kono
parents:
diff changeset
211 }
kono
parents:
diff changeset
212 }
kono
parents:
diff changeset
213 }
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 if (fflush (stdout) || fclose (stdout) || have_error)
kono
parents:
diff changeset
216 return FATAL_EXIT_CODE;
kono
parents:
diff changeset
217 return SUCCESS_EXIT_CODE;
kono
parents:
diff changeset
218 }