]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/optc-gen.awk
sh.c: Do not include algorithm.
[thirdparty/gcc.git] / gcc / optc-gen.awk
CommitLineData
23a5b65a 1# Copyright (C) 2003-2014 Free Software Foundation, Inc.
776dc15d
KC
2# Contributed by Kelley Cook, June 2004.
3# Original code from Neil Booth, May 2003.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the
9dcd6f09 7# Free Software Foundation; either version 3, or (at your option) any
776dc15d
KC
8# later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
9dcd6f09
NC
16# along with this program; see the file COPYING3. If not see
17# <http://www.gnu.org/licenses/>.
776dc15d
KC
18
19# This Awk script reads in the option records generated from
fc54bc84 20# opt-gather.awk, combines the flags of duplicate options and generates a
776dc15d
KC
21# C file.
22#
86fa5de4
JM
23
24# This program uses functions from opt-functions.awk and code from
25# opt-read.awk.
776dc15d 26#
86fa5de4 27# Usage: awk -f opt-functions.awk -f opt-read.awk -f optc-gen.awk \
776dc15d
KC
28# [-v header_name=header.h] < inputfile > options.c
29
776dc15d
KC
30# Dump that array of options into a C file.
31END {
f2bc201f
MLI
32
33# Record first EnabledBy and LangEnabledBy uses.
34n_enabledby = 0;
35for (i = 0; i < n_langs; i++) {
36 n_enabledby_lang[i] = 0;
37}
38for (i = 0; i < n_opts; i++) {
39 enabledby_arg = opt_args("EnabledBy", flags[i]);
40 if (enabledby_arg != "") {
d919140b
MLI
41 n_enabledby_names = split(enabledby_arg, enabledby_names, " && ");
42 if (n_enabledby_names > 2) {
43 print "#error EnabledBy (Wfoo && Wbar && Wbaz) not currently supported"
44 }
45 for (j = 1; j <= n_enabledby_names; j++) {
46 enabledby_name = enabledby_names[j];
47 enabledby_index = opt_numbers[enabledby_name];
48 if (enabledby_index == "") {
49 print "#error Enabledby: " enabledby_name
50 } else {
51 condition = "";
52 if (n_enabledby_names == 2) {
53 opt_var_name_1 = search_var_name(enabledby_names[1], opt_numbers, opts, flags, n_opts);
54 opt_var_name_2 = search_var_name(enabledby_names[2], opt_numbers, opts, flags, n_opts);
55 if (opt_var_name_1 == "") {
56 print "#error " enabledby_names[1] " does not have a Var() flag"
57 }
58 if (opt_var_name_2 == "") {
59 print "#error " enabledby_names[2] " does not have a Var() flag"
60 }
61 condition = "opts->x_" opt_var_name_1 " && opts->x_" opt_var_name_2;
62 }
63 if (enables[enabledby_name] == "") {
64 enabledby[n_enabledby] = enabledby_name;
65 n_enabledby++;
66 }
67 enables[enabledby_name] = enables[enabledby_name] opts[i] ";";
68 enablesif[enabledby_name] = enablesif[enabledby_name] condition ";";
f2bc201f 69 }
f2bc201f
MLI
70 }
71 }
72
73 enabledby_arg = opt_args("LangEnabledBy", flags[i]);
74 if (enabledby_arg != "") {
0829c7f7 75 enabledby_langs = nth_arg(0, enabledby_arg);
f2bc201f 76 enabledby_name = nth_arg(1, enabledby_arg);
0829c7f7
MLI
77 enabledby_posarg = nth_arg(2, enabledby_arg);
78 enabledby_negarg = nth_arg(3, enabledby_arg);
79 lang_enabled_by(enabledby_langs, enabledby_name, enabledby_posarg, enabledby_negarg);
f2bc201f
MLI
80 }
81}
82
83
af47e6ac 84print "/* This file is auto-generated by optc-gen.awk. */"
776dc15d 85print ""
aeb70e78
RS
86n_headers = split(header_name, headers, " ")
87for (i = 1; i <= n_headers; i++)
88 print "#include " quote headers[i] quote
776dc15d 89print "#include " quote "opts.h" quote
fb664b5a 90print "#include " quote "intl.h" quote
c344a866 91print "#include " quote "insn-attr-common.h" quote
776dc15d
KC
92print ""
93
fd438373
MM
94if (n_extra_c_includes > 0) {
95 for (i = 0; i < n_extra_c_includes; i++) {
96 print "#include " quote extra_c_includes[i] quote
97 }
98 print ""
99}
100
e6d4b984
JM
101for (i = 0; i < n_enums; i++) {
102 name = enum_names[i]
103 type = enum_type[name]
104 print "static const struct cl_enum_arg cl_enum_" name \
105 "_data[] = "
106 print "{"
107 print enum_data[name] " { NULL, 0, 0 }"
108 print "};"
109 print ""
110 print "static void"
111 print "cl_enum_" name "_set (void *var, int value)"
112 print "{"
113 print " *((" type " *) var) = (" type ") value;"
114 print "}"
115 print ""
116 print "static int"
117 print "cl_enum_" name "_get (const void *var)"
118 print "{"
119 print " return (int) *((const " type " *) var);"
120 print "}"
121 print ""
122}
123
124print "const struct cl_enum cl_enums[] ="
125print "{"
126for (i = 0; i < n_enums; i++) {
127 name = enum_names[i]
128 ehelp = enum_help[name]
129 if (ehelp == "")
130 ehelp = "NULL"
131 else
132 ehelp = quote ehelp quote
133 unknown_error = enum_unknown_error[name]
134 if (unknown_error == "")
135 unknown_error = "NULL"
136 else
137 unknown_error = quote unknown_error quote
138 print " {"
139 print " " ehelp ","
140 print " " unknown_error ","
141 print " cl_enum_" name "_data,"
142 print " sizeof (" enum_type[name] "),"
143 print " cl_enum_" name "_set,"
144 print " cl_enum_" name "_get"
145 print " },"
146}
147print "};"
148print "const unsigned int cl_enums_count = " n_enums ";"
149print ""
150
a75bfaa6 151print "const struct gcc_options global_options_init =\n{"
e90afde6 152for (i = 0; i < n_extra_vars; i++) {
e3339d0f
JM
153 var = extra_vars[i]
154 init = extra_vars[i]
155 if (var ~ "=" ) {
156 sub(".*= *", "", init)
e3339d0f
JM
157 } else {
158 init = "0"
159 }
24f48c2f
JM
160 sub(" *=.*", "", var)
161 name = var
162 sub("^.*[ *]", "", name)
163 sub("\\[.*\\]$", "", name)
164 var_seen[name] = 1
165 print " " init ", /* " name " */"
e90afde6 166}
776dc15d
KC
167for (i = 0; i < n_opts; i++) {
168 name = var_name(flags[i]);
169 if (name == "")
170 continue;
171
e3339d0f
JM
172 init = opt_args("Init", flags[i])
173 if (init != "") {
174 if (name in var_init && var_init[name] != init)
175 print "#error multiple initializers for " name
176 var_init[name] = init
14c7833c 177 }
e3339d0f
JM
178}
179for (i = 0; i < n_opts; i++) {
180 name = var_name(flags[i]);
181 if (name == "")
182 continue;
183
184 if (name in var_seen)
185 continue;
186
187 if (name in var_init)
188 init = var_init[name]
189 else
190 init = "0"
a56a0779 191
e3339d0f 192 print " " init ", /* " name " */"
f7f655c7
DD
193
194 var_seen[name] = 1;
a56a0779 195}
ee133b69
RS
196for (i = 0; i < n_opts; i++) {
197 name = static_var(opts[i], flags[i]);
46625112
JM
198 if (name != "") {
199 print " 0, /* " name " (private state) */"
200 print "#undef x_" name
201 }
ee133b69 202}
5e46b0c6
ILT
203for (i = 0; i < n_opts; i++) {
204 if (flag_set_p("SetByCombined", flags[i]))
205 print " false, /* frontend_set_" var_name(flags[i]) " */"
206}
46625112 207print "};"
ee133b69 208print ""
a75bfaa6 209print "struct gcc_options global_options;"
d4d24ba4
JM
210print "struct gcc_options global_options_set;"
211print ""
776dc15d
KC
212
213print "const char * const lang_names[] =\n{"
214for (i = 0; i < n_langs; i++) {
f2bc201f 215 macros[i] = "CL_" lang_sanitized_name(langs[i])
776dc15d
KC
216 s = substr(" ", length (macros[i]))
217 print " " quote langs[i] quote ","
218 }
219
220print " 0\n};\n"
221print "const unsigned int cl_options_count = N_OPTS;\n"
58265ea6
GF
222print "#if (1U << " n_langs ") > CL_MIN_OPTION_CLASS"
223print " #error the number of languages exceeds the implementation limit"
224print "#endif"
c662432e 225print "const unsigned int cl_lang_count = " n_langs ";\n"
776dc15d
KC
226
227print "const struct cl_option cl_options[] =\n{"
228
14c7833c
L
229j = 0
230for (i = 0; i < n_opts; i++) {
776dc15d 231 back_chain[i] = "N_OPTS";
14c7833c
L
232 indices[opts[i]] = j;
233 # Combine the flags of identical switches. Switches
234 # appear many times if they are handled by many front
235 # ends, for example.
236 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
237 flags[i + 1] = flags[i] " " flags[i + 1];
a16d1645
RW
238 if (help[i + 1] == "")
239 help[i + 1] = help[i]
40a1cfba 240 else if (help[i] != "" && help[i + 1] != help[i])
71caddc5
MLI
241 print "#error Multiple different help strings for " \
242 opts[i] ":\n\t" help[i] "\n\t" help[i + 1]
243
14c7833c
L
244 i++;
245 back_chain[i] = "N_OPTS";
246 indices[opts[i]] = j;
247 }
248 j++;
249}
776dc15d 250
b167666c 251for (i = 0; i < n_opts; i++) {
a16d1645
RW
252 # With identical flags, pick only the last one. The
253 # earlier loop ensured that it has all flags merged,
254 # and a nonempty help text if one of the texts was nonempty.
b167666c 255 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
b167666c
RS
256 i++;
257 }
776dc15d 258
b167666c 259 len = length (opts[i]);
9db94baa 260 enum = opt_enum(opts[i])
b167666c
RS
261
262 # If this switch takes joined arguments, back-chain all
263 # subsequent switches to it for which it is a prefix. If
264 # a later switch S is a longer prefix of a switch T, T
265 # will be back-chained to S in a later iteration of this
266 # for() loop, which is what we want.
267 if (flag_set_p("Joined.*", flags[i])) {
268 for (j = i + 1; j < n_opts; j++) {
269 if (substr (opts[j], 1, len) != opts[i])
270 break;
271 back_chain[j] = enum;
776dc15d 272 }
b167666c 273 }
776dc15d 274
b167666c
RS
275 s = substr(" ", length (opts[i]))
276 if (i + 1 == n_opts)
277 comma = ""
776dc15d 278
b167666c
RS
279 if (help[i] == "")
280 hlp = "0"
281 else
282 hlp = quote help[i] quote;
776dc15d 283
61ff2bdc
JM
284 missing_arg_error = opt_args("MissingArgError", flags[i])
285 if (missing_arg_error == "")
286 missing_arg_error = "0"
287 else
288 missing_arg_error = quote missing_arg_error quote
289
2d2bd949
JM
290
291 warn_message = opt_args("Warn", flags[i])
292 if (warn_message == "")
293 warn_message = "0"
294 else
295 warn_message = quote warn_message quote
296
5de8299c
JM
297 alias_arg = opt_args("Alias", flags[i])
298 if (alias_arg == "") {
2d2bd949
JM
299 if (flag_set_p("Ignore", flags[i]))
300 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
301 else
302 alias_data = "NULL, NULL, N_OPTS"
5de8299c
JM
303 } else {
304 alias_opt = nth_arg(0, alias_arg)
305 alias_posarg = nth_arg(1, alias_arg)
306 alias_negarg = nth_arg(2, alias_arg)
307
46625112 308 if (var_ref(opts[i], flags[i]) != "-1")
5de8299c
JM
309 print "#error Alias setting variable"
310
311 if (alias_posarg != "" && alias_negarg == "") {
312 if (!flag_set_p("RejectNegative", flags[i]) \
313 && opts[i] ~ "^[Wfm]")
314 print "#error Alias with single argument " \
315 "allowing negative form"
316 }
666a21a2
JM
317 if (alias_posarg != "" \
318 && flag_set_p("NegativeAlias", flags[i])) {
319 print "#error Alias with multiple arguments " \
320 "used with NegativeAlias"
321 }
5de8299c
JM
322
323 alias_opt = opt_enum(alias_opt)
324 if (alias_posarg == "")
325 alias_posarg = "NULL"
326 else
327 alias_posarg = quote alias_posarg quote
328 if (alias_negarg == "")
329 alias_negarg = "NULL"
330 else
331 alias_negarg = quote alias_negarg quote
332 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
333 }
334
14c7833c
L
335 neg = opt_args("Negative", flags[i]);
336 if (neg != "")
337 idx = indices[neg]
338 else {
339 if (flag_set_p("RejectNegative", flags[i]))
340 idx = -1;
341 else {
342 if (opts[i] ~ "^[Wfm]")
343 idx = indices[opts[i]];
344 else
345 idx = -1;
346 }
347 }
b1c61c7e
RW
348 # Split the printf after %u to work around an ia64-hp-hpux11.23
349 # awk bug.
2d2bd949
JM
350 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
351 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
5de8299c 352 alias_data, back_chain[i], len)
b1c61c7e 353 printf(" %d,\n", idx)
aeb70e78
RS
354 condition = opt_args("Condition", flags[i])
355 cl_flags = switch_flags(flags[i])
300d83d9
JM
356 cl_bit_fields = switch_bit_fields(flags[i])
357 cl_zero_bit_fields = switch_bit_fields("")
aeb70e78
RS
358 if (condition != "")
359 printf("#if %s\n" \
360 " %s,\n" \
300d83d9 361 " 0, %s,\n" \
aeb70e78 362 "#else\n" \
300d83d9
JM
363 " 0,\n" \
364 " 1 /* Disabled. */, %s,\n" \
aeb70e78 365 "#endif\n",
300d83d9 366 condition, cl_flags, cl_bit_fields, cl_zero_bit_fields)
aeb70e78 367 else
300d83d9
JM
368 printf(" %s,\n" \
369 " 0, %s,\n",
370 cl_flags, cl_bit_fields)
ee133b69
RS
371 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
372 var_set(flags[i]), comma)
776dc15d
KC
373}
374
375print "};"
ab442df7 376
7d5a5747
MLI
377print "\n\n"
378print "bool "
379print "common_handle_option_auto (struct gcc_options *opts, "
380print " struct gcc_options *opts_set, "
381print " const struct cl_decoded_option *decoded, "
382print " unsigned int lang_mask, int kind, "
383print " location_t loc, "
384print " const struct cl_option_handlers *handlers, "
385print " diagnostic_context *dc) "
386print "{ "
387print " size_t scode = decoded->opt_index; "
388print " int value = decoded->value; "
389print " enum opt_code code = (enum opt_code) scode; "
390print " "
391print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
392print " "
393print " switch (code) "
394print " { "
d919140b 395# Handle EnabledBy
7d5a5747
MLI
396for (i = 0; i < n_enabledby; i++) {
397 enabledby_name = enabledby[i];
398 print " case " opt_enum(enabledby_name) ":"
65d4f2cd 399 n_enables = split(enables[enabledby_name], thisenable, ";");
d919140b
MLI
400 n_enablesif = split(enablesif[enabledby_name], thisenableif, ";");
401 if (n_enables != n_enablesif) {
402 print "#error n_enables != n_enablesif: Something went wrong!"
403 }
7d5a5747
MLI
404 for (j = 1; j < n_enables; j++) {
405 opt_var_name = var_name(flags[opt_numbers[thisenable[j]]]);
9b095bf1 406 if (opt_var_name != "") {
d919140b
MLI
407 condition = "!opts_set->x_" opt_var_name
408 if (thisenableif[j] != "") {
a82122df
MK
409 value = "(" thisenableif[j] ")"
410 } else {
411 value = "value"
d919140b
MLI
412 }
413 print " if (" condition ")"
9b095bf1 414 print " handle_generated_option (opts, opts_set,"
a82122df 415 print " " opt_enum(thisenable[j]) ", NULL, " value ","
9b095bf1
MLI
416 print " lang_mask, kind, loc, handlers, dc);"
417 } else {
418 print "#error " thisenable[j] " does not have a Var() flag"
419 }
7d5a5747
MLI
420 }
421 print " break;\n"
7d5a5747
MLI
422}
423print " default: "
424print " break; "
425print " } "
426print " return true; "
427print "} "
428
f2bc201f
MLI
429# Handle LangEnabledBy
430for (i = 0; i < n_langs; i++) {
431 lang_name = lang_sanitized_name(langs[i]);
432 mark_unused = " ATTRIBUTE_UNUSED";
433
434 print "\n\n"
435 print "bool "
436 print lang_name "_handle_option_auto (struct gcc_options *opts" mark_unused ", "
437 print " struct gcc_options *opts_set" mark_unused ", "
438 print " size_t scode" mark_unused ", const char *arg" mark_unused ", int value" mark_unused ", "
439 print " unsigned int lang_mask" mark_unused ", int kind" mark_unused ", "
440 print " location_t loc" mark_unused ", "
441 print " const struct cl_option_handlers *handlers" mark_unused ", "
442 print " diagnostic_context *dc" mark_unused ") "
443 print "{ "
444 print " enum opt_code code = (enum opt_code) scode; "
445 print " "
446 print " switch (code) "
447 print " { "
448
449 for (k = 0; k < n_enabledby_lang[i]; k++) {
450 enabledby_name = enabledby[lang_name,k];
451 print " case " opt_enum(enabledby_name) ":"
65d4f2cd
MLI
452 n_thisenable = split(enables[lang_name,enabledby_name], thisenable, ";");
453 for (j = 1; j < n_thisenable; j++) {
454 n_thisenable_args = split(thisenable[j], thisenable_args, ",");
455 if (n_thisenable_args == 1) {
456 thisenable_opt = thisenable[j];
457 value = "value";
458 } else {
459 thisenable_opt = thisenable_args[1];
460 with_posarg = thisenable_args[2];
461 with_negarg = thisenable_args[3];
462 value = "value ? " with_posarg " : " with_negarg;
463 }
464 opt_var_name = var_name(flags[opt_numbers[thisenable_opt]]);
9b095bf1
MLI
465 if (opt_var_name != "") {
466 print " if (!opts_set->x_" opt_var_name ")"
467 print " handle_generated_option (opts, opts_set,"
65d4f2cd 468 print " " opt_enum(thisenable_opt) ", NULL, " value ","
9b095bf1
MLI
469 print " lang_mask, kind, loc, handlers, dc);"
470 } else {
65d4f2cd 471 print "#error " thisenable_opt " does not have a Var() flag"
9b095bf1 472 }
f2bc201f
MLI
473 }
474 print " break;\n"
475 }
476 print " default: "
477 print " break; "
478 print " } "
479 print " return true; "
480 print "} "
481}
482
43f9a13c
MLI
483#Handle CPP()
484print "\n"
485print "#include " quote "cpplib.h" quote;
486print "void"
487print "cpp_handle_option_auto (const struct gcc_options * opts, "
488print " size_t scode, struct cpp_options * cpp_opts)"
489print "{ "
490print " enum opt_code code = (enum opt_code) scode; "
491print " "
492print " switch (code) "
493print " { "
494for (i = 0; i < n_opts; i++) {
495 # With identical flags, pick only the last one. The
496 # earlier loop ensured that it has all flags merged,
497 # and a nonempty help text if one of the texts was nonempty.
498 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
499 i++;
500 }
501
502 cpp_option = nth_arg(0, opt_args("CPP", flags[i]));
2b71f4a4
MLI
503 if (cpp_option != "") {
504 opt_var_name = var_name(flags[i]);
505 init = opt_args("Init", flags[i])
506 if (opt_var_name != "" && init != "") {
507 print " case " opt_enum(opts[i]) ":"
508 print " cpp_opts->" cpp_option " = opts->x_" opt_var_name ";"
509 print " break;"
510 } else if (opt_var_name == "" && init == "") {
511 print "#error CPP() requires setting Init() and Var() for " opts[i]
512 } else if (opt_var_name != "") {
513 print "#error CPP() requires setting Init() for " opts[i]
514 } else {
515 print "#error CPP() requires setting Var() for " opts[i]
516 }
43f9a13c 517 }
776dc15d 518}
43f9a13c
MLI
519print " default: "
520print " break; "
521print " } "
522print "}\n"
523print "void"
524print "init_global_opts_from_cpp(struct gcc_options * opts, "
525print " const struct cpp_options * cpp_opts)"
526print "{ "
527for (i = 0; i < n_opts; i++) {
528 # With identical flags, pick only the last one. The
529 # earlier loop ensured that it has all flags merged,
530 # and a nonempty help text if one of the texts was nonempty.
531 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
532 i++;
533 }
534 cpp_option = nth_arg(0, opt_args("CPP", flags[i]));
535 opt_var_name = var_name(flags[i]);
536 if (cpp_option != "" && opt_var_name != "") {
537 print " opts->x_" opt_var_name " = cpp_opts->" cpp_option ";"
538 }
539}
540print "} "
541
542}
543