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