re PR bootstrap/54281 (Fails to bootstrap with --disable-nls)
[official-gcc.git] / gcc / optc-gen.awk
blob0abe6bcf2a8e67090a210d600f3d25bdef231d34
1 # Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011, 2012
2 # Free Software Foundation, Inc.
3 # Contributed by Kelley Cook, June 2004.
4 # Original code from Neil Booth, May 2003.
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 3, or (at your option) any
9 # later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; see the file COPYING3. If not see
18 # <http://www.gnu.org/licenses/>.
20 # This Awk script reads in the option records generated from
21 # opt-gather.awk, combines the flags of duplicate options and generates a
22 # C file.
25 # This program uses functions from opt-functions.awk and code from
26 # opt-read.awk.
28 # Usage: awk -f opt-functions.awk -f opt-read.awk -f optc-gen.awk \
29 # [-v header_name=header.h] < inputfile > options.c
31 # Dump that array of options into a C file.
32 END {
34 # Record first EnabledBy and LangEnabledBy uses.
35 n_enabledby = 0;
36 for (i = 0; i < n_langs; i++) {
37 n_enabledby_lang[i] = 0;
39 for (i = 0; i < n_opts; i++) {
40 enabledby_arg = opt_args("EnabledBy", flags[i]);
41 if (enabledby_arg != "") {
42 enabledby_name = enabledby_arg;
43 enabledby_index = opt_numbers[enabledby_name];
44 if (enabledby_index == "") {
45 print "#error Enabledby: " enabledby_name
46 } else {
47 if (enables[enabledby_name] == "") {
48 enabledby[n_enabledby] = enabledby_name;
49 n_enabledby++;
51 enables[enabledby_name] = enables[enabledby_name] opts[i] ",";
55 enabledby_arg = opt_args("LangEnabledBy", flags[i]);
56 if (enabledby_arg != "") {
57 n_enabledby_arg_langs = split(nth_arg(0, enabledby_arg), enabledby_arg_langs, " ");
58 enabledby_name = nth_arg(1, enabledby_arg);
59 enabledby_index = opt_numbers[enabledby_name];
60 if (enabledby_index == "") {
61 print "#error Enabledby: " enabledby_name
62 } else {
63 for (j = 1; j <= n_enabledby_arg_langs; j++) {
64 lang_name = lang_sanitized_name(enabledby_arg_langs[j]);
65 lang_index = lang_numbers[enabledby_arg_langs[j]];
66 if (enables[lang_name,enabledby_name] == "") {
67 enabledby[lang_name,n_enabledby_lang[lang_index]] = enabledby_name;
68 n_enabledby_lang[lang_index]++;
70 enables[lang_name,enabledby_name] = enables[lang_name,enabledby_name] opts[i] ",";
77 print "/* This file is auto-generated by optc-gen.awk. */"
78 print ""
79 n_headers = split(header_name, headers, " ")
80 for (i = 1; i <= n_headers; i++)
81 print "#include " quote headers[i] quote
82 print "#include " quote "opts.h" quote
83 print "#include " quote "intl.h" quote
84 print "#include " quote "insn-attr-common.h" quote
85 print ""
87 if (n_extra_c_includes > 0) {
88 for (i = 0; i < n_extra_c_includes; i++) {
89 print "#include " quote extra_c_includes[i] quote
91 print ""
94 for (i = 0; i < n_enums; i++) {
95 name = enum_names[i]
96 type = enum_type[name]
97 print "static const struct cl_enum_arg cl_enum_" name \
98 "_data[] = "
99 print "{"
100 print enum_data[name] " { NULL, 0, 0 }"
101 print "};"
102 print ""
103 print "static void"
104 print "cl_enum_" name "_set (void *var, int value)"
105 print "{"
106 print " *((" type " *) var) = (" type ") value;"
107 print "}"
108 print ""
109 print "static int"
110 print "cl_enum_" name "_get (const void *var)"
111 print "{"
112 print " return (int) *((const " type " *) var);"
113 print "}"
114 print ""
117 print "const struct cl_enum cl_enums[] ="
118 print "{"
119 for (i = 0; i < n_enums; i++) {
120 name = enum_names[i]
121 ehelp = enum_help[name]
122 if (ehelp == "")
123 ehelp = "NULL"
124 else
125 ehelp = quote ehelp quote
126 unknown_error = enum_unknown_error[name]
127 if (unknown_error == "")
128 unknown_error = "NULL"
129 else
130 unknown_error = quote unknown_error quote
131 print " {"
132 print " " ehelp ","
133 print " " unknown_error ","
134 print " cl_enum_" name "_data,"
135 print " sizeof (" enum_type[name] "),"
136 print " cl_enum_" name "_set,"
137 print " cl_enum_" name "_get"
138 print " },"
140 print "};"
141 print "const unsigned int cl_enums_count = " n_enums ";"
142 print ""
144 print "const struct gcc_options global_options_init =\n{"
145 for (i = 0; i < n_extra_vars; i++) {
146 var = extra_vars[i]
147 init = extra_vars[i]
148 if (var ~ "=" ) {
149 sub(".*= *", "", init)
150 } else {
151 init = "0"
153 sub(" *=.*", "", var)
154 name = var
155 sub("^.*[ *]", "", name)
156 sub("\\[.*\\]$", "", name)
157 var_seen[name] = 1
158 print " " init ", /* " name " */"
160 for (i = 0; i < n_opts; i++) {
161 name = var_name(flags[i]);
162 if (name == "")
163 continue;
165 init = opt_args("Init", flags[i])
166 if (init != "") {
167 if (name in var_init && var_init[name] != init)
168 print "#error multiple initializers for " name
169 var_init[name] = init
172 for (i = 0; i < n_opts; i++) {
173 name = var_name(flags[i]);
174 if (name == "")
175 continue;
177 if (name in var_seen)
178 continue;
180 if (name in var_init)
181 init = var_init[name]
182 else
183 init = "0"
185 print " " init ", /* " name " */"
187 var_seen[name] = 1;
189 for (i = 0; i < n_opts; i++) {
190 name = static_var(opts[i], flags[i]);
191 if (name != "") {
192 print " 0, /* " name " (private state) */"
193 print "#undef x_" name
196 for (i = 0; i < n_opts; i++) {
197 if (flag_set_p("SetByCombined", flags[i]))
198 print " false, /* frontend_set_" var_name(flags[i]) " */"
200 print "};"
201 print ""
202 print "struct gcc_options global_options;"
203 print "struct gcc_options global_options_set;"
204 print ""
206 print "const char * const lang_names[] =\n{"
207 for (i = 0; i < n_langs; i++) {
208 macros[i] = "CL_" lang_sanitized_name(langs[i])
209 s = substr(" ", length (macros[i]))
210 print " " quote langs[i] quote ","
213 print " 0\n};\n"
214 print "const unsigned int cl_options_count = N_OPTS;\n"
215 print "#if (1U << " n_langs ") > CL_MIN_OPTION_CLASS"
216 print " #error the number of languages exceeds the implementation limit"
217 print "#endif"
218 print "const unsigned int cl_lang_count = " n_langs ";\n"
220 print "const struct cl_option cl_options[] =\n{"
222 j = 0
223 for (i = 0; i < n_opts; i++) {
224 back_chain[i] = "N_OPTS";
225 indices[opts[i]] = j;
226 # Combine the flags of identical switches. Switches
227 # appear many times if they are handled by many front
228 # ends, for example.
229 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
230 flags[i + 1] = flags[i] " " flags[i + 1];
231 if (help[i + 1] == "")
232 help[i + 1] = help[i]
233 else if (help[i] != "" && help[i + 1] != help[i])
234 print "#error Multiple different help strings for " \
235 opts[i] ":\n\t" help[i] "\n\t" help[i + 1]
237 i++;
238 back_chain[i] = "N_OPTS";
239 indices[opts[i]] = j;
241 j++;
244 for (i = 0; i < n_opts; i++) {
245 # With identical flags, pick only the last one. The
246 # earlier loop ensured that it has all flags merged,
247 # and a nonempty help text if one of the texts was nonempty.
248 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
249 i++;
252 len = length (opts[i]);
253 enum = opt_enum(opts[i])
255 # If this switch takes joined arguments, back-chain all
256 # subsequent switches to it for which it is a prefix. If
257 # a later switch S is a longer prefix of a switch T, T
258 # will be back-chained to S in a later iteration of this
259 # for() loop, which is what we want.
260 if (flag_set_p("Joined.*", flags[i])) {
261 for (j = i + 1; j < n_opts; j++) {
262 if (substr (opts[j], 1, len) != opts[i])
263 break;
264 back_chain[j] = enum;
268 s = substr(" ", length (opts[i]))
269 if (i + 1 == n_opts)
270 comma = ""
272 if (help[i] == "")
273 hlp = "0"
274 else
275 hlp = quote help[i] quote;
277 missing_arg_error = opt_args("MissingArgError", flags[i])
278 if (missing_arg_error == "")
279 missing_arg_error = "0"
280 else
281 missing_arg_error = quote missing_arg_error quote
284 warn_message = opt_args("Warn", flags[i])
285 if (warn_message == "")
286 warn_message = "0"
287 else
288 warn_message = quote warn_message quote
290 alias_arg = opt_args("Alias", flags[i])
291 if (alias_arg == "") {
292 if (flag_set_p("Ignore", flags[i]))
293 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
294 else
295 alias_data = "NULL, NULL, N_OPTS"
296 } else {
297 alias_opt = nth_arg(0, alias_arg)
298 alias_posarg = nth_arg(1, alias_arg)
299 alias_negarg = nth_arg(2, alias_arg)
301 if (var_ref(opts[i], flags[i]) != "-1")
302 print "#error Alias setting variable"
304 if (alias_posarg != "" && alias_negarg == "") {
305 if (!flag_set_p("RejectNegative", flags[i]) \
306 && opts[i] ~ "^[Wfm]")
307 print "#error Alias with single argument " \
308 "allowing negative form"
310 if (alias_posarg != "" \
311 && flag_set_p("NegativeAlias", flags[i])) {
312 print "#error Alias with multiple arguments " \
313 "used with NegativeAlias"
316 alias_opt = opt_enum(alias_opt)
317 if (alias_posarg == "")
318 alias_posarg = "NULL"
319 else
320 alias_posarg = quote alias_posarg quote
321 if (alias_negarg == "")
322 alias_negarg = "NULL"
323 else
324 alias_negarg = quote alias_negarg quote
325 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
328 neg = opt_args("Negative", flags[i]);
329 if (neg != "")
330 idx = indices[neg]
331 else {
332 if (flag_set_p("RejectNegative", flags[i]))
333 idx = -1;
334 else {
335 if (opts[i] ~ "^[Wfm]")
336 idx = indices[opts[i]];
337 else
338 idx = -1;
341 # Split the printf after %u to work around an ia64-hp-hpux11.23
342 # awk bug.
343 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
344 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
345 alias_data, back_chain[i], len)
346 printf(" %d,\n", idx)
347 condition = opt_args("Condition", flags[i])
348 cl_flags = switch_flags(flags[i])
349 cl_bit_fields = switch_bit_fields(flags[i])
350 cl_zero_bit_fields = switch_bit_fields("")
351 if (condition != "")
352 printf("#if %s\n" \
353 " %s,\n" \
354 " 0, %s,\n" \
355 "#else\n" \
356 " 0,\n" \
357 " 1 /* Disabled. */, %s,\n" \
358 "#endif\n",
359 condition, cl_flags, cl_bit_fields, cl_zero_bit_fields)
360 else
361 printf(" %s,\n" \
362 " 0, %s,\n",
363 cl_flags, cl_bit_fields)
364 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
365 var_set(flags[i]), comma)
368 print "};"
370 print "\n\n"
371 print "bool "
372 print "common_handle_option_auto (struct gcc_options *opts, "
373 print " struct gcc_options *opts_set, "
374 print " const struct cl_decoded_option *decoded, "
375 print " unsigned int lang_mask, int kind, "
376 print " location_t loc, "
377 print " const struct cl_option_handlers *handlers, "
378 print " diagnostic_context *dc) "
379 print "{ "
380 print " size_t scode = decoded->opt_index; "
381 print " int value = decoded->value; "
382 print " enum opt_code code = (enum opt_code) scode; "
383 print " "
384 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
385 print " "
386 print " switch (code) "
387 print " { "
388 for (i = 0; i < n_enabledby; i++) {
389 enabledby_name = enabledby[i];
390 print " case " opt_enum(enabledby_name) ":"
391 n_enables = split(enables[enabledby_name], thisenable, ",");
392 for (j = 1; j < n_enables; j++) {
393 opt_var_name = var_name(flags[opt_numbers[thisenable[j]]]);
394 if (opt_var_name != "") {
395 print " if (!opts_set->x_" opt_var_name ")"
396 print " handle_generated_option (opts, opts_set,"
397 print " " opt_enum(thisenable[j]) ", NULL, value,"
398 print " lang_mask, kind, loc, handlers, dc);"
399 } else {
400 print "#error " thisenable[j] " does not have a Var() flag"
403 print " break;\n"
405 print " default: "
406 print " break; "
407 print " } "
408 print " return true; "
409 print "} "
411 # Handle LangEnabledBy
412 for (i = 0; i < n_langs; i++) {
413 lang_name = lang_sanitized_name(langs[i]);
414 mark_unused = " ATTRIBUTE_UNUSED";
416 print "\n\n"
417 print "bool "
418 print lang_name "_handle_option_auto (struct gcc_options *opts" mark_unused ", "
419 print " struct gcc_options *opts_set" mark_unused ", "
420 print " size_t scode" mark_unused ", const char *arg" mark_unused ", int value" mark_unused ", "
421 print " unsigned int lang_mask" mark_unused ", int kind" mark_unused ", "
422 print " location_t loc" mark_unused ", "
423 print " const struct cl_option_handlers *handlers" mark_unused ", "
424 print " diagnostic_context *dc" mark_unused ") "
425 print "{ "
426 print " enum opt_code code = (enum opt_code) scode; "
427 print " "
428 print " switch (code) "
429 print " { "
431 for (k = 0; k < n_enabledby_lang[i]; k++) {
432 enabledby_name = enabledby[lang_name,k];
433 print " case " opt_enum(enabledby_name) ":"
434 n_enables = split(enables[lang_name,enabledby_name], thisenable, ",");
435 for (j = 1; j < n_enables; j++) {
436 opt_var_name = var_name(flags[opt_numbers[thisenable[j]]]);
437 if (opt_var_name != "") {
438 print " if (!opts_set->x_" opt_var_name ")"
439 print " handle_generated_option (opts, opts_set,"
440 print " " opt_enum(thisenable[j]) ", arg, value,"
441 print " lang_mask, kind, loc, handlers, dc);"
442 } else {
443 print "#error " thisenable[j] " does not have a Var() flag"
446 print " break;\n"
448 print " default: "
449 print " break; "
450 print " } "
451 print " return true; "
452 print "} "