1 # Copyright (C) 2003-2021 Free Software Foundation, Inc.
2 # Contributed by Kelley Cook, June 2004.
3 # Original code from Neil Booth, May 2003.
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
7 # Free Software Foundation; either version 3, or (at your option) any
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.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; see the file COPYING3. If not see
17 # <http://www.gnu.org/licenses/>.
19 # This Awk script reads in the option records generated from
20 # opt-gather.awk, combines the flags of duplicate options and generates a
24 # This program uses functions from opt-functions.awk and code from
27 # Usage: awk -f opt-functions.awk -f opt-read.awk -f optc-gen.awk \
28 # [-v header_name=header.h] < inputfile > options.c
30 # Dump that array of options into a C file.
34 # Combine the flags of identical switches. Switches
35 # appear many times if they are handled by many front
37 for (i =
0; i
< n_opts
; i
++) {
38 merged_flags
[i
] = flags
[i
]
40 for (i =
0; i
< n_opts
; i
++) {
41 while(i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
42 merged_flags
[i
+ 1] = merged_flags
[i
] " " merged_flags
[i
+ 1];
47 # Record EnabledBy and LangEnabledBy uses.
49 for (i =
0; i
< n_langs
; i
++) {
50 n_enabledby_lang
[i
] =
0;
52 for (i =
0; i
< n_opts
; i
++) {
53 enabledby_arg = opt_args
("EnabledBy", flags
[i
]);
54 if (enabledby_arg
!= "") {
55 logical_and =
index(enabledby_arg
, " && ");
56 if (logical_and
!= 0) {
57 # EnabledBy(arg1 && arg2)
60 # EnabledBy(arg) or EnabledBy(arg1 || arg2 || arg3)
61 split_sep =
" \\|\\| ";
63 n_enabledby_names =
split(enabledby_arg
, enabledby_names
, split_sep
);
64 if (logical_and
!= 0 && n_enabledby_names
> 2) {
65 print "#error " opts
[i
] " EnabledBy(Wfoo && Wbar && Wbaz) currently not supported"
67 for (j =
1; j
<= n_enabledby_names
; j
++) {
68 enabledby_name = enabledby_names
[j
];
69 enabledby_index = opt_numbers
[enabledby_name
];
70 if (enabledby_index ==
"") {
71 print "#error " opts
[i
] " Enabledby(" enabledby_name
"), unknown option '" enabledby_name
"'"
72 } else if (!flag_set_p
("Common", merged_flags
[enabledby_index
])) {
73 print "#error " opts
[i
] " Enabledby(" enabledby_name
"), '" \
74 enabledby_name
"' must have flag 'Common'" \
75 " to use Enabledby(), otherwise use LangEnabledBy()"
78 if (logical_and
!= 0) {
79 opt_var_name_1 = search_var_name
(enabledby_names
[1], opt_numbers
, opts
, flags
, n_opts
);
80 opt_var_name_2 = search_var_name
(enabledby_names
[2], opt_numbers
, opts
, flags
, n_opts
);
81 if (opt_var_name_1 ==
"") {
82 print "#error " enabledby_names
[1] " does not have a Var() flag"
84 if (opt_var_name_2 ==
"") {
85 print "#error " enabledby_names
[2] " does not have a Var() flag"
87 condition =
"opts->x_" opt_var_name_1
" && opts->x_" opt_var_name_2
;
89 if (enables
[enabledby_name
] ==
"") {
90 enabledby
[n_enabledby
] = enabledby_name
;
93 enables
[enabledby_name
] = enables
[enabledby_name
] opts
[i
] ";";
94 enablesif
[enabledby_name
] = enablesif
[enabledby_name
] condition
";";
99 enabledby_arg = opt_args
("LangEnabledBy", flags
[i
]);
100 if (enabledby_arg
!= "") {
101 enabledby_langs = nth_arg
(0, enabledby_arg
);
102 enabledby_name = nth_arg
(1, enabledby_arg
);
103 enabledby_posarg = nth_arg
(2, enabledby_arg
);
104 enabledby_negarg = nth_arg
(3, enabledby_arg
);
105 lang_enabled_by
(enabledby_langs
, enabledby_name
, enabledby_posarg
, enabledby_negarg
);
108 if (flag_set_p
("Param", flags
[i
]) && !
(opts
[i
] ~
"^-param="))
109 print "#error Parameter option name '" opts
[i
] "' must start with '-param='"
113 print "/* This file is auto-generated by optc-gen.awk. */"
115 n_headers =
split(header_name
, headers
, " ")
116 for (i =
1; i
<= n_headers
; i
++)
117 print "#include " quote headers
[i
] quote
118 print "#include " quote
"opts.h" quote
119 print "#include " quote
"intl.h" quote
120 print "#include " quote
"insn-attr-common.h" quote
123 if (n_extra_c_includes
> 0) {
124 for (i =
0; i
< n_extra_c_includes
; i
++) {
125 print "#include " quote extra_c_includes
[i
] quote
130 for (i =
0; i
< n_enums
; i
++) {
132 type = enum_type
[name
]
133 print "static const struct cl_enum_arg cl_enum_" name \
136 print enum_data
[name
] " { NULL, 0, 0 }"
140 print "cl_enum_" name
"_set (void *var, int value)"
142 print " *((" type
" *) var) = (" type
") value;"
146 print "cl_enum_" name
"_get (const void *var)"
148 print " return (int) *((const " type
" *) var);"
153 print "const struct cl_enum cl_enums[] ="
155 for (i =
0; i
< n_enums
; i
++) {
157 ehelp = enum_help
[name
]
161 ehelp = quote ehelp quote
162 unknown_error = enum_unknown_error
[name
]
163 if (unknown_error ==
"")
164 unknown_error =
"NULL"
166 unknown_error = quote unknown_error quote
169 print " " unknown_error
","
170 print " cl_enum_" name
"_data,"
171 print " sizeof (" enum_type
[name
] "),"
172 print " cl_enum_" name
"_set,"
173 print " cl_enum_" name
"_get"
177 print "const unsigned int cl_enums_count = " n_enums
";"
180 print "const struct gcc_options global_options_init =\n{"
181 for (i =
0; i
< n_extra_vars
; i
++) {
185 sub(".*= *", "", init
)
189 sub(" *=.*", "", var
)
191 sub("^.*[ *]", "", name
)
192 sub("\\[.*\\]$", "", name
)
194 print " " init
", /* " name
" */"
196 for (i =
0; i
< n_opts
; i
++) {
197 name = var_name
(flags
[i
]);
198 init = opt_args
("Init", flags
[i
])
202 print "#error " opts
[i
] " must specify Var to use Init"
207 if (name in var_init
&& var_init
[name
] != init
)
208 print "#error multiple initializers for " name
209 var_init
[name
] = init
212 for (i =
0; i
< n_opts
; i
++) {
213 name = var_name
(flags
[i
]);
217 if (name in var_seen
)
220 if (name in var_init
)
221 init = var_init
[name
]
225 print " " init
", /* " name
" */"
229 for (i =
0; i
< n_opts
; i
++) {
230 name = static_var
(opts
[i
], flags
[i
]);
232 print " 0, /* " name
" (private state) */"
233 print "#undef x_" name
236 for (i =
0; i
< n_opts
; i
++) {
237 if (flag_set_p
("SetByCombined", flags
[i
]))
238 print " false, /* frontend_set_" var_name
(flags
[i
]) " */"
242 print "struct gcc_options global_options;"
243 print "struct gcc_options global_options_set;"
246 print "const char * const lang_names[] =\n{"
247 for (i =
0; i
< n_langs
; i
++) {
248 macros
[i
] =
"CL_" lang_sanitized_name
(langs
[i
])
249 s =
substr(" ", length (macros
[i
]))
250 print " " quote langs
[i
] quote
","
254 print "const unsigned int cl_options_count = N_OPTS;\n"
255 print "#if (1U << " n_langs
") > CL_MIN_OPTION_CLASS"
256 print " #error the number of languages exceeds the implementation limit"
258 print "const unsigned int cl_lang_count = " n_langs
";\n"
260 print "const struct cl_option cl_options[] =\n{"
263 for (i =
0; i
< n_opts
; i
++) {
264 back_chain
[i
] =
"N_OPTS";
265 indices
[opts
[i
]] = j
;
266 # Combine the flags of identical switches. Switches
267 # appear many times if they are handled by many front
269 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
270 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
271 if (help
[i
+ 1] ==
"")
272 help
[i
+ 1] = help
[i
]
273 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
])
274 print "#error Multiple different help strings for " \
275 opts
[i
] ":\n\t" help
[i
] "\n\t" help
[i
+ 1]
278 back_chain
[i
] =
"N_OPTS";
279 indices
[opts
[i
]] = j
;
285 for (i =
0; i
< n_opts
; i
++) {
286 # With identical flags, pick only the last one. The
287 # earlier loop ensured that it has all flags merged,
288 # and a nonempty help text if one of the texts was nonempty.
289 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
293 len =
length (opts
[i
]);
294 enum = opt_enum
(opts
[i
])
296 # If this switch takes joined arguments, back-chain all
297 # subsequent switches to it for which it is a prefix. If
298 # a later switch S is a longer prefix of a switch T, T
299 # will be back-chained to S in a later iteration of this
300 # for() loop, which is what we want.
301 if (flag_set_p
("Joined.*", flags
[i
])) {
302 for (j = i
+ 1; j
< n_opts
; j
++) {
303 if (substr (opts
[j
], 1, len
) != opts
[i
])
305 back_chain
[j
] = enum
;
309 s =
substr(" ", length (opts
[i
]))
316 hlp = quote help
[i
] quote
;
318 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
319 if (missing_arg_error ==
"")
320 missing_arg_error =
"NULL"
322 missing_arg_error = quote missing_arg_error quote
325 warn_message = opt_args
("Warn", flags
[i
])
326 if (warn_message ==
"")
327 warn_message =
"NULL"
329 warn_message = quote warn_message quote
331 alias_arg = opt_args
("Alias", flags
[i
])
332 if (alias_arg ==
"") {
333 if (flag_set_p
("Ignore", flags
[i
])) {
334 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
335 if (warn_message
!= "NULL")
336 print "#error Ignored option with Warn"
337 if (var_name
(flags
[i
]) != "")
338 print "#error Ignored option with Var"
340 else if (flag_set_p
("Deprecated", flags
[i
]))
341 print "#error Deprecated was replaced with WarnRemoved"
342 else if (flag_set_p
("WarnRemoved", flags
[i
])) {
343 alias_data =
"NULL, NULL, OPT_SPECIAL_warn_removed"
344 if (warn_message
!= "NULL")
345 print "#error WarnRemoved option with Warn"
348 alias_data =
"NULL, NULL, N_OPTS"
349 if (flag_set_p
("Enum.*", flags
[i
])) {
350 if (!flag_set_p
("RejectNegative", flags
[i
]) \
351 && opts
[i
] ~
"^[Wfgm]")
352 print "#error Enum allowing negative form"
355 alias_opt = nth_arg
(0, alias_arg
)
356 alias_posarg = nth_arg
(1, alias_arg
)
357 alias_negarg = nth_arg
(2, alias_arg
)
359 if (var_ref
(opts
[i
], flags
[i
]) != "(unsigned short) -1")
360 print "#error Alias setting variable"
362 if (alias_posarg
!= "" && alias_negarg ==
"") {
363 if (!flag_set_p
("RejectNegative", flags
[i
]) \
364 && opts
[i
] ~
"^[Wfm]")
365 print "#error Alias with single argument " \
366 "allowing negative form"
368 if (alias_posarg
!= "" \
369 && flag_set_p
("NegativeAlias", flags
[i
])) {
370 print "#error Alias with multiple arguments " \
371 "used with NegativeAlias"
374 alias_opt = opt_enum
(alias_opt
)
375 if (alias_posarg ==
"")
376 alias_posarg =
"NULL"
378 alias_posarg = quote alias_posarg quote
379 if (alias_negarg ==
"")
380 alias_negarg =
"NULL"
382 alias_negarg = quote alias_negarg quote
383 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
386 neg = opt_args
("Negative", flags
[i
]);
390 if (flag_set_p
("RejectNegative", flags
[i
]))
393 if (opts
[i
] ~
"^[Wfgm]")
394 idx = indices
[opts
[i
]];
399 # Split the printf after %u to work around an ia64-hp-hpux11.23
401 printf(" /* [%i] = */ {\n", optindex
)
402 printf(" %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
403 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
404 alias_data
, back_chain
[i
], len
)
405 printf(" /* .neg_idx = */ %d,\n", idx
)
406 condition = opt_args
("Condition", flags
[i
])
407 cl_flags = switch_flags
(flags
[i
])
408 cl_bit_fields = switch_bit_fields
(flags
[i
])
409 cl_zero_bit_fields = switch_bit_fields
("")
416 " 1 /* Disabled. */, %s,\n" \
418 condition
, cl_flags
, cl_bit_fields
, cl_zero_bit_fields
)
422 cl_flags
, cl_bit_fields
)
423 printf(" %s, %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
424 var_set
(flags
[i
]), integer_range_info
(opt_args
("IntegerRange", flags
[i
]),
425 opt_args
("Init", flags
[i
]), opts
[i
], flag_set_p
("UInteger", flags
[i
])), comma
)
427 # Bump up the informational option index.
435 print "common_handle_option_auto (struct gcc_options *opts, "
436 print " struct gcc_options *opts_set, "
437 print " const struct cl_decoded_option *decoded, "
438 print " unsigned int lang_mask, int kind, "
439 print " location_t loc, "
440 print " const struct cl_option_handlers *handlers, "
441 print " diagnostic_context *dc) "
443 print " size_t scode = decoded->opt_index; "
444 print " HOST_WIDE_INT value = decoded->value; "
445 print " enum opt_code code = (enum opt_code) scode; "
447 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
449 print " switch (code) "
452 for (i =
0; i
< n_enabledby
; i
++) {
453 enabledby_name = enabledby
[i
];
454 print " case " opt_enum
(enabledby_name
) ":"
455 n_enables =
split(enables
[enabledby_name
], thisenable
, ";");
456 n_enablesif =
split(enablesif
[enabledby_name
], thisenableif
, ";");
457 if (n_enables
!= n_enablesif
) {
458 print "#error n_enables != n_enablesif: Something went wrong!"
460 for (j =
1; j
< n_enables
; j
++) {
461 opt_var_name = var_name
(flags
[opt_numbers
[thisenable
[j
]]]);
462 if (opt_var_name
!= "") {
463 condition =
"!opts_set->x_" opt_var_name
464 if (thisenableif
[j
] != "") {
465 value =
"(" thisenableif
[j
] ")"
469 print " if (" condition
")"
470 print " handle_generated_option (opts, opts_set,"
471 print " " opt_enum
(thisenable
[j
]) ", NULL, " value
","
472 print " lang_mask, kind, loc, handlers, true, dc);"
474 print "#error " thisenable
[j
] " does not have a Var() flag"
482 print " return true; "
485 # Handle LangEnabledBy
486 for (i =
0; i
< n_langs
; i
++) {
487 lang_name = lang_sanitized_name
(langs
[i
]);
488 mark_unused =
" ATTRIBUTE_UNUSED";
492 print lang_name
"_handle_option_auto (struct gcc_options *opts" mark_unused
", "
493 print " struct gcc_options *opts_set" mark_unused
", "
494 print " size_t scode" mark_unused
", const char *arg" mark_unused
", HOST_WIDE_INT value" mark_unused
", "
495 print " unsigned int lang_mask" mark_unused
", int kind" mark_unused
", "
496 print " location_t loc" mark_unused
", "
497 print " const struct cl_option_handlers *handlers" mark_unused
", "
498 print " diagnostic_context *dc" mark_unused
") "
500 print " enum opt_code code = (enum opt_code) scode; "
502 print " switch (code) "
505 for (k =
0; k
< n_enabledby_lang
[i
]; k
++) {
506 enabledby_name = enabledby
[lang_name
,k
];
507 print " case " opt_enum
(enabledby_name
) ":"
508 n_thisenable =
split(enables
[lang_name
,enabledby_name
], thisenable
, ";");
509 for (j =
1; j
< n_thisenable
; j
++) {
510 n_thisenable_args =
split(thisenable
[j
], thisenable_args
, ",");
511 if (n_thisenable_args ==
1) {
512 thisenable_opt = thisenable
[j
];
515 thisenable_opt = thisenable_args
[1];
516 with_posarg = thisenable_args
[2];
517 with_negarg = thisenable_args
[3];
518 value =
"value ? " with_posarg
" : " with_negarg
;
520 opt_var_name = var_name
(flags
[opt_numbers
[thisenable_opt
]]);
521 if (opt_var_name
!= "") {
522 print " if (!opts_set->x_" opt_var_name
")"
523 print " handle_generated_option (opts, opts_set,"
524 print " " opt_enum
(thisenable_opt
) ", NULL, " value
","
525 print " lang_mask, kind, loc, handlers, true, dc);"
527 print "#error " thisenable_opt
" does not have a Var() flag"
535 print " return true; "
541 print "#include " quote
"cpplib.h" quote
;
543 print "cpp_handle_option_auto (const struct gcc_options * opts, "
544 print " size_t scode, struct cpp_options * cpp_opts)"
546 print " enum opt_code code = (enum opt_code) scode; "
548 print " switch (code) "
550 for (i =
0; i
< n_opts
; i
++) {
551 # With identical flags, pick only the last one. The
552 # earlier loop ensured that it has all flags merged,
553 # and a nonempty help text if one of the texts was nonempty.
554 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
558 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
559 if (cpp_option
!= "") {
560 opt_var_name = var_name
(flags
[i
]);
561 init = opt_args
("Init", flags
[i
])
562 if (opt_var_name
!= "" && init
!= "") {
563 print " case " opt_enum
(opts
[i
]) ":"
564 print " cpp_opts->" cpp_option
" = opts->x_" opt_var_name
";"
566 } else if (opt_var_name ==
"" && init ==
"") {
567 print "#error CPP() requires setting Init() and Var() for " opts
[i
]
568 } else if (opt_var_name
!= "") {
569 print "#error CPP() requires setting Init() for " opts
[i
]
571 print "#error CPP() requires setting Var() for " opts
[i
]
580 print "init_global_opts_from_cpp(struct gcc_options * opts, "
581 print " const struct cpp_options * cpp_opts)"
583 for (i =
0; i
< n_opts
; i
++) {
584 # With identical flags, pick only the last one. The
585 # earlier loop ensured that it has all flags merged,
586 # and a nonempty help text if one of the texts was nonempty.
587 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
590 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
591 opt_var_name = var_name
(flags
[i
]);
592 if (cpp_option
!= "" && opt_var_name
!= "") {
593 print " opts->x_" opt_var_name
" = cpp_opts->" cpp_option
";"
598 split("", var_seen
, ":")
599 print "\n#if !defined(GENERATOR_FILE) && defined(ENABLE_PLUGIN)"
600 print "DEBUG_VARIABLE const struct cl_var cl_vars[] =\n{"
602 for (i =
0; i
< n_opts
; i
++) {
603 name = var_name
(flags
[i
]);
609 for (i =
0; i
< n_extra_vars
; i
++) {
611 sub(" *=.*", "", var
)
613 sub("^.*[ *]", "", name
)
614 sub("\\[.*\\]$", "", name
)
615 if (name in var_seen
)
617 print " { " quote name quote
", offsetof (struct gcc_options, x_" name
") },"
621 print " { NULL, (unsigned short) -1 }\n};\n#endif"