1 # Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011, 2012
2 # Free Software Foundation, Inc.
3 # Contributed by Kelley Cook, June 2004.
4 # Original code from Neil Booth, May 2003.
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 3, or (at your option) any
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; see the file COPYING3. If not see
18 # <http://www.gnu.org/licenses/>.
20 # This Awk script reads in the option records generated from
21 # opt-gather.awk, combines the flags of duplicate options and generates a
25 # This program uses functions from opt-functions.awk and code from
28 # Usage: awk -f opt-functions.awk -f opt-read.awk -f optc-gen.awk \
29 # [-v header_name=header.h] < inputfile > options.c
31 # Dump that array of options into a C file.
34 # Record first EnabledBy and LangEnabledBy uses.
36 for (i =
0; i
< n_langs
; i
++) {
37 n_enabledby_lang
[i
] =
0;
39 for (i =
0; i
< n_opts
; i
++) {
40 enabledby_arg = opt_args
("EnabledBy", flags
[i
]);
41 if (enabledby_arg
!= "") {
42 n_enabledby_names =
split(enabledby_arg
, enabledby_names
, " && ");
43 if (n_enabledby_names
> 2) {
44 print "#error EnabledBy (Wfoo && Wbar && Wbaz) not currently supported"
46 for (j =
1; j
<= n_enabledby_names
; j
++) {
47 enabledby_name = enabledby_names
[j
];
48 enabledby_index = opt_numbers
[enabledby_name
];
49 if (enabledby_index ==
"") {
50 print "#error Enabledby: " enabledby_name
53 if (n_enabledby_names ==
2) {
54 opt_var_name_1 = search_var_name
(enabledby_names
[1], opt_numbers
, opts
, flags
, n_opts
);
55 opt_var_name_2 = search_var_name
(enabledby_names
[2], opt_numbers
, opts
, flags
, n_opts
);
56 if (opt_var_name_1 ==
"") {
57 print "#error " enabledby_names
[1] " does not have a Var() flag"
59 if (opt_var_name_2 ==
"") {
60 print "#error " enabledby_names
[2] " does not have a Var() flag"
62 condition =
"opts->x_" opt_var_name_1
" && opts->x_" opt_var_name_2
;
64 if (enables
[enabledby_name
] ==
"") {
65 enabledby
[n_enabledby
] = enabledby_name
;
68 enables
[enabledby_name
] = enables
[enabledby_name
] opts
[i
] ";";
69 enablesif
[enabledby_name
] = enablesif
[enabledby_name
] condition
";";
74 enabledby_arg = opt_args
("LangEnabledBy", flags
[i
]);
75 if (enabledby_arg
!= "") {
76 n_enabledby_arg_langs =
split(nth_arg
(0, enabledby_arg
), enabledby_arg_langs
, " ");
77 enabledby_name = nth_arg
(1, enabledby_arg
);
78 enabledby_posarg = nth_arg
(2, enabledby_arg
)
79 enabledby_negarg = nth_arg
(3, enabledby_arg
)
80 enabledby_index = opt_numbers
[enabledby_name
];
81 if (enabledby_index ==
"") {
82 print "#error LangEnabledby: " enabledby_name
84 if (enabledby_posarg
!= "" && enabledby_negarg
!= "") {
85 with_args =
"," enabledby_posarg
"," enabledby_negarg
86 } else if (enabledby_posarg ==
"" && enabledby_negarg ==
"") {
89 print "#error LangEnabledBy with three arguments, it should have either 2 or 4"
92 for (j =
1; j
<= n_enabledby_arg_langs
; j
++) {
93 lang_name = lang_sanitized_name
(enabledby_arg_langs
[j
]);
94 lang_index = lang_numbers
[enabledby_arg_langs
[j
]];
95 if (enables
[lang_name
,enabledby_name
] ==
"") {
96 enabledby
[lang_name
,n_enabledby_lang
[lang_index
]] = enabledby_name
;
97 n_enabledby_lang
[lang_index
]++;
99 enables
[lang_name
,enabledby_name
] = enables
[lang_name
,enabledby_name
] opts
[i
] with_args
";";
106 print "/* This file is auto-generated by optc-gen.awk. */"
108 n_headers =
split(header_name
, headers
, " ")
109 for (i =
1; i
<= n_headers
; i
++)
110 print "#include " quote headers
[i
] quote
111 print "#include " quote
"opts.h" quote
112 print "#include " quote
"intl.h" quote
113 print "#include " quote
"insn-attr-common.h" quote
116 if (n_extra_c_includes
> 0) {
117 for (i =
0; i
< n_extra_c_includes
; i
++) {
118 print "#include " quote extra_c_includes
[i
] quote
123 for (i =
0; i
< n_enums
; i
++) {
125 type = enum_type
[name
]
126 print "static const struct cl_enum_arg cl_enum_" name \
129 print enum_data
[name
] " { NULL, 0, 0 }"
133 print "cl_enum_" name
"_set (void *var, int value)"
135 print " *((" type
" *) var) = (" type
") value;"
139 print "cl_enum_" name
"_get (const void *var)"
141 print " return (int) *((const " type
" *) var);"
146 print "const struct cl_enum cl_enums[] ="
148 for (i =
0; i
< n_enums
; i
++) {
150 ehelp = enum_help
[name
]
154 ehelp = quote ehelp quote
155 unknown_error = enum_unknown_error
[name
]
156 if (unknown_error ==
"")
157 unknown_error =
"NULL"
159 unknown_error = quote unknown_error quote
162 print " " unknown_error
","
163 print " cl_enum_" name
"_data,"
164 print " sizeof (" enum_type
[name
] "),"
165 print " cl_enum_" name
"_set,"
166 print " cl_enum_" name
"_get"
170 print "const unsigned int cl_enums_count = " n_enums
";"
173 print "const struct gcc_options global_options_init =\n{"
174 for (i =
0; i
< n_extra_vars
; i
++) {
178 sub(".*= *", "", init
)
182 sub(" *=.*", "", var
)
184 sub("^.*[ *]", "", name
)
185 sub("\\[.*\\]$", "", name
)
187 print " " init
", /* " name
" */"
189 for (i =
0; i
< n_opts
; i
++) {
190 name = var_name
(flags
[i
]);
194 init = opt_args
("Init", flags
[i
])
196 if (name in var_init
&& var_init
[name
] != init
)
197 print "#error multiple initializers for " name
198 var_init
[name
] = init
201 for (i =
0; i
< n_opts
; i
++) {
202 name = var_name
(flags
[i
]);
206 if (name in var_seen
)
209 if (name in var_init
)
210 init = var_init
[name
]
214 print " " init
", /* " name
" */"
218 for (i =
0; i
< n_opts
; i
++) {
219 name = static_var
(opts
[i
], flags
[i
]);
221 print " 0, /* " name
" (private state) */"
222 print "#undef x_" name
225 for (i =
0; i
< n_opts
; i
++) {
226 if (flag_set_p
("SetByCombined", flags
[i
]))
227 print " false, /* frontend_set_" var_name
(flags
[i
]) " */"
231 print "struct gcc_options global_options;"
232 print "struct gcc_options global_options_set;"
235 print "const char * const lang_names[] =\n{"
236 for (i =
0; i
< n_langs
; i
++) {
237 macros
[i
] =
"CL_" lang_sanitized_name
(langs
[i
])
238 s =
substr(" ", length (macros
[i
]))
239 print " " quote langs
[i
] quote
","
243 print "const unsigned int cl_options_count = N_OPTS;\n"
244 print "#if (1U << " n_langs
") > CL_MIN_OPTION_CLASS"
245 print " #error the number of languages exceeds the implementation limit"
247 print "const unsigned int cl_lang_count = " n_langs
";\n"
249 print "const struct cl_option cl_options[] =\n{"
252 for (i =
0; i
< n_opts
; i
++) {
253 back_chain
[i
] =
"N_OPTS";
254 indices
[opts
[i
]] = j
;
255 # Combine the flags of identical switches. Switches
256 # appear many times if they are handled by many front
258 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
259 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
260 if (help
[i
+ 1] ==
"")
261 help
[i
+ 1] = help
[i
]
262 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
])
263 print "#error Multiple different help strings for " \
264 opts
[i
] ":\n\t" help
[i
] "\n\t" help
[i
+ 1]
267 back_chain
[i
] =
"N_OPTS";
268 indices
[opts
[i
]] = j
;
273 for (i =
0; i
< n_opts
; i
++) {
274 # With identical flags, pick only the last one. The
275 # earlier loop ensured that it has all flags merged,
276 # and a nonempty help text if one of the texts was nonempty.
277 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
281 len =
length (opts
[i
]);
282 enum = opt_enum
(opts
[i
])
284 # If this switch takes joined arguments, back-chain all
285 # subsequent switches to it for which it is a prefix. If
286 # a later switch S is a longer prefix of a switch T, T
287 # will be back-chained to S in a later iteration of this
288 # for() loop, which is what we want.
289 if (flag_set_p
("Joined.*", flags
[i
])) {
290 for (j = i
+ 1; j
< n_opts
; j
++) {
291 if (substr (opts
[j
], 1, len
) != opts
[i
])
293 back_chain
[j
] = enum
;
297 s =
substr(" ", length (opts
[i
]))
304 hlp = quote help
[i
] quote
;
306 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
307 if (missing_arg_error ==
"")
308 missing_arg_error =
"0"
310 missing_arg_error = quote missing_arg_error quote
313 warn_message = opt_args
("Warn", flags
[i
])
314 if (warn_message ==
"")
317 warn_message = quote warn_message quote
319 alias_arg = opt_args
("Alias", flags
[i
])
320 if (alias_arg ==
"") {
321 if (flag_set_p
("Ignore", flags
[i
]))
322 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
324 alias_data =
"NULL, NULL, N_OPTS"
326 alias_opt = nth_arg
(0, alias_arg
)
327 alias_posarg = nth_arg
(1, alias_arg
)
328 alias_negarg = nth_arg
(2, alias_arg
)
330 if (var_ref
(opts
[i
], flags
[i
]) != "-1")
331 print "#error Alias setting variable"
333 if (alias_posarg
!= "" && alias_negarg ==
"") {
334 if (!flag_set_p
("RejectNegative", flags
[i
]) \
335 && opts
[i
] ~
"^[Wfm]")
336 print "#error Alias with single argument " \
337 "allowing negative form"
339 if (alias_posarg
!= "" \
340 && flag_set_p
("NegativeAlias", flags
[i
])) {
341 print "#error Alias with multiple arguments " \
342 "used with NegativeAlias"
345 alias_opt = opt_enum
(alias_opt
)
346 if (alias_posarg ==
"")
347 alias_posarg =
"NULL"
349 alias_posarg = quote alias_posarg quote
350 if (alias_negarg ==
"")
351 alias_negarg =
"NULL"
353 alias_negarg = quote alias_negarg quote
354 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
357 neg = opt_args
("Negative", flags
[i
]);
361 if (flag_set_p
("RejectNegative", flags
[i
]))
364 if (opts
[i
] ~
"^[Wfm]")
365 idx = indices
[opts
[i
]];
370 # Split the printf after %u to work around an ia64-hp-hpux11.23
372 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
373 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
374 alias_data
, back_chain
[i
], len
)
375 printf(" %d,\n", idx
)
376 condition = opt_args
("Condition", flags
[i
])
377 cl_flags = switch_flags
(flags
[i
])
378 cl_bit_fields = switch_bit_fields
(flags
[i
])
379 cl_zero_bit_fields = switch_bit_fields
("")
386 " 1 /* Disabled. */, %s,\n" \
388 condition
, cl_flags
, cl_bit_fields
, cl_zero_bit_fields
)
392 cl_flags
, cl_bit_fields
)
393 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
394 var_set
(flags
[i
]), comma
)
401 print "common_handle_option_auto (struct gcc_options *opts, "
402 print " struct gcc_options *opts_set, "
403 print " const struct cl_decoded_option *decoded, "
404 print " unsigned int lang_mask, int kind, "
405 print " location_t loc, "
406 print " const struct cl_option_handlers *handlers, "
407 print " diagnostic_context *dc) "
409 print " size_t scode = decoded->opt_index; "
410 print " int value = decoded->value; "
411 print " enum opt_code code = (enum opt_code) scode; "
413 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
415 print " switch (code) "
418 for (i =
0; i
< n_enabledby
; i
++) {
419 enabledby_name = enabledby
[i
];
420 print " case " opt_enum
(enabledby_name
) ":"
421 n_enables =
split(enables
[enabledby_name
], thisenable
, ";");
422 n_enablesif =
split(enablesif
[enabledby_name
], thisenableif
, ";");
423 if (n_enables
!= n_enablesif
) {
424 print "#error n_enables != n_enablesif: Something went wrong!"
426 for (j =
1; j
< n_enables
; j
++) {
427 opt_var_name = var_name
(flags
[opt_numbers
[thisenable
[j
]]]);
428 if (opt_var_name
!= "") {
429 condition =
"!opts_set->x_" opt_var_name
430 if (thisenableif
[j
] != "") {
431 condition = condition
" && (" thisenableif
[j
] ")"
433 print " if (" condition
")"
434 print " handle_generated_option (opts, opts_set,"
435 print " " opt_enum
(thisenable
[j
]) ", NULL, value,"
436 print " lang_mask, kind, loc, handlers, dc);"
438 print "#error " thisenable
[j
] " does not have a Var() flag"
446 print " return true; "
449 # Handle LangEnabledBy
450 for (i =
0; i
< n_langs
; i
++) {
451 lang_name = lang_sanitized_name
(langs
[i
]);
452 mark_unused =
" ATTRIBUTE_UNUSED";
456 print lang_name
"_handle_option_auto (struct gcc_options *opts" mark_unused
", "
457 print " struct gcc_options *opts_set" mark_unused
", "
458 print " size_t scode" mark_unused
", const char *arg" mark_unused
", int value" mark_unused
", "
459 print " unsigned int lang_mask" mark_unused
", int kind" mark_unused
", "
460 print " location_t loc" mark_unused
", "
461 print " const struct cl_option_handlers *handlers" mark_unused
", "
462 print " diagnostic_context *dc" mark_unused
") "
464 print " enum opt_code code = (enum opt_code) scode; "
466 print " switch (code) "
469 for (k =
0; k
< n_enabledby_lang
[i
]; k
++) {
470 enabledby_name = enabledby
[lang_name
,k
];
471 print " case " opt_enum
(enabledby_name
) ":"
472 n_thisenable =
split(enables
[lang_name
,enabledby_name
], thisenable
, ";");
473 for (j =
1; j
< n_thisenable
; j
++) {
474 n_thisenable_args =
split(thisenable
[j
], thisenable_args
, ",");
475 if (n_thisenable_args ==
1) {
476 thisenable_opt = thisenable
[j
];
479 thisenable_opt = thisenable_args
[1];
480 with_posarg = thisenable_args
[2];
481 with_negarg = thisenable_args
[3];
482 value =
"value ? " with_posarg
" : " with_negarg
;
484 opt_var_name = var_name
(flags
[opt_numbers
[thisenable_opt
]]);
485 if (opt_var_name
!= "") {
486 print " if (!opts_set->x_" opt_var_name
")"
487 print " handle_generated_option (opts, opts_set,"
488 print " " opt_enum
(thisenable_opt
) ", NULL, " value
","
489 print " lang_mask, kind, loc, handlers, dc);"
491 print "#error " thisenable_opt
" does not have a Var() flag"
499 print " return true; "