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 n_enabledby_names =
split(enabledby_arg
, enabledby_names
, " && ");
42 if (n_enabledby_names
> 2) {
43 print "#error EnabledBy (Wfoo && Wbar && Wbaz) not currently supported"
45 for (j =
1; j
<= n_enabledby_names
; j
++) {
46 enabledby_name = enabledby_names
[j
];
47 enabledby_index = opt_numbers
[enabledby_name
];
48 if (enabledby_index ==
"") {
49 print "#error Enabledby: " enabledby_name
52 if (n_enabledby_names ==
2) {
53 opt_var_name_1 = search_var_name
(enabledby_names
[1], opt_numbers
, opts
, flags
, n_opts
);
54 opt_var_name_2 = search_var_name
(enabledby_names
[2], opt_numbers
, opts
, flags
, n_opts
);
55 if (opt_var_name_1 ==
"") {
56 print "#error " enabledby_names
[1] " does not have a Var() flag"
58 if (opt_var_name_2 ==
"") {
59 print "#error " enabledby_names
[2] " does not have a Var() flag"
61 condition =
"opts->x_" opt_var_name_1
" && opts->x_" opt_var_name_2
;
63 if (enables
[enabledby_name
] ==
"") {
64 enabledby
[n_enabledby
] = enabledby_name
;
67 enables
[enabledby_name
] = enables
[enabledby_name
] opts
[i
] ";";
68 enablesif
[enabledby_name
] = enablesif
[enabledby_name
] condition
";";
73 enabledby_arg = opt_args
("LangEnabledBy", flags
[i
]);
74 if (enabledby_arg
!= "") {
75 enabledby_langs = nth_arg
(0, enabledby_arg
);
76 enabledby_name = nth_arg
(1, enabledby_arg
);
77 enabledby_posarg = nth_arg
(2, enabledby_arg
);
78 enabledby_negarg = nth_arg
(3, enabledby_arg
);
79 lang_enabled_by
(enabledby_langs
, enabledby_name
, enabledby_posarg
, enabledby_negarg
);
84 print "/* This file is auto-generated by optc-gen.awk. */"
86 n_headers =
split(header_name
, headers
, " ")
87 for (i =
1; i
<= n_headers
; i
++)
88 print "#include " quote headers
[i
] quote
89 print "#include " quote
"opts.h" quote
90 print "#include " quote
"intl.h" quote
91 print "#include " quote
"insn-attr-common.h" quote
94 if (n_extra_c_includes
> 0) {
95 for (i =
0; i
< n_extra_c_includes
; i
++) {
96 print "#include " quote extra_c_includes
[i
] quote
101 for (i =
0; i
< n_enums
; i
++) {
103 type = enum_type
[name
]
104 print "static const struct cl_enum_arg cl_enum_" name \
107 print enum_data
[name
] " { NULL, 0, 0 }"
111 print "cl_enum_" name
"_set (void *var, int value)"
113 print " *((" type
" *) var) = (" type
") value;"
117 print "cl_enum_" name
"_get (const void *var)"
119 print " return (int) *((const " type
" *) var);"
124 print "const struct cl_enum cl_enums[] ="
126 for (i =
0; i
< n_enums
; i
++) {
128 ehelp = enum_help
[name
]
132 ehelp = quote ehelp quote
133 unknown_error = enum_unknown_error
[name
]
134 if (unknown_error ==
"")
135 unknown_error =
"NULL"
137 unknown_error = quote unknown_error quote
140 print " " unknown_error
","
141 print " cl_enum_" name
"_data,"
142 print " sizeof (" enum_type
[name
] "),"
143 print " cl_enum_" name
"_set,"
144 print " cl_enum_" name
"_get"
148 print "const unsigned int cl_enums_count = " n_enums
";"
151 print "const struct gcc_options global_options_init =\n{"
152 for (i =
0; i
< n_extra_vars
; i
++) {
156 sub(".*= *", "", init
)
160 sub(" *=.*", "", var
)
162 sub("^.*[ *]", "", name
)
163 sub("\\[.*\\]$", "", name
)
165 print " " init
", /* " name
" */"
167 for (i =
0; i
< n_opts
; i
++) {
168 name = var_name
(flags
[i
]);
172 init = opt_args
("Init", flags
[i
])
174 if (name in var_init
&& var_init
[name
] != init
)
175 print "#error multiple initializers for " name
176 var_init
[name
] = init
179 for (i =
0; i
< n_opts
; i
++) {
180 name = var_name
(flags
[i
]);
184 if (name in var_seen
)
187 if (name in var_init
)
188 init = var_init
[name
]
192 print " " init
", /* " name
" */"
196 for (i =
0; i
< n_opts
; i
++) {
197 name = static_var
(opts
[i
], flags
[i
]);
199 print " 0, /* " name
" (private state) */"
200 print "#undef x_" name
203 for (i =
0; i
< n_opts
; i
++) {
204 if (flag_set_p
("SetByCombined", flags
[i
]))
205 print " false, /* frontend_set_" var_name
(flags
[i
]) " */"
209 print "struct gcc_options global_options;"
210 print "struct gcc_options global_options_set;"
213 print "const char * const lang_names[] =\n{"
214 for (i =
0; i
< n_langs
; i
++) {
215 macros
[i
] =
"CL_" lang_sanitized_name
(langs
[i
])
216 s =
substr(" ", length (macros
[i
]))
217 print " " quote langs
[i
] quote
","
221 print "const unsigned int cl_options_count = N_OPTS;\n"
222 print "#if (1U << " n_langs
") > CL_MIN_OPTION_CLASS"
223 print " #error the number of languages exceeds the implementation limit"
225 print "const unsigned int cl_lang_count = " n_langs
";\n"
227 print "const struct cl_option cl_options[] =\n{"
230 for (i =
0; i
< n_opts
; i
++) {
231 back_chain
[i
] =
"N_OPTS";
232 indices
[opts
[i
]] = j
;
233 # Combine the flags of identical switches. Switches
234 # appear many times if they are handled by many front
236 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
237 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
238 if (help
[i
+ 1] ==
"")
239 help
[i
+ 1] = help
[i
]
240 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
])
241 print "#error Multiple different help strings for " \
242 opts
[i
] ":\n\t" help
[i
] "\n\t" help
[i
+ 1]
245 back_chain
[i
] =
"N_OPTS";
246 indices
[opts
[i
]] = j
;
251 for (i =
0; i
< n_opts
; i
++) {
252 # With identical flags, pick only the last one. The
253 # earlier loop ensured that it has all flags merged,
254 # and a nonempty help text if one of the texts was nonempty.
255 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
259 len =
length (opts
[i
]);
260 enum = opt_enum
(opts
[i
])
262 # If this switch takes joined arguments, back-chain all
263 # subsequent switches to it for which it is a prefix. If
264 # a later switch S is a longer prefix of a switch T, T
265 # will be back-chained to S in a later iteration of this
266 # for() loop, which is what we want.
267 if (flag_set_p
("Joined.*", flags
[i
])) {
268 for (j = i
+ 1; j
< n_opts
; j
++) {
269 if (substr (opts
[j
], 1, len
) != opts
[i
])
271 back_chain
[j
] = enum
;
275 s =
substr(" ", length (opts
[i
]))
282 hlp = quote help
[i
] quote
;
284 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
285 if (missing_arg_error ==
"")
286 missing_arg_error =
"0"
288 missing_arg_error = quote missing_arg_error quote
291 warn_message = opt_args
("Warn", flags
[i
])
292 if (warn_message ==
"")
295 warn_message = quote warn_message quote
297 alias_arg = opt_args
("Alias", flags
[i
])
298 if (alias_arg ==
"") {
299 if (flag_set_p
("Ignore", flags
[i
]))
300 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
302 alias_data =
"NULL, NULL, N_OPTS"
304 alias_opt = nth_arg
(0, alias_arg
)
305 alias_posarg = nth_arg
(1, alias_arg
)
306 alias_negarg = nth_arg
(2, alias_arg
)
308 if (var_ref
(opts
[i
], flags
[i
]) != "-1")
309 print "#error Alias setting variable"
311 if (alias_posarg
!= "" && alias_negarg ==
"") {
312 if (!flag_set_p
("RejectNegative", flags
[i
]) \
313 && opts
[i
] ~
"^[Wfm]")
314 print "#error Alias with single argument " \
315 "allowing negative form"
317 if (alias_posarg
!= "" \
318 && flag_set_p
("NegativeAlias", flags
[i
])) {
319 print "#error Alias with multiple arguments " \
320 "used with NegativeAlias"
323 alias_opt = opt_enum
(alias_opt
)
324 if (alias_posarg ==
"")
325 alias_posarg =
"NULL"
327 alias_posarg = quote alias_posarg quote
328 if (alias_negarg ==
"")
329 alias_negarg =
"NULL"
331 alias_negarg = quote alias_negarg quote
332 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
335 neg = opt_args
("Negative", flags
[i
]);
339 if (flag_set_p
("RejectNegative", flags
[i
]))
342 if (opts
[i
] ~
"^[Wfm]")
343 idx = indices
[opts
[i
]];
348 # Split the printf after %u to work around an ia64-hp-hpux11.23
350 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
351 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
352 alias_data
, back_chain
[i
], len
)
353 printf(" %d,\n", idx
)
354 condition = opt_args
("Condition", flags
[i
])
355 cl_flags = switch_flags
(flags
[i
])
356 cl_bit_fields = switch_bit_fields
(flags
[i
])
357 cl_zero_bit_fields = switch_bit_fields
("")
364 " 1 /* Disabled. */, %s,\n" \
366 condition
, cl_flags
, cl_bit_fields
, cl_zero_bit_fields
)
370 cl_flags
, cl_bit_fields
)
371 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
372 var_set
(flags
[i
]), comma
)
379 print "common_handle_option_auto (struct gcc_options *opts, "
380 print " struct gcc_options *opts_set, "
381 print " const struct cl_decoded_option *decoded, "
382 print " unsigned int lang_mask, int kind, "
383 print " location_t loc, "
384 print " const struct cl_option_handlers *handlers, "
385 print " diagnostic_context *dc) "
387 print " size_t scode = decoded->opt_index; "
388 print " int value = decoded->value; "
389 print " enum opt_code code = (enum opt_code) scode; "
391 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
393 print " switch (code) "
396 for (i =
0; i
< n_enabledby
; i
++) {
397 enabledby_name = enabledby
[i
];
398 print " case " opt_enum
(enabledby_name
) ":"
399 n_enables =
split(enables
[enabledby_name
], thisenable
, ";");
400 n_enablesif =
split(enablesif
[enabledby_name
], thisenableif
, ";");
401 if (n_enables
!= n_enablesif
) {
402 print "#error n_enables != n_enablesif: Something went wrong!"
404 for (j =
1; j
< n_enables
; j
++) {
405 opt_var_name = var_name
(flags
[opt_numbers
[thisenable
[j
]]]);
406 if (opt_var_name
!= "") {
407 condition =
"!opts_set->x_" opt_var_name
408 if (thisenableif
[j
] != "") {
409 condition = condition
" && (" thisenableif
[j
] ")"
411 print " if (" condition
")"
412 print " handle_generated_option (opts, opts_set,"
413 print " " opt_enum
(thisenable
[j
]) ", NULL, value,"
414 print " lang_mask, kind, loc, handlers, dc);"
416 print "#error " thisenable
[j
] " does not have a Var() flag"
424 print " return true; "
427 # Handle LangEnabledBy
428 for (i =
0; i
< n_langs
; i
++) {
429 lang_name = lang_sanitized_name
(langs
[i
]);
430 mark_unused =
" ATTRIBUTE_UNUSED";
434 print lang_name
"_handle_option_auto (struct gcc_options *opts" mark_unused
", "
435 print " struct gcc_options *opts_set" mark_unused
", "
436 print " size_t scode" mark_unused
", const char *arg" mark_unused
", int value" mark_unused
", "
437 print " unsigned int lang_mask" mark_unused
", int kind" mark_unused
", "
438 print " location_t loc" mark_unused
", "
439 print " const struct cl_option_handlers *handlers" mark_unused
", "
440 print " diagnostic_context *dc" mark_unused
") "
442 print " enum opt_code code = (enum opt_code) scode; "
444 print " switch (code) "
447 for (k =
0; k
< n_enabledby_lang
[i
]; k
++) {
448 enabledby_name = enabledby
[lang_name
,k
];
449 print " case " opt_enum
(enabledby_name
) ":"
450 n_thisenable =
split(enables
[lang_name
,enabledby_name
], thisenable
, ";");
451 for (j =
1; j
< n_thisenable
; j
++) {
452 n_thisenable_args =
split(thisenable
[j
], thisenable_args
, ",");
453 if (n_thisenable_args ==
1) {
454 thisenable_opt = thisenable
[j
];
457 thisenable_opt = thisenable_args
[1];
458 with_posarg = thisenable_args
[2];
459 with_negarg = thisenable_args
[3];
460 value =
"value ? " with_posarg
" : " with_negarg
;
462 opt_var_name = var_name
(flags
[opt_numbers
[thisenable_opt
]]);
463 if (opt_var_name
!= "") {
464 print " if (!opts_set->x_" opt_var_name
")"
465 print " handle_generated_option (opts, opts_set,"
466 print " " opt_enum
(thisenable_opt
) ", NULL, " value
","
467 print " lang_mask, kind, loc, handlers, dc);"
469 print "#error " thisenable_opt
" does not have a Var() flag"
477 print " return true; "