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
]);
201 init = opt_args
("Init", flags
[i
])
203 if (name in var_init
&& var_init
[name
] != init
)
204 print "#error multiple initializers for " name
205 var_init
[name
] = init
208 for (i =
0; i
< n_opts
; i
++) {
209 name = var_name
(flags
[i
]);
213 if (name in var_seen
)
216 if (name in var_init
)
217 init = var_init
[name
]
221 print " " init
", /* " name
" */"
225 for (i =
0; i
< n_opts
; i
++) {
226 name = static_var
(opts
[i
], flags
[i
]);
228 print " 0, /* " name
" (private state) */"
229 print "#undef x_" name
232 for (i =
0; i
< n_opts
; i
++) {
233 if (flag_set_p
("SetByCombined", flags
[i
]))
234 print " false, /* frontend_set_" var_name
(flags
[i
]) " */"
238 print "struct gcc_options global_options;"
239 print "struct gcc_options global_options_set;"
242 print "const char * const lang_names[] =\n{"
243 for (i =
0; i
< n_langs
; i
++) {
244 macros
[i
] =
"CL_" lang_sanitized_name
(langs
[i
])
245 s =
substr(" ", length (macros
[i
]))
246 print " " quote langs
[i
] quote
","
250 print "const unsigned int cl_options_count = N_OPTS;\n"
251 print "#if (1U << " n_langs
") > CL_MIN_OPTION_CLASS"
252 print " #error the number of languages exceeds the implementation limit"
254 print "const unsigned int cl_lang_count = " n_langs
";\n"
256 print "const struct cl_option cl_options[] =\n{"
259 for (i =
0; i
< n_opts
; i
++) {
260 back_chain
[i
] =
"N_OPTS";
261 indices
[opts
[i
]] = j
;
262 # Combine the flags of identical switches. Switches
263 # appear many times if they are handled by many front
265 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
266 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
267 if (help
[i
+ 1] ==
"")
268 help
[i
+ 1] = help
[i
]
269 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
])
270 print "#error Multiple different help strings for " \
271 opts
[i
] ":\n\t" help
[i
] "\n\t" help
[i
+ 1]
274 back_chain
[i
] =
"N_OPTS";
275 indices
[opts
[i
]] = j
;
281 for (i =
0; i
< n_opts
; i
++) {
282 # With identical flags, pick only the last one. The
283 # earlier loop ensured that it has all flags merged,
284 # and a nonempty help text if one of the texts was nonempty.
285 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
289 len =
length (opts
[i
]);
290 enum = opt_enum
(opts
[i
])
292 # If this switch takes joined arguments, back-chain all
293 # subsequent switches to it for which it is a prefix. If
294 # a later switch S is a longer prefix of a switch T, T
295 # will be back-chained to S in a later iteration of this
296 # for() loop, which is what we want.
297 if (flag_set_p
("Joined.*", flags
[i
])) {
298 for (j = i
+ 1; j
< n_opts
; j
++) {
299 if (substr (opts
[j
], 1, len
) != opts
[i
])
301 back_chain
[j
] = enum
;
305 s =
substr(" ", length (opts
[i
]))
312 hlp = quote help
[i
] quote
;
314 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
315 if (missing_arg_error ==
"")
316 missing_arg_error =
"NULL"
318 missing_arg_error = quote missing_arg_error quote
321 warn_message = opt_args
("Warn", flags
[i
])
322 if (warn_message ==
"")
323 warn_message =
"NULL"
325 warn_message = quote warn_message quote
327 alias_arg = opt_args
("Alias", flags
[i
])
328 if (alias_arg ==
"") {
329 if (flag_set_p
("Ignore", flags
[i
])) {
330 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
331 if (warn_message
!= "NULL")
332 print "#error Ignored option with Warn"
333 if (var_name
(flags
[i
]) != "")
334 print "#error Ignored option with Var"
336 else if (flag_set_p
("Deprecated", flags
[i
]))
337 print "#error Deprecated was replaced with WarnRemoved"
338 else if (flag_set_p
("WarnRemoved", flags
[i
])) {
339 alias_data =
"NULL, NULL, OPT_SPECIAL_warn_removed"
340 if (warn_message
!= "NULL")
341 print "#error WarnRemoved option with Warn"
344 alias_data =
"NULL, NULL, N_OPTS"
345 if (flag_set_p
("Enum.*", flags
[i
])) {
346 if (!flag_set_p
("RejectNegative", flags
[i
]) \
347 && opts
[i
] ~
"^[Wfgm]")
348 print "#error Enum allowing negative form"
351 alias_opt = nth_arg
(0, alias_arg
)
352 alias_posarg = nth_arg
(1, alias_arg
)
353 alias_negarg = nth_arg
(2, alias_arg
)
355 if (var_ref
(opts
[i
], flags
[i
]) != "(unsigned short) -1")
356 print "#error Alias setting variable"
358 if (alias_posarg
!= "" && alias_negarg ==
"") {
359 if (!flag_set_p
("RejectNegative", flags
[i
]) \
360 && opts
[i
] ~
"^[Wfm]")
361 print "#error Alias with single argument " \
362 "allowing negative form"
364 if (alias_posarg
!= "" \
365 && flag_set_p
("NegativeAlias", flags
[i
])) {
366 print "#error Alias with multiple arguments " \
367 "used with NegativeAlias"
370 alias_opt = opt_enum
(alias_opt
)
371 if (alias_posarg ==
"")
372 alias_posarg =
"NULL"
374 alias_posarg = quote alias_posarg quote
375 if (alias_negarg ==
"")
376 alias_negarg =
"NULL"
378 alias_negarg = quote alias_negarg quote
379 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
382 neg = opt_args
("Negative", flags
[i
]);
386 if (flag_set_p
("RejectNegative", flags
[i
]))
389 if (opts
[i
] ~
"^[Wfgm]")
390 idx = indices
[opts
[i
]];
395 # Split the printf after %u to work around an ia64-hp-hpux11.23
397 printf(" /* [%i] = */ {\n", optindex
)
398 printf(" %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
399 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
400 alias_data
, back_chain
[i
], len
)
401 printf(" /* .neg_idx = */ %d,\n", idx
)
402 condition = opt_args
("Condition", flags
[i
])
403 cl_flags = switch_flags
(flags
[i
])
404 cl_bit_fields = switch_bit_fields
(flags
[i
])
405 cl_zero_bit_fields = switch_bit_fields
("")
412 " 1 /* Disabled. */, %s,\n" \
414 condition
, cl_flags
, cl_bit_fields
, cl_zero_bit_fields
)
418 cl_flags
, cl_bit_fields
)
419 printf(" %s, %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
420 var_set
(flags
[i
]), integer_range_info
(opt_args
("IntegerRange", flags
[i
]),
421 opt_args
("Init", flags
[i
]), opts
[i
]), comma
)
423 # Bump up the informational option index.
431 print "common_handle_option_auto (struct gcc_options *opts, "
432 print " struct gcc_options *opts_set, "
433 print " const struct cl_decoded_option *decoded, "
434 print " unsigned int lang_mask, int kind, "
435 print " location_t loc, "
436 print " const struct cl_option_handlers *handlers, "
437 print " diagnostic_context *dc) "
439 print " size_t scode = decoded->opt_index; "
440 print " HOST_WIDE_INT value = decoded->value; "
441 print " enum opt_code code = (enum opt_code) scode; "
443 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
445 print " switch (code) "
448 for (i =
0; i
< n_enabledby
; i
++) {
449 enabledby_name = enabledby
[i
];
450 print " case " opt_enum
(enabledby_name
) ":"
451 n_enables =
split(enables
[enabledby_name
], thisenable
, ";");
452 n_enablesif =
split(enablesif
[enabledby_name
], thisenableif
, ";");
453 if (n_enables
!= n_enablesif
) {
454 print "#error n_enables != n_enablesif: Something went wrong!"
456 for (j =
1; j
< n_enables
; j
++) {
457 opt_var_name = var_name
(flags
[opt_numbers
[thisenable
[j
]]]);
458 if (opt_var_name
!= "") {
459 condition =
"!opts_set->x_" opt_var_name
460 if (thisenableif
[j
] != "") {
461 value =
"(" thisenableif
[j
] ")"
465 print " if (" condition
")"
466 print " handle_generated_option (opts, opts_set,"
467 print " " opt_enum
(thisenable
[j
]) ", NULL, " value
","
468 print " lang_mask, kind, loc, handlers, true, dc);"
470 print "#error " thisenable
[j
] " does not have a Var() flag"
478 print " return true; "
481 # Handle LangEnabledBy
482 for (i =
0; i
< n_langs
; i
++) {
483 lang_name = lang_sanitized_name
(langs
[i
]);
484 mark_unused =
" ATTRIBUTE_UNUSED";
488 print lang_name
"_handle_option_auto (struct gcc_options *opts" mark_unused
", "
489 print " struct gcc_options *opts_set" mark_unused
", "
490 print " size_t scode" mark_unused
", const char *arg" mark_unused
", HOST_WIDE_INT value" mark_unused
", "
491 print " unsigned int lang_mask" mark_unused
", int kind" mark_unused
", "
492 print " location_t loc" mark_unused
", "
493 print " const struct cl_option_handlers *handlers" mark_unused
", "
494 print " diagnostic_context *dc" mark_unused
") "
496 print " enum opt_code code = (enum opt_code) scode; "
498 print " switch (code) "
501 for (k =
0; k
< n_enabledby_lang
[i
]; k
++) {
502 enabledby_name = enabledby
[lang_name
,k
];
503 print " case " opt_enum
(enabledby_name
) ":"
504 n_thisenable =
split(enables
[lang_name
,enabledby_name
], thisenable
, ";");
505 for (j =
1; j
< n_thisenable
; j
++) {
506 n_thisenable_args =
split(thisenable
[j
], thisenable_args
, ",");
507 if (n_thisenable_args ==
1) {
508 thisenable_opt = thisenable
[j
];
511 thisenable_opt = thisenable_args
[1];
512 with_posarg = thisenable_args
[2];
513 with_negarg = thisenable_args
[3];
514 value =
"value ? " with_posarg
" : " with_negarg
;
516 opt_var_name = var_name
(flags
[opt_numbers
[thisenable_opt
]]);
517 if (opt_var_name
!= "") {
518 print " if (!opts_set->x_" opt_var_name
")"
519 print " handle_generated_option (opts, opts_set,"
520 print " " opt_enum
(thisenable_opt
) ", NULL, " value
","
521 print " lang_mask, kind, loc, handlers, true, dc);"
523 print "#error " thisenable_opt
" does not have a Var() flag"
531 print " return true; "
537 print "#include " quote
"cpplib.h" quote
;
539 print "cpp_handle_option_auto (const struct gcc_options * opts, "
540 print " size_t scode, struct cpp_options * cpp_opts)"
542 print " enum opt_code code = (enum opt_code) scode; "
544 print " switch (code) "
546 for (i =
0; i
< n_opts
; i
++) {
547 # With identical flags, pick only the last one. The
548 # earlier loop ensured that it has all flags merged,
549 # and a nonempty help text if one of the texts was nonempty.
550 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
554 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
555 if (cpp_option
!= "") {
556 opt_var_name = var_name
(flags
[i
]);
557 init = opt_args
("Init", flags
[i
])
558 if (opt_var_name
!= "" && init
!= "") {
559 print " case " opt_enum
(opts
[i
]) ":"
560 print " cpp_opts->" cpp_option
" = opts->x_" opt_var_name
";"
562 } else if (opt_var_name ==
"" && init ==
"") {
563 print "#error CPP() requires setting Init() and Var() for " opts
[i
]
564 } else if (opt_var_name
!= "") {
565 print "#error CPP() requires setting Init() for " opts
[i
]
567 print "#error CPP() requires setting Var() for " opts
[i
]
576 print "init_global_opts_from_cpp(struct gcc_options * opts, "
577 print " const struct cpp_options * cpp_opts)"
579 for (i =
0; i
< n_opts
; i
++) {
580 # With identical flags, pick only the last one. The
581 # earlier loop ensured that it has all flags merged,
582 # and a nonempty help text if one of the texts was nonempty.
583 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
586 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
587 opt_var_name = var_name
(flags
[i
]);
588 if (cpp_option
!= "" && opt_var_name
!= "") {
589 print " opts->x_" opt_var_name
" = cpp_opts->" cpp_option
";"
594 split("", var_seen
, ":")
595 print "\n#if !defined(GENERATOR_FILE) && defined(ENABLE_PLUGIN)"
596 print "DEBUG_VARIABLE const struct cl_var cl_vars[] =\n{"
598 for (i =
0; i
< n_opts
; i
++) {
599 name = var_name
(flags
[i
]);
605 for (i =
0; i
< n_extra_vars
; i
++) {
607 sub(" *=.*", "", var
)
609 sub("^.*[ *]", "", name
)
610 sub("\\[.*\\]$", "", name
)
611 if (name in var_seen
)
613 print " { " quote name quote
", offsetof (struct gcc_options, x_" name
") },"
617 print " { NULL, (unsigned short) -1 }\n};\n#endif"