1 # Copyright (C) 2003-2014 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.
33 # Record first EnabledBy and LangEnabledBy uses.
35 for (i =
0; i
< n_langs
; i
++) {
36 n_enabledby_lang
[i
] =
0;
38 for (i =
0; i
< n_opts
; i
++) {
39 enabledby_arg = opt_args
("EnabledBy", flags
[i
]);
40 if (enabledby_arg
!= "") {
41 logical_and =
index(enabledby_arg
, " && ");
42 if (logical_and
!= 0) {
43 # EnabledBy(arg1 && arg2)
46 # EnabledBy(arg) or EnabledBy(arg1 || arg2 || arg3)
47 split_sep =
" \\|\\| ";
49 n_enabledby_names =
split(enabledby_arg
, enabledby_names
, split_sep
);
50 if (logical_and
!= 0 && n_enabledby_names
> 2) {
51 print "#error EnabledBy (Wfoo && Wbar && Wbaz) not currently supported"
53 for (j =
1; j
<= n_enabledby_names
; j
++) {
54 enabledby_name = enabledby_names
[j
];
55 enabledby_index = opt_numbers
[enabledby_name
];
56 if (enabledby_index ==
"") {
57 print "#error Enabledby: " enabledby_name
60 if (logical_and
!= 0) {
61 opt_var_name_1 = search_var_name
(enabledby_names
[1], opt_numbers
, opts
, flags
, n_opts
);
62 opt_var_name_2 = search_var_name
(enabledby_names
[2], opt_numbers
, opts
, flags
, n_opts
);
63 if (opt_var_name_1 ==
"") {
64 print "#error " enabledby_names
[1] " does not have a Var() flag"
66 if (opt_var_name_2 ==
"") {
67 print "#error " enabledby_names
[2] " does not have a Var() flag"
69 condition =
"opts->x_" opt_var_name_1
" && opts->x_" opt_var_name_2
;
71 if (enables
[enabledby_name
] ==
"") {
72 enabledby
[n_enabledby
] = enabledby_name
;
75 enables
[enabledby_name
] = enables
[enabledby_name
] opts
[i
] ";";
76 enablesif
[enabledby_name
] = enablesif
[enabledby_name
] condition
";";
81 enabledby_arg = opt_args
("LangEnabledBy", flags
[i
]);
82 if (enabledby_arg
!= "") {
83 enabledby_langs = nth_arg
(0, enabledby_arg
);
84 enabledby_name = nth_arg
(1, enabledby_arg
);
85 enabledby_posarg = nth_arg
(2, enabledby_arg
);
86 enabledby_negarg = nth_arg
(3, enabledby_arg
);
87 lang_enabled_by
(enabledby_langs
, enabledby_name
, enabledby_posarg
, enabledby_negarg
);
92 print "/* This file is auto-generated by optc-gen.awk. */"
94 n_headers =
split(header_name
, headers
, " ")
95 for (i =
1; i
<= n_headers
; i
++)
96 print "#include " quote headers
[i
] quote
97 print "#include " quote
"opts.h" quote
98 print "#include " quote
"intl.h" quote
99 print "#include " quote
"insn-attr-common.h" quote
102 if (n_extra_c_includes
> 0) {
103 for (i =
0; i
< n_extra_c_includes
; i
++) {
104 print "#include " quote extra_c_includes
[i
] quote
109 for (i =
0; i
< n_enums
; i
++) {
111 type = enum_type
[name
]
112 print "static const struct cl_enum_arg cl_enum_" name \
115 print enum_data
[name
] " { NULL, 0, 0 }"
119 print "cl_enum_" name
"_set (void *var, int value)"
121 print " *((" type
" *) var) = (" type
") value;"
125 print "cl_enum_" name
"_get (const void *var)"
127 print " return (int) *((const " type
" *) var);"
132 print "const struct cl_enum cl_enums[] ="
134 for (i =
0; i
< n_enums
; i
++) {
136 ehelp = enum_help
[name
]
140 ehelp = quote ehelp quote
141 unknown_error = enum_unknown_error
[name
]
142 if (unknown_error ==
"")
143 unknown_error =
"NULL"
145 unknown_error = quote unknown_error quote
148 print " " unknown_error
","
149 print " cl_enum_" name
"_data,"
150 print " sizeof (" enum_type
[name
] "),"
151 print " cl_enum_" name
"_set,"
152 print " cl_enum_" name
"_get"
156 print "const unsigned int cl_enums_count = " n_enums
";"
159 print "const struct gcc_options global_options_init =\n{"
160 for (i =
0; i
< n_extra_vars
; i
++) {
164 sub(".*= *", "", init
)
168 sub(" *=.*", "", var
)
170 sub("^.*[ *]", "", name
)
171 sub("\\[.*\\]$", "", name
)
173 print " " init
", /* " name
" */"
175 for (i =
0; i
< n_opts
; i
++) {
176 name = var_name
(flags
[i
]);
180 init = opt_args
("Init", flags
[i
])
182 if (name in var_init
&& var_init
[name
] != init
)
183 print "#error multiple initializers for " name
184 var_init
[name
] = init
187 for (i =
0; i
< n_opts
; i
++) {
188 name = var_name
(flags
[i
]);
192 if (name in var_seen
)
195 if (name in var_init
)
196 init = var_init
[name
]
200 print " " init
", /* " name
" */"
204 for (i =
0; i
< n_opts
; i
++) {
205 name = static_var
(opts
[i
], flags
[i
]);
207 print " 0, /* " name
" (private state) */"
208 print "#undef x_" name
211 for (i =
0; i
< n_opts
; i
++) {
212 if (flag_set_p
("SetByCombined", flags
[i
]))
213 print " false, /* frontend_set_" var_name
(flags
[i
]) " */"
217 print "struct gcc_options global_options;"
218 print "struct gcc_options global_options_set;"
221 print "const char * const lang_names[] =\n{"
222 for (i =
0; i
< n_langs
; i
++) {
223 macros
[i
] =
"CL_" lang_sanitized_name
(langs
[i
])
224 s =
substr(" ", length (macros
[i
]))
225 print " " quote langs
[i
] quote
","
229 print "const unsigned int cl_options_count = N_OPTS;\n"
230 print "#if (1U << " n_langs
") > CL_MIN_OPTION_CLASS"
231 print " #error the number of languages exceeds the implementation limit"
233 print "const unsigned int cl_lang_count = " n_langs
";\n"
235 print "const struct cl_option cl_options[] =\n{"
238 for (i =
0; i
< n_opts
; i
++) {
239 back_chain
[i
] =
"N_OPTS";
240 indices
[opts
[i
]] = j
;
241 # Combine the flags of identical switches. Switches
242 # appear many times if they are handled by many front
244 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
245 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
246 if (help
[i
+ 1] ==
"")
247 help
[i
+ 1] = help
[i
]
248 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
])
249 print "#error Multiple different help strings for " \
250 opts
[i
] ":\n\t" help
[i
] "\n\t" help
[i
+ 1]
253 back_chain
[i
] =
"N_OPTS";
254 indices
[opts
[i
]] = j
;
259 for (i =
0; i
< n_opts
; i
++) {
260 # With identical flags, pick only the last one. The
261 # earlier loop ensured that it has all flags merged,
262 # and a nonempty help text if one of the texts was nonempty.
263 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
267 len =
length (opts
[i
]);
268 enum = opt_enum
(opts
[i
])
270 # If this switch takes joined arguments, back-chain all
271 # subsequent switches to it for which it is a prefix. If
272 # a later switch S is a longer prefix of a switch T, T
273 # will be back-chained to S in a later iteration of this
274 # for() loop, which is what we want.
275 if (flag_set_p
("Joined.*", flags
[i
])) {
276 for (j = i
+ 1; j
< n_opts
; j
++) {
277 if (substr (opts
[j
], 1, len
) != opts
[i
])
279 back_chain
[j
] = enum
;
283 s =
substr(" ", length (opts
[i
]))
290 hlp = quote help
[i
] quote
;
292 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
293 if (missing_arg_error ==
"")
294 missing_arg_error =
"0"
296 missing_arg_error = quote missing_arg_error quote
299 warn_message = opt_args
("Warn", flags
[i
])
300 if (warn_message ==
"")
303 warn_message = quote warn_message quote
305 alias_arg = opt_args
("Alias", flags
[i
])
306 if (alias_arg ==
"") {
307 if (flag_set_p
("Ignore", flags
[i
]))
308 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
310 alias_data =
"NULL, NULL, N_OPTS"
312 alias_opt = nth_arg
(0, alias_arg
)
313 alias_posarg = nth_arg
(1, alias_arg
)
314 alias_negarg = nth_arg
(2, alias_arg
)
316 if (var_ref
(opts
[i
], flags
[i
]) != "-1")
317 print "#error Alias setting variable"
319 if (alias_posarg
!= "" && alias_negarg ==
"") {
320 if (!flag_set_p
("RejectNegative", flags
[i
]) \
321 && opts
[i
] ~
"^[Wfm]")
322 print "#error Alias with single argument " \
323 "allowing negative form"
325 if (alias_posarg
!= "" \
326 && flag_set_p
("NegativeAlias", flags
[i
])) {
327 print "#error Alias with multiple arguments " \
328 "used with NegativeAlias"
331 alias_opt = opt_enum
(alias_opt
)
332 if (alias_posarg ==
"")
333 alias_posarg =
"NULL"
335 alias_posarg = quote alias_posarg quote
336 if (alias_negarg ==
"")
337 alias_negarg =
"NULL"
339 alias_negarg = quote alias_negarg quote
340 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
343 neg = opt_args
("Negative", flags
[i
]);
347 if (flag_set_p
("RejectNegative", flags
[i
]))
350 if (opts
[i
] ~
"^[Wfm]")
351 idx = indices
[opts
[i
]];
356 # Split the printf after %u to work around an ia64-hp-hpux11.23
358 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
359 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
360 alias_data
, back_chain
[i
], len
)
361 printf(" %d,\n", idx
)
362 condition = opt_args
("Condition", flags
[i
])
363 cl_flags = switch_flags
(flags
[i
])
364 cl_bit_fields = switch_bit_fields
(flags
[i
])
365 cl_zero_bit_fields = switch_bit_fields
("")
372 " 1 /* Disabled. */, %s,\n" \
374 condition
, cl_flags
, cl_bit_fields
, cl_zero_bit_fields
)
378 cl_flags
, cl_bit_fields
)
379 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
380 var_set
(flags
[i
]), comma
)
387 print "common_handle_option_auto (struct gcc_options *opts, "
388 print " struct gcc_options *opts_set, "
389 print " const struct cl_decoded_option *decoded, "
390 print " unsigned int lang_mask, int kind, "
391 print " location_t loc, "
392 print " const struct cl_option_handlers *handlers, "
393 print " diagnostic_context *dc) "
395 print " size_t scode = decoded->opt_index; "
396 print " int value = decoded->value; "
397 print " enum opt_code code = (enum opt_code) scode; "
399 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
401 print " switch (code) "
404 for (i =
0; i
< n_enabledby
; i
++) {
405 enabledby_name = enabledby
[i
];
406 print " case " opt_enum
(enabledby_name
) ":"
407 n_enables =
split(enables
[enabledby_name
], thisenable
, ";");
408 n_enablesif =
split(enablesif
[enabledby_name
], thisenableif
, ";");
409 if (n_enables
!= n_enablesif
) {
410 print "#error n_enables != n_enablesif: Something went wrong!"
412 for (j =
1; j
< n_enables
; j
++) {
413 opt_var_name = var_name
(flags
[opt_numbers
[thisenable
[j
]]]);
414 if (opt_var_name
!= "") {
415 condition =
"!opts_set->x_" opt_var_name
416 if (thisenableif
[j
] != "") {
417 value =
"(" thisenableif
[j
] ")"
421 print " if (" condition
")"
422 print " handle_generated_option (opts, opts_set,"
423 print " " opt_enum
(thisenable
[j
]) ", NULL, " value
","
424 print " lang_mask, kind, loc, handlers, dc);"
426 print "#error " thisenable
[j
] " does not have a Var() flag"
434 print " return true; "
437 # Handle LangEnabledBy
438 for (i =
0; i
< n_langs
; i
++) {
439 lang_name = lang_sanitized_name
(langs
[i
]);
440 mark_unused =
" ATTRIBUTE_UNUSED";
444 print lang_name
"_handle_option_auto (struct gcc_options *opts" mark_unused
", "
445 print " struct gcc_options *opts_set" mark_unused
", "
446 print " size_t scode" mark_unused
", const char *arg" mark_unused
", int value" mark_unused
", "
447 print " unsigned int lang_mask" mark_unused
", int kind" mark_unused
", "
448 print " location_t loc" mark_unused
", "
449 print " const struct cl_option_handlers *handlers" mark_unused
", "
450 print " diagnostic_context *dc" mark_unused
") "
452 print " enum opt_code code = (enum opt_code) scode; "
454 print " switch (code) "
457 for (k =
0; k
< n_enabledby_lang
[i
]; k
++) {
458 enabledby_name = enabledby
[lang_name
,k
];
459 print " case " opt_enum
(enabledby_name
) ":"
460 n_thisenable =
split(enables
[lang_name
,enabledby_name
], thisenable
, ";");
461 for (j =
1; j
< n_thisenable
; j
++) {
462 n_thisenable_args =
split(thisenable
[j
], thisenable_args
, ",");
463 if (n_thisenable_args ==
1) {
464 thisenable_opt = thisenable
[j
];
467 thisenable_opt = thisenable_args
[1];
468 with_posarg = thisenable_args
[2];
469 with_negarg = thisenable_args
[3];
470 value =
"value ? " with_posarg
" : " with_negarg
;
472 opt_var_name = var_name
(flags
[opt_numbers
[thisenable_opt
]]);
473 if (opt_var_name
!= "") {
474 print " if (!opts_set->x_" opt_var_name
")"
475 print " handle_generated_option (opts, opts_set,"
476 print " " opt_enum
(thisenable_opt
) ", NULL, " value
","
477 print " lang_mask, kind, loc, handlers, dc);"
479 print "#error " thisenable_opt
" does not have a Var() flag"
487 print " return true; "
493 print "#include " quote
"cpplib.h" quote
;
495 print "cpp_handle_option_auto (const struct gcc_options * opts, "
496 print " size_t scode, struct cpp_options * cpp_opts)"
498 print " enum opt_code code = (enum opt_code) scode; "
500 print " switch (code) "
502 for (i =
0; i
< n_opts
; i
++) {
503 # With identical flags, pick only the last one. The
504 # earlier loop ensured that it has all flags merged,
505 # and a nonempty help text if one of the texts was nonempty.
506 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
510 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
511 if (cpp_option
!= "") {
512 opt_var_name = var_name
(flags
[i
]);
513 init = opt_args
("Init", flags
[i
])
514 if (opt_var_name
!= "" && init
!= "") {
515 print " case " opt_enum
(opts
[i
]) ":"
516 print " cpp_opts->" cpp_option
" = opts->x_" opt_var_name
";"
518 } else if (opt_var_name ==
"" && init ==
"") {
519 print "#error CPP() requires setting Init() and Var() for " opts
[i
]
520 } else if (opt_var_name
!= "") {
521 print "#error CPP() requires setting Init() for " opts
[i
]
523 print "#error CPP() requires setting Var() for " opts
[i
]
532 print "init_global_opts_from_cpp(struct gcc_options * opts, "
533 print " const struct cpp_options * cpp_opts)"
535 for (i =
0; i
< n_opts
; i
++) {
536 # With identical flags, pick only the last one. The
537 # earlier loop ensured that it has all flags merged,
538 # and a nonempty help text if one of the texts was nonempty.
539 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
542 cpp_option = nth_arg
(0, opt_args
("CPP", flags
[i
]));
543 opt_var_name = var_name
(flags
[i
]);
544 if (cpp_option
!= "" && opt_var_name
!= "") {
545 print " opts->x_" opt_var_name
" = cpp_opts->" cpp_option
";"