1 # Copyright (C) 2003-2024 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 # Do not allow Joined and Separate properties if
346 # an options ends with '='.
347 if (flag_set_p
("Joined", flags
[i
]) && flag_set_p
("Separate", flags
[i
]) && opts
[i
] ~
"=$") {
348 print "#error Option '" opts
[i
] "' ending with '=' cannot have " \
349 "both Joined and Separate properties"
352 # If this switch takes joined arguments, back-chain all
353 # subsequent switches to it for which it is a prefix. If
354 # a later switch S is a longer prefix of a switch T, T
355 # will be back-chained to S in a later iteration of this
356 # for() loop, which is what we want.
357 if (flag_set_p
("Joined.*", flags
[i
])) {
358 for (j = i
+ 1; j
< n_opts
; j
++) {
359 if (substr (opts
[j
], 1, len
) != opts
[i
])
361 back_chain
[j
] = enum
;
365 s =
substr(" ", length (opts
[i
]))
372 hlp = quote help
[i
] quote
;
374 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
375 if (missing_arg_error ==
"")
376 missing_arg_error =
"NULL"
378 missing_arg_error = quote missing_arg_error quote
381 warn_message = opt_args
("Warn", flags
[i
])
382 if (warn_message ==
"")
383 warn_message =
"NULL"
385 warn_message = quote warn_message quote
387 alias_arg = opt_args
("Alias", flags
[i
])
388 if (alias_arg ==
"") {
389 if (flag_set_p
("Ignore", flags
[i
])) {
390 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
391 if (warn_message
!= "NULL")
392 print "#error Ignored option with Warn"
393 if (var_name
(flags
[i
]) != "")
394 print "#error Ignored option with Var"
396 else if (flag_set_p
("Deprecated", flags
[i
]))
397 print "#error Deprecated was replaced with WarnRemoved"
398 else if (flag_set_p
("WarnRemoved", flags
[i
])) {
399 alias_data =
"NULL, NULL, OPT_SPECIAL_warn_removed"
400 if (warn_message
!= "NULL")
401 print "#error WarnRemoved option with Warn"
404 alias_data =
"NULL, NULL, N_OPTS"
405 if (flag_set_p
("Enum.*", flags
[i
])) {
406 if (!flag_set_p
("RejectNegative", flags
[i
]) \
407 && !flag_set_p
("EnumSet", flags
[i
]) \
408 && !flag_set_p
("EnumBitSet", flags
[i
]) \
409 && opts
[i
] ~
"^[Wfgm]")
410 print "#error Enum allowing negative form"
413 alias_opt = nth_arg
(0, alias_arg
)
414 alias_posarg = nth_arg
(1, alias_arg
)
415 alias_negarg = nth_arg
(2, alias_arg
)
417 if (var_ref
(opts
[i
], flags
[i
]) != "(unsigned short) -1")
418 print "#error Alias setting variable"
420 if (alias_posarg
!= "" && alias_negarg ==
"") {
421 if (!flag_set_p
("RejectNegative", flags
[i
]) \
422 && opts
[i
] ~
"^[Wfm]")
423 print "#error Alias with single argument " \
424 "allowing negative form"
426 if (alias_posarg
!= "" \
427 && flag_set_p
("NegativeAlias", flags
[i
])) {
428 print "#error Alias with multiple arguments " \
429 "used with NegativeAlias"
432 alias_opt = opt_enum
(alias_opt
)
433 if (alias_posarg ==
"")
434 alias_posarg =
"NULL"
436 alias_posarg = quote alias_posarg quote
437 if (alias_negarg ==
"")
438 alias_negarg =
"NULL"
440 alias_negarg = quote alias_negarg quote
441 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
444 neg = opt_args
("Negative", flags
[i
]);
448 if (flag_set_p
("RejectNegative", flags
[i
]))
451 if (opts
[i
] ~
"^[Wfgm]")
452 idx = indices
[opts
[i
]];
457 # Split the printf after %u to work around an ia64-hp-hpux11.23
459 printf(" /* [%i] = */ {\n", optindex
)
460 printf(" %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
461 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
462 alias_data
, back_chain
[i
], len
)
463 printf(" /* .neg_idx = */ %d,\n", idx
)
464 condition = opt_args
("Condition", flags
[i
])
465 cl_flags = switch_flags
(flags
[i
])
466 cl_bit_fields = switch_bit_fields
(flags
[i
])
467 cl_zero_bit_fields = switch_bit_fields
("")
474 " 1 /* Disabled. */, %s,\n" \
476 condition
, cl_flags
, cl_bit_fields
, cl_zero_bit_fields
)
480 cl_flags
, cl_bit_fields
)
481 printf(" %s, %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
482 var_set
(flags
[i
]), integer_range_info
(opt_args
("IntegerRange", flags
[i
]),
483 opt_args
("Init", flags
[i
]), opts
[i
], flag_set_p
("UInteger", flags
[i
])), comma
)
485 # Bump up the informational option index.
493 print "common_handle_option_auto (struct gcc_options *opts, "
494 print " struct gcc_options *opts_set, "
495 print " const struct cl_decoded_option *decoded, "
496 print " unsigned int lang_mask, int kind, "
497 print " location_t loc, "
498 print " const struct cl_option_handlers *handlers, "
499 print " diagnostic_context *dc) "
501 print " size_t scode = decoded->opt_index; "
502 print " HOST_WIDE_INT value = decoded->value; "
503 print " enum opt_code code = (enum opt_code) scode; "
505 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
507 print " switch (code) "
510 for (i =
0; i
< n_enabledby
; i
++) {
511 enabledby_name = enabledby
[i
];
512 print " case " opt_enum
(enabledby_name
) ":"
513 n_enables =
split(enables
[enabledby_name
], thisenable
, ";");
514 n_enablesif =
split(enablesif
[enabledby_name
], thisenableif
, ";");
515 if (n_enables
!= n_enablesif
) {
516 print "#error n_enables != n_enablesif: Something went wrong!"
518 for (j =
1; j
< n_enables
; j
++) {
519 opt_var_name = var_name
(flags
[opt_numbers
[thisenable
[j
]]]);
520 if (opt_var_name
!= "") {
521 condition =
"!opts_set->x_" opt_var_name
522 if (thisenableif
[j
] != "") {
523 value =
"(" thisenableif
[j
] ")"
527 print " if (" condition
")"
528 print " handle_generated_option (opts, opts_set,"
529 print " " opt_enum
(thisenable
[j
]) ", NULL, " value
","
530 print " lang_mask, kind, loc, handlers, true, dc);"
532 print "#error " thisenable
[j
] " does not have a Var() flag"
540 print " return true; "
543 # Handle LangEnabledBy
544 for (i =
0; i
< n_langs
; i
++) {
545 lang_name = lang_sanitized_name
(langs
[i
]);
546 mark_unused =
" ATTRIBUTE_UNUSED";
550 print lang_name
"_handle_option_auto (struct gcc_options *opts" mark_unused
", "
551 print " struct gcc_options *opts_set" mark_unused
", "
552 print " size_t scode" mark_unused
", const char *arg" mark_unused
", HOST_WIDE_INT value" mark_unused
", "
553 print " unsigned int lang_mask" mark_unused
", int kind" mark_unused
", "
554 print " location_t loc" mark_unused
", "
555 print " const struct cl_option_handlers *handlers" mark_unused
", "
556 print " diagnostic_context *dc" mark_unused
") "
558 print " enum opt_code code = (enum opt_code) scode; "
560 print " switch (code) "
563 for (k =
0; k
< n_enabledby_lang
[i
]; k
++) {
564 enabledby_name = enabledby
[lang_name
,k
];
565 print " case " opt_enum
(enabledby_name
) ":"
566 n_thisenable =
split(enables
[lang_name
,enabledby_name
], thisenable
, ";");
567 for (j =
1; j
< n_thisenable
; j
++) {
568 n_thisenable_args =
split(thisenable
[j
], thisenable_args
, ",");
569 if (n_thisenable_args ==
1) {
570 thisenable_opt = thisenable
[j
];
573 thisenable_opt = thisenable_args
[1];
574 with_posarg = thisenable_args
[2];
575 with_negarg = thisenable_args
[3];
576 value =
"value ? " with_posarg
" : " with_negarg
;
578 opt_var_name = var_name
(flags
[opt_numbers
[thisenable_opt
]]);
579 if (opt_var_name
!= "") {
580 print " if (!opts_set->x_" opt_var_name
")"
581 print " handle_generated_option (opts, opts_set,"
582 print " " opt_enum
(thisenable_opt
) ", NULL, " value
","
583 print " lang_mask, kind, loc, handlers, true, dc);"
585 print "#error " thisenable_opt
" does not have a Var() flag"
593 print " return true; "
599 print "#include " quote
"cpplib.h" quote
;
601 print "cpp_handle_option_auto (const struct gcc_options * opts, "
602 print " size_t scode, struct cpp_options * cpp_opts)"
604 print " enum opt_code code = (enum opt_code) scode; "
606 print " switch (code) "
608 for (i =
0; i
< n_opts
; i
++) {
609 # With identical flags, pick only the last one. The
610 # earlier loop ensured that it has all flags merged,
611 # and a nonempty help text if one of the texts was nonempty.
612 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
616 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
617 if (cpp_option
!= "") {
618 opt_var_name = var_name
(flags
[i
]);
619 init = opt_args
("Init", flags
[i
])
620 if (opt_var_name
!= "" && init
!= "") {
621 print " case " opt_enum
(opts
[i
]) ":"
622 print " cpp_opts->" cpp_option
" = opts->x_" opt_var_name
";"
624 } else if (opt_var_name ==
"" && init ==
"") {
625 print "#error CPP() requires setting Init() and Var() for " opts
[i
]
626 } else if (opt_var_name
!= "") {
627 print "#error CPP() requires setting Init() for " opts
[i
]
629 print "#error CPP() requires setting Var() for " opts
[i
]
638 print "init_global_opts_from_cpp(struct gcc_options * opts, "
639 print " const struct cpp_options * cpp_opts)"
641 for (i =
0; i
< n_opts
; i
++) {
642 # With identical flags, pick only the last one. The
643 # earlier loop ensured that it has all flags merged,
644 # and a nonempty help text if one of the texts was nonempty.
645 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
648 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
649 opt_var_name = var_name
(flags
[i
]);
650 if (cpp_option
!= "" && opt_var_name
!= "") {
651 print " opts->x_" opt_var_name
" = cpp_opts->" cpp_option
";"
656 split("", var_seen
, ":")
657 print "\n#if !defined(GENERATOR_FILE) && defined(ENABLE_PLUGIN)"
658 print "DEBUG_VARIABLE const struct cl_var cl_vars[] =\n{"
660 for (i =
0; i
< n_opts
; i
++) {
661 name = var_name
(flags
[i
]);
667 for (i =
0; i
< n_extra_vars
; i
++) {
669 sub(" *=.*", "", var
)
671 sub("^.*[ *]", "", name
)
672 sub("\\[.*\\]$", "", name
)
673 if (name in var_seen
)
675 print " { " quote name quote
", offsetof (struct gcc_options, x_" name
") },"
679 print " { NULL, (unsigned short) -1 }\n};\n#endif"