1 # Copyright (C) 2003-2022 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.cc
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_n_args = n_args
(enabledby_arg
)
102 if (enabledby_n_args
!= 2 \
103 && enabledby_n_args
!= 4) {
104 print "#error " opts
[i
] " LangEnabledBy(" enabledby_arg
") must specify two or four arguments"
107 enabledby_langs = nth_arg
(0, enabledby_arg
);
108 if (enabledby_langs ==
"")
109 print "#error " opts
[i
] " LangEnabledBy(" enabledby_arg
") must specify LANGUAGE"
110 enabledby_opt = nth_arg
(1, enabledby_arg
);
111 if (enabledby_opt ==
"")
112 print "#error " opts
[i
] " LangEnabledBy(" enabledby_arg
") must specify OPT"
114 enabledby_posarg_negarg =
""
115 if (enabledby_n_args ==
4) {
116 enabledby_posarg = nth_arg
(2, enabledby_arg
);
117 enabledby_negarg = nth_arg
(3, enabledby_arg
);
118 if (enabledby_posarg ==
"" \
119 || enabledby_negarg ==
"")
120 print "#error " opts
[i
] " LangEnabledBy(" enabledby_arg
") with four arguments must specify POSARG and NEGARG"
122 enabledby_posarg_negarg =
"," enabledby_posarg
"," enabledby_negarg
125 n_enabledby_arg_langs =
split(enabledby_langs
, enabledby_arg_langs
, " ");
126 n_enabledby_array =
split(enabledby_opt
, enabledby_array
, " \\|\\| ");
127 for (k =
1; k
<= n_enabledby_array
; k
++) {
128 enabledby_index = opt_numbers
[enabledby_array
[k
]];
129 if (enabledby_index ==
"") {
130 print "#error " opts
[i
] " LangEnabledBy(" enabledby_arg
"), unknown option '" enabledby_opt
"'"
134 for (j =
1; j
<= n_enabledby_arg_langs
; j
++) {
135 lang_name = enabledby_arg_langs
[j
]
136 lang_index = lang_numbers
[lang_name
];
137 if (lang_index ==
"") {
138 print "// #error " opts
[i
] " LangEnabledBy(" enabledby_arg
"), unknown language '" lang_name
"'"
142 lang_name = lang_sanitized_name
(lang_name
);
144 if (enables
[lang_name
,enabledby_array
[k
]] ==
"") {
145 enabledby
[lang_name
,n_enabledby_lang
[lang_index
]] = enabledby_array
[k
];
146 n_enabledby_lang
[lang_index
]++;
148 enables
[lang_name
,enabledby_array
[k
]] \
149 = enables
[lang_name
,enabledby_array
[k
]] opts
[i
] enabledby_posarg_negarg
";";
154 if (flag_set_p
("Param", flags
[i
]) && !
(opts
[i
] ~
"^-param="))
155 print "#error Parameter option name '" opts
[i
] "' must start with '-param='"
159 print "/* This file is auto-generated by optc-gen.awk. */"
161 n_headers =
split(header_name
, headers
, " ")
162 for (i =
1; i
<= n_headers
; i
++)
163 print "#include " quote headers
[i
] quote
164 print "#include " quote
"opts.h" quote
165 print "#include " quote
"intl.h" quote
166 print "#include " quote
"insn-attr-common.h" quote
169 if (n_extra_c_includes
> 0) {
170 for (i =
0; i
< n_extra_c_includes
; i
++) {
171 print "#include " quote extra_c_includes
[i
] quote
176 for (i =
0; i
< n_enums
; i
++) {
178 type = enum_type
[name
]
179 print "static const struct cl_enum_arg cl_enum_" name \
182 print enum_data
[name
] " { NULL, 0, 0 }"
186 print "cl_enum_" name
"_set (void *var, int value)"
188 print " *((" type
" *) var) = (" type
") value;"
192 print "cl_enum_" name
"_get (const void *var)"
194 print " return (int) *((const " type
" *) var);"
199 print "const struct cl_enum cl_enums[] ="
201 for (i =
0; i
< n_enums
; i
++) {
203 ehelp = enum_help
[name
]
207 ehelp = quote ehelp quote
208 unknown_error = enum_unknown_error
[name
]
209 if (unknown_error ==
"")
210 unknown_error =
"NULL"
212 unknown_error = quote unknown_error quote
215 print " " unknown_error
","
216 print " cl_enum_" name
"_data,"
217 print " sizeof (" enum_type
[name
] "),"
218 print " cl_enum_" name
"_set,"
219 print " cl_enum_" name
"_get"
223 print "const unsigned int cl_enums_count = " n_enums
";"
226 print "const struct gcc_options global_options_init =\n{"
227 for (i =
0; i
< n_extra_vars
; i
++) {
231 sub(".*= *", "", init
)
235 sub(" *=.*", "", var
)
237 sub("^.*[ *]", "", name
)
238 sub("\\[.*\\]$", "", name
)
240 print " " init
", /* " name
" */"
242 for (i =
0; i
< n_opts
; i
++) {
243 name = var_name
(flags
[i
]);
244 init = opt_args
("Init", flags
[i
])
248 print "#error " opts
[i
] " must specify Var to use Init"
253 if (name in var_init
&& var_init
[name
] != init
)
254 print "#error multiple initializers for " name
255 var_init
[name
] = init
258 for (i =
0; i
< n_opts
; i
++) {
259 name = var_name
(flags
[i
]);
263 if (name in var_seen
)
266 if (name in var_init
)
267 init = var_init
[name
]
271 print " " init
", /* " name
" */"
275 for (i =
0; i
< n_opts
; i
++) {
276 name = static_var
(opts
[i
], flags
[i
]);
278 print " 0, /* " name
" (private state) */"
279 print "#undef x_" name
282 for (i =
0; i
< n_opts
; i
++) {
283 if (flag_set_p
("SetByCombined", flags
[i
]))
284 print " false, /* frontend_set_" var_name
(flags
[i
]) " */"
288 print "struct gcc_options global_options;"
289 print "struct gcc_options global_options_set;"
292 print "const char * const lang_names[] =\n{"
293 for (i =
0; i
< n_langs
; i
++) {
294 macros
[i
] =
"CL_" lang_sanitized_name
(langs
[i
])
295 s =
substr(" ", length (macros
[i
]))
296 print " " quote langs
[i
] quote
","
300 print "const unsigned int cl_options_count = N_OPTS;\n"
301 print "#if (1U << " n_langs
") > CL_MIN_OPTION_CLASS"
302 print " #error the number of languages exceeds the implementation limit"
304 print "const unsigned int cl_lang_count = " n_langs
";\n"
306 print "const struct cl_option cl_options[] =\n{"
309 for (i =
0; i
< n_opts
; i
++) {
310 back_chain
[i
] =
"N_OPTS";
311 indices
[opts
[i
]] = j
;
312 # Combine the flags of identical switches. Switches
313 # appear many times if they are handled by many front
315 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
316 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
317 if (help
[i
+ 1] ==
"")
318 help
[i
+ 1] = help
[i
]
319 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
]) {
320 print "#error Multiple different help strings for " \
322 print "#error " help
[i
]
323 print "#error " help
[i
+ 1]
327 back_chain
[i
] =
"N_OPTS";
328 indices
[opts
[i
]] = j
;
334 for (i =
0; i
< n_opts
; i
++) {
335 # With identical flags, pick only the last one. The
336 # earlier loop ensured that it has all flags merged,
337 # and a nonempty help text if one of the texts was nonempty.
338 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
342 len =
length (opts
[i
]);
343 enum = opt_enum
(opts
[i
])
345 # If this switch takes joined arguments, back-chain all
346 # subsequent switches to it for which it is a prefix. If
347 # a later switch S is a longer prefix of a switch T, T
348 # will be back-chained to S in a later iteration of this
349 # for() loop, which is what we want.
350 if (flag_set_p
("Joined.*", flags
[i
])) {
351 for (j = i
+ 1; j
< n_opts
; j
++) {
352 if (substr (opts
[j
], 1, len
) != opts
[i
])
354 back_chain
[j
] = enum
;
358 s =
substr(" ", length (opts
[i
]))
365 hlp = quote help
[i
] quote
;
367 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
368 if (missing_arg_error ==
"")
369 missing_arg_error =
"NULL"
371 missing_arg_error = quote missing_arg_error quote
374 warn_message = opt_args
("Warn", flags
[i
])
375 if (warn_message ==
"")
376 warn_message =
"NULL"
378 warn_message = quote warn_message quote
380 alias_arg = opt_args
("Alias", flags
[i
])
381 if (alias_arg ==
"") {
382 if (flag_set_p
("Ignore", flags
[i
])) {
383 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
384 if (warn_message
!= "NULL")
385 print "#error Ignored option with Warn"
386 if (var_name
(flags
[i
]) != "")
387 print "#error Ignored option with Var"
389 else if (flag_set_p
("Deprecated", flags
[i
]))
390 print "#error Deprecated was replaced with WarnRemoved"
391 else if (flag_set_p
("WarnRemoved", flags
[i
])) {
392 alias_data =
"NULL, NULL, OPT_SPECIAL_warn_removed"
393 if (warn_message
!= "NULL")
394 print "#error WarnRemoved option with Warn"
397 alias_data =
"NULL, NULL, N_OPTS"
398 if (flag_set_p
("Enum.*", flags
[i
])) {
399 if (!flag_set_p
("RejectNegative", flags
[i
]) \
400 && !flag_set_p
("EnumSet", flags
[i
]) \
401 && !flag_set_p
("EnumBitSet", flags
[i
]) \
402 && opts
[i
] ~
"^[Wfgm]")
403 print "#error Enum allowing negative form"
406 alias_opt = nth_arg
(0, alias_arg
)
407 alias_posarg = nth_arg
(1, alias_arg
)
408 alias_negarg = nth_arg
(2, alias_arg
)
410 if (var_ref
(opts
[i
], flags
[i
]) != "(unsigned short) -1")
411 print "#error Alias setting variable"
413 if (alias_posarg
!= "" && alias_negarg ==
"") {
414 if (!flag_set_p
("RejectNegative", flags
[i
]) \
415 && opts
[i
] ~
"^[Wfm]")
416 print "#error Alias with single argument " \
417 "allowing negative form"
419 if (alias_posarg
!= "" \
420 && flag_set_p
("NegativeAlias", flags
[i
])) {
421 print "#error Alias with multiple arguments " \
422 "used with NegativeAlias"
425 alias_opt = opt_enum
(alias_opt
)
426 if (alias_posarg ==
"")
427 alias_posarg =
"NULL"
429 alias_posarg = quote alias_posarg quote
430 if (alias_negarg ==
"")
431 alias_negarg =
"NULL"
433 alias_negarg = quote alias_negarg quote
434 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
437 neg = opt_args
("Negative", flags
[i
]);
441 if (flag_set_p
("RejectNegative", flags
[i
]))
444 if (opts
[i
] ~
"^[Wfgm]")
445 idx = indices
[opts
[i
]];
450 # Split the printf after %u to work around an ia64-hp-hpux11.23
452 printf(" /* [%i] = */ {\n", optindex
)
453 printf(" %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
454 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
455 alias_data
, back_chain
[i
], len
)
456 printf(" /* .neg_idx = */ %d,\n", idx
)
457 condition = opt_args
("Condition", flags
[i
])
458 cl_flags = switch_flags
(flags
[i
])
459 cl_bit_fields = switch_bit_fields
(flags
[i
])
460 cl_zero_bit_fields = switch_bit_fields
("")
467 " 1 /* Disabled. */, %s,\n" \
469 condition
, cl_flags
, cl_bit_fields
, cl_zero_bit_fields
)
473 cl_flags
, cl_bit_fields
)
474 printf(" %s, %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
475 var_set
(flags
[i
]), integer_range_info
(opt_args
("IntegerRange", flags
[i
]),
476 opt_args
("Init", flags
[i
]), opts
[i
], flag_set_p
("UInteger", flags
[i
])), comma
)
478 # Bump up the informational option index.
486 print "common_handle_option_auto (struct gcc_options *opts, "
487 print " struct gcc_options *opts_set, "
488 print " const struct cl_decoded_option *decoded, "
489 print " unsigned int lang_mask, int kind, "
490 print " location_t loc, "
491 print " const struct cl_option_handlers *handlers, "
492 print " diagnostic_context *dc) "
494 print " size_t scode = decoded->opt_index; "
495 print " HOST_WIDE_INT value = decoded->value; "
496 print " enum opt_code code = (enum opt_code) scode; "
498 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
500 print " switch (code) "
503 for (i =
0; i
< n_enabledby
; i
++) {
504 enabledby_name = enabledby
[i
];
505 print " case " opt_enum
(enabledby_name
) ":"
506 n_enables =
split(enables
[enabledby_name
], thisenable
, ";");
507 n_enablesif =
split(enablesif
[enabledby_name
], thisenableif
, ";");
508 if (n_enables
!= n_enablesif
) {
509 print "#error n_enables != n_enablesif: Something went wrong!"
511 for (j =
1; j
< n_enables
; j
++) {
512 opt_var_name = var_name
(flags
[opt_numbers
[thisenable
[j
]]]);
513 if (opt_var_name
!= "") {
514 condition =
"!opts_set->x_" opt_var_name
515 if (thisenableif
[j
] != "") {
516 value =
"(" thisenableif
[j
] ")"
520 print " if (" condition
")"
521 print " handle_generated_option (opts, opts_set,"
522 print " " opt_enum
(thisenable
[j
]) ", NULL, " value
","
523 print " lang_mask, kind, loc, handlers, true, dc);"
525 print "#error " thisenable
[j
] " does not have a Var() flag"
533 print " return true; "
536 # Handle LangEnabledBy
537 for (i =
0; i
< n_langs
; i
++) {
538 lang_name = lang_sanitized_name
(langs
[i
]);
539 mark_unused =
" ATTRIBUTE_UNUSED";
543 print lang_name
"_handle_option_auto (struct gcc_options *opts" mark_unused
", "
544 print " struct gcc_options *opts_set" mark_unused
", "
545 print " size_t scode" mark_unused
", const char *arg" mark_unused
", HOST_WIDE_INT value" mark_unused
", "
546 print " unsigned int lang_mask" mark_unused
", int kind" mark_unused
", "
547 print " location_t loc" mark_unused
", "
548 print " const struct cl_option_handlers *handlers" mark_unused
", "
549 print " diagnostic_context *dc" mark_unused
") "
551 print " enum opt_code code = (enum opt_code) scode; "
553 print " switch (code) "
556 for (k =
0; k
< n_enabledby_lang
[i
]; k
++) {
557 enabledby_name = enabledby
[lang_name
,k
];
558 print " case " opt_enum
(enabledby_name
) ":"
559 n_thisenable =
split(enables
[lang_name
,enabledby_name
], thisenable
, ";");
560 for (j =
1; j
< n_thisenable
; j
++) {
561 n_thisenable_args =
split(thisenable
[j
], thisenable_args
, ",");
562 if (n_thisenable_args ==
1) {
563 thisenable_opt = thisenable
[j
];
566 thisenable_opt = thisenable_args
[1];
567 with_posarg = thisenable_args
[2];
568 with_negarg = thisenable_args
[3];
569 value =
"value ? " with_posarg
" : " with_negarg
;
571 opt_var_name = var_name
(flags
[opt_numbers
[thisenable_opt
]]);
572 if (opt_var_name
!= "") {
573 print " if (!opts_set->x_" opt_var_name
")"
574 print " handle_generated_option (opts, opts_set,"
575 print " " opt_enum
(thisenable_opt
) ", NULL, " value
","
576 print " lang_mask, kind, loc, handlers, true, dc);"
578 print "#error " thisenable_opt
" does not have a Var() flag"
586 print " return true; "
592 print "#include " quote
"cpplib.h" quote
;
594 print "cpp_handle_option_auto (const struct gcc_options * opts, "
595 print " size_t scode, struct cpp_options * cpp_opts)"
597 print " enum opt_code code = (enum opt_code) scode; "
599 print " switch (code) "
601 for (i =
0; i
< n_opts
; i
++) {
602 # With identical flags, pick only the last one. The
603 # earlier loop ensured that it has all flags merged,
604 # and a nonempty help text if one of the texts was nonempty.
605 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
609 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
610 if (cpp_option
!= "") {
611 opt_var_name = var_name
(flags
[i
]);
612 init = opt_args
("Init", flags
[i
])
613 if (opt_var_name
!= "" && init
!= "") {
614 print " case " opt_enum
(opts
[i
]) ":"
615 print " cpp_opts->" cpp_option
" = opts->x_" opt_var_name
";"
617 } else if (opt_var_name ==
"" && init ==
"") {
618 print "#error CPP() requires setting Init() and Var() for " opts
[i
]
619 } else if (opt_var_name
!= "") {
620 print "#error CPP() requires setting Init() for " opts
[i
]
622 print "#error CPP() requires setting Var() for " opts
[i
]
631 print "init_global_opts_from_cpp(struct gcc_options * opts, "
632 print " const struct cpp_options * cpp_opts)"
634 for (i =
0; i
< n_opts
; i
++) {
635 # With identical flags, pick only the last one. The
636 # earlier loop ensured that it has all flags merged,
637 # and a nonempty help text if one of the texts was nonempty.
638 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
641 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
642 opt_var_name = var_name
(flags
[i
]);
643 if (cpp_option
!= "" && opt_var_name
!= "") {
644 print " opts->x_" opt_var_name
" = cpp_opts->" cpp_option
";"
649 split("", var_seen
, ":")
650 print "\n#if !defined(GENERATOR_FILE) && defined(ENABLE_PLUGIN)"
651 print "DEBUG_VARIABLE const struct cl_var cl_vars[] =\n{"
653 for (i =
0; i
< n_opts
; i
++) {
654 name = var_name
(flags
[i
]);
660 for (i =
0; i
< n_extra_vars
; i
++) {
662 sub(" *=.*", "", var
)
664 sub("^.*[ *]", "", name
)
665 sub("\\[.*\\]$", "", name
)
666 if (name in var_seen
)
668 print " { " quote name quote
", offsetof (struct gcc_options, x_" name
") },"
672 print " { NULL, (unsigned short) -1 }\n};\n#endif"