1 # Copyright (C) 2003-2017 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
);
110 print "/* This file is auto-generated by optc-gen.awk. */"
112 n_headers =
split(header_name
, headers
, " ")
113 for (i =
1; i
<= n_headers
; i
++)
114 print "#include " quote headers
[i
] quote
115 print "#include " quote
"opts.h" quote
116 print "#include " quote
"intl.h" quote
117 print "#include " quote
"insn-attr-common.h" quote
120 if (n_extra_c_includes
> 0) {
121 for (i =
0; i
< n_extra_c_includes
; i
++) {
122 print "#include " quote extra_c_includes
[i
] quote
127 for (i =
0; i
< n_enums
; i
++) {
129 type = enum_type
[name
]
130 print "static const struct cl_enum_arg cl_enum_" name \
133 print enum_data
[name
] " { NULL, 0, 0 }"
137 print "cl_enum_" name
"_set (void *var, int value)"
139 print " *((" type
" *) var) = (" type
") value;"
143 print "cl_enum_" name
"_get (const void *var)"
145 print " return (int) *((const " type
" *) var);"
150 print "const struct cl_enum cl_enums[] ="
152 for (i =
0; i
< n_enums
; i
++) {
154 ehelp = enum_help
[name
]
158 ehelp = quote ehelp quote
159 unknown_error = enum_unknown_error
[name
]
160 if (unknown_error ==
"")
161 unknown_error =
"NULL"
163 unknown_error = quote unknown_error quote
166 print " " unknown_error
","
167 print " cl_enum_" name
"_data,"
168 print " sizeof (" enum_type
[name
] "),"
169 print " cl_enum_" name
"_set,"
170 print " cl_enum_" name
"_get"
174 print "const unsigned int cl_enums_count = " n_enums
";"
177 print "const struct gcc_options global_options_init =\n{"
178 for (i =
0; i
< n_extra_vars
; i
++) {
182 sub(".*= *", "", init
)
186 sub(" *=.*", "", var
)
188 sub("^.*[ *]", "", name
)
189 sub("\\[.*\\]$", "", name
)
191 print " " init
", /* " name
" */"
193 for (i =
0; i
< n_opts
; i
++) {
194 name = var_name
(flags
[i
]);
198 init = opt_args
("Init", flags
[i
])
200 if (name in var_init
&& var_init
[name
] != init
)
201 print "#error multiple initializers for " name
202 var_init
[name
] = init
205 for (i =
0; i
< n_opts
; i
++) {
206 name = var_name
(flags
[i
]);
210 if (name in var_seen
)
213 if (name in var_init
)
214 init = var_init
[name
]
218 print " " init
", /* " name
" */"
222 for (i =
0; i
< n_opts
; i
++) {
223 name = static_var
(opts
[i
], flags
[i
]);
225 print " 0, /* " name
" (private state) */"
226 print "#undef x_" name
229 for (i =
0; i
< n_opts
; i
++) {
230 if (flag_set_p
("SetByCombined", flags
[i
]))
231 print " false, /* frontend_set_" var_name
(flags
[i
]) " */"
235 print "struct gcc_options global_options;"
236 print "struct gcc_options global_options_set;"
239 print "const char * const lang_names[] =\n{"
240 for (i =
0; i
< n_langs
; i
++) {
241 macros
[i
] =
"CL_" lang_sanitized_name
(langs
[i
])
242 s =
substr(" ", length (macros
[i
]))
243 print " " quote langs
[i
] quote
","
247 print "const unsigned int cl_options_count = N_OPTS;\n"
248 print "#if (1U << " n_langs
") > CL_MIN_OPTION_CLASS"
249 print " #error the number of languages exceeds the implementation limit"
251 print "const unsigned int cl_lang_count = " n_langs
";\n"
253 print "const struct cl_option cl_options[] =\n{"
256 for (i =
0; i
< n_opts
; i
++) {
257 back_chain
[i
] =
"N_OPTS";
258 indices
[opts
[i
]] = j
;
259 # Combine the flags of identical switches. Switches
260 # appear many times if they are handled by many front
262 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
263 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
264 if (help
[i
+ 1] ==
"")
265 help
[i
+ 1] = help
[i
]
266 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
])
267 print "#error Multiple different help strings for " \
268 opts
[i
] ":\n\t" help
[i
] "\n\t" help
[i
+ 1]
271 back_chain
[i
] =
"N_OPTS";
272 indices
[opts
[i
]] = j
;
277 for (i =
0; i
< n_opts
; i
++) {
278 # With identical flags, pick only the last one. The
279 # earlier loop ensured that it has all flags merged,
280 # and a nonempty help text if one of the texts was nonempty.
281 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
285 len =
length (opts
[i
]);
286 enum = opt_enum
(opts
[i
])
288 # If this switch takes joined arguments, back-chain all
289 # subsequent switches to it for which it is a prefix. If
290 # a later switch S is a longer prefix of a switch T, T
291 # will be back-chained to S in a later iteration of this
292 # for() loop, which is what we want.
293 if (flag_set_p
("Joined.*", flags
[i
])) {
294 for (j = i
+ 1; j
< n_opts
; j
++) {
295 if (substr (opts
[j
], 1, len
) != opts
[i
])
297 back_chain
[j
] = enum
;
301 s =
substr(" ", length (opts
[i
]))
308 hlp = quote help
[i
] quote
;
310 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
311 if (missing_arg_error ==
"")
312 missing_arg_error =
"0"
314 missing_arg_error = quote missing_arg_error quote
317 warn_message = opt_args
("Warn", flags
[i
])
318 if (warn_message ==
"")
321 warn_message = quote warn_message quote
323 alias_arg = opt_args
("Alias", flags
[i
])
324 if (alias_arg ==
"") {
325 if (flag_set_p
("Ignore", flags
[i
]))
326 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
328 alias_data =
"NULL, NULL, N_OPTS"
329 if (flag_set_p
("Enum.*", flags
[i
])) {
330 if (!flag_set_p
("RejectNegative", flags
[i
]) \
331 && opts
[i
] ~
"^[Wfm]")
332 print "#error Enum allowing negative form"
335 alias_opt = nth_arg
(0, alias_arg
)
336 alias_posarg = nth_arg
(1, alias_arg
)
337 alias_negarg = nth_arg
(2, alias_arg
)
339 if (var_ref
(opts
[i
], flags
[i
]) != "(unsigned short) -1")
340 print "#error Alias setting variable"
342 if (alias_posarg
!= "" && alias_negarg ==
"") {
343 if (!flag_set_p
("RejectNegative", flags
[i
]) \
344 && opts
[i
] ~
"^[Wfm]")
345 print "#error Alias with single argument " \
346 "allowing negative form"
348 if (alias_posarg
!= "" \
349 && flag_set_p
("NegativeAlias", flags
[i
])) {
350 print "#error Alias with multiple arguments " \
351 "used with NegativeAlias"
354 alias_opt = opt_enum
(alias_opt
)
355 if (alias_posarg ==
"")
356 alias_posarg =
"NULL"
358 alias_posarg = quote alias_posarg quote
359 if (alias_negarg ==
"")
360 alias_negarg =
"NULL"
362 alias_negarg = quote alias_negarg quote
363 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
366 neg = opt_args
("Negative", flags
[i
]);
370 if (flag_set_p
("RejectNegative", flags
[i
]))
373 if (opts
[i
] ~
"^[Wfm]")
374 idx = indices
[opts
[i
]];
379 # Split the printf after %u to work around an ia64-hp-hpux11.23
381 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
382 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
383 alias_data
, back_chain
[i
], len
)
384 printf(" %d,\n", idx
)
385 condition = opt_args
("Condition", flags
[i
])
386 cl_flags = switch_flags
(flags
[i
])
387 cl_bit_fields = switch_bit_fields
(flags
[i
])
388 cl_zero_bit_fields = switch_bit_fields
("")
395 " 1 /* Disabled. */, %s,\n" \
397 condition
, cl_flags
, cl_bit_fields
, cl_zero_bit_fields
)
401 cl_flags
, cl_bit_fields
)
402 printf(" %s, %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
403 var_set
(flags
[i
]), integer_range_info
(opt_args
("IntegerRange", flags
[i
])), comma
)
410 print "common_handle_option_auto (struct gcc_options *opts, "
411 print " struct gcc_options *opts_set, "
412 print " const struct cl_decoded_option *decoded, "
413 print " unsigned int lang_mask, int kind, "
414 print " location_t loc, "
415 print " const struct cl_option_handlers *handlers, "
416 print " diagnostic_context *dc) "
418 print " size_t scode = decoded->opt_index; "
419 print " int value = decoded->value; "
420 print " enum opt_code code = (enum opt_code) scode; "
422 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
424 print " switch (code) "
427 for (i =
0; i
< n_enabledby
; i
++) {
428 enabledby_name = enabledby
[i
];
429 print " case " opt_enum
(enabledby_name
) ":"
430 n_enables =
split(enables
[enabledby_name
], thisenable
, ";");
431 n_enablesif =
split(enablesif
[enabledby_name
], thisenableif
, ";");
432 if (n_enables
!= n_enablesif
) {
433 print "#error n_enables != n_enablesif: Something went wrong!"
435 for (j =
1; j
< n_enables
; j
++) {
436 opt_var_name = var_name
(flags
[opt_numbers
[thisenable
[j
]]]);
437 if (opt_var_name
!= "") {
438 condition =
"!opts_set->x_" opt_var_name
439 if (thisenableif
[j
] != "") {
440 value =
"(" thisenableif
[j
] ")"
444 print " if (" condition
")"
445 print " handle_generated_option (opts, opts_set,"
446 print " " opt_enum
(thisenable
[j
]) ", NULL, " value
","
447 print " lang_mask, kind, loc, handlers, true, dc);"
449 print "#error " thisenable
[j
] " does not have a Var() flag"
457 print " return true; "
460 # Handle LangEnabledBy
461 for (i =
0; i
< n_langs
; i
++) {
462 lang_name = lang_sanitized_name
(langs
[i
]);
463 mark_unused =
" ATTRIBUTE_UNUSED";
467 print lang_name
"_handle_option_auto (struct gcc_options *opts" mark_unused
", "
468 print " struct gcc_options *opts_set" mark_unused
", "
469 print " size_t scode" mark_unused
", const char *arg" mark_unused
", int value" mark_unused
", "
470 print " unsigned int lang_mask" mark_unused
", int kind" mark_unused
", "
471 print " location_t loc" mark_unused
", "
472 print " const struct cl_option_handlers *handlers" mark_unused
", "
473 print " diagnostic_context *dc" mark_unused
") "
475 print " enum opt_code code = (enum opt_code) scode; "
477 print " switch (code) "
480 for (k =
0; k
< n_enabledby_lang
[i
]; k
++) {
481 enabledby_name = enabledby
[lang_name
,k
];
482 print " case " opt_enum
(enabledby_name
) ":"
483 n_thisenable =
split(enables
[lang_name
,enabledby_name
], thisenable
, ";");
484 for (j =
1; j
< n_thisenable
; j
++) {
485 n_thisenable_args =
split(thisenable
[j
], thisenable_args
, ",");
486 if (n_thisenable_args ==
1) {
487 thisenable_opt = thisenable
[j
];
490 thisenable_opt = thisenable_args
[1];
491 with_posarg = thisenable_args
[2];
492 with_negarg = thisenable_args
[3];
493 value =
"value ? " with_posarg
" : " with_negarg
;
495 opt_var_name = var_name
(flags
[opt_numbers
[thisenable_opt
]]);
496 if (opt_var_name
!= "") {
497 print " if (!opts_set->x_" opt_var_name
")"
498 print " handle_generated_option (opts, opts_set,"
499 print " " opt_enum
(thisenable_opt
) ", NULL, " value
","
500 print " lang_mask, kind, loc, handlers, true, dc);"
502 print "#error " thisenable_opt
" does not have a Var() flag"
510 print " return true; "
516 print "#include " quote
"cpplib.h" quote
;
518 print "cpp_handle_option_auto (const struct gcc_options * opts, "
519 print " size_t scode, struct cpp_options * cpp_opts)"
521 print " enum opt_code code = (enum opt_code) scode; "
523 print " switch (code) "
525 for (i =
0; i
< n_opts
; i
++) {
526 # With identical flags, pick only the last one. The
527 # earlier loop ensured that it has all flags merged,
528 # and a nonempty help text if one of the texts was nonempty.
529 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
533 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
534 if (cpp_option
!= "") {
535 opt_var_name = var_name
(flags
[i
]);
536 init = opt_args
("Init", flags
[i
])
537 if (opt_var_name
!= "" && init
!= "") {
538 print " case " opt_enum
(opts
[i
]) ":"
539 print " cpp_opts->" cpp_option
" = opts->x_" opt_var_name
";"
541 } else if (opt_var_name ==
"" && init ==
"") {
542 print "#error CPP() requires setting Init() and Var() for " opts
[i
]
543 } else if (opt_var_name
!= "") {
544 print "#error CPP() requires setting Init() for " opts
[i
]
546 print "#error CPP() requires setting Var() for " opts
[i
]
555 print "init_global_opts_from_cpp(struct gcc_options * opts, "
556 print " const struct cpp_options * cpp_opts)"
558 for (i =
0; i
< n_opts
; i
++) {
559 # With identical flags, pick only the last one. The
560 # earlier loop ensured that it has all flags merged,
561 # and a nonempty help text if one of the texts was nonempty.
562 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
565 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
566 opt_var_name = var_name
(flags
[i
]);
567 if (cpp_option
!= "" && opt_var_name
!= "") {
568 print " opts->x_" opt_var_name
" = cpp_opts->" cpp_option
";"