2013-01-08 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / optc-gen.awk
blobcc098887ea4312a3481fe1582a05c0a43ebbee95
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
9 # later version.
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
22 # C file.
25 # This program uses functions from opt-functions.awk and code from
26 # opt-read.awk.
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.
32 END {
34 # Record first EnabledBy and LangEnabledBy uses.
35 n_enabledby = 0;
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
51 } else {
52 condition = "";
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;
66 n_enabledby++;
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 enabledby_langs = nth_arg(0, enabledby_arg);
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 lang_enabled_by(enabledby_langs, enabledby_name, enabledby_posarg, enabledby_negarg);
85 print "/* This file is auto-generated by optc-gen.awk. */"
86 print ""
87 n_headers = split(header_name, headers, " ")
88 for (i = 1; i <= n_headers; i++)
89 print "#include " quote headers[i] quote
90 print "#include " quote "opts.h" quote
91 print "#include " quote "intl.h" quote
92 print "#include " quote "insn-attr-common.h" quote
93 print ""
95 if (n_extra_c_includes > 0) {
96 for (i = 0; i < n_extra_c_includes; i++) {
97 print "#include " quote extra_c_includes[i] quote
99 print ""
102 for (i = 0; i < n_enums; i++) {
103 name = enum_names[i]
104 type = enum_type[name]
105 print "static const struct cl_enum_arg cl_enum_" name \
106 "_data[] = "
107 print "{"
108 print enum_data[name] " { NULL, 0, 0 }"
109 print "};"
110 print ""
111 print "static void"
112 print "cl_enum_" name "_set (void *var, int value)"
113 print "{"
114 print " *((" type " *) var) = (" type ") value;"
115 print "}"
116 print ""
117 print "static int"
118 print "cl_enum_" name "_get (const void *var)"
119 print "{"
120 print " return (int) *((const " type " *) var);"
121 print "}"
122 print ""
125 print "const struct cl_enum cl_enums[] ="
126 print "{"
127 for (i = 0; i < n_enums; i++) {
128 name = enum_names[i]
129 ehelp = enum_help[name]
130 if (ehelp == "")
131 ehelp = "NULL"
132 else
133 ehelp = quote ehelp quote
134 unknown_error = enum_unknown_error[name]
135 if (unknown_error == "")
136 unknown_error = "NULL"
137 else
138 unknown_error = quote unknown_error quote
139 print " {"
140 print " " ehelp ","
141 print " " unknown_error ","
142 print " cl_enum_" name "_data,"
143 print " sizeof (" enum_type[name] "),"
144 print " cl_enum_" name "_set,"
145 print " cl_enum_" name "_get"
146 print " },"
148 print "};"
149 print "const unsigned int cl_enums_count = " n_enums ";"
150 print ""
152 print "const struct gcc_options global_options_init =\n{"
153 for (i = 0; i < n_extra_vars; i++) {
154 var = extra_vars[i]
155 init = extra_vars[i]
156 if (var ~ "=" ) {
157 sub(".*= *", "", init)
158 } else {
159 init = "0"
161 sub(" *=.*", "", var)
162 name = var
163 sub("^.*[ *]", "", name)
164 sub("\\[.*\\]$", "", name)
165 var_seen[name] = 1
166 print " " init ", /* " name " */"
168 for (i = 0; i < n_opts; i++) {
169 name = var_name(flags[i]);
170 if (name == "")
171 continue;
173 init = opt_args("Init", flags[i])
174 if (init != "") {
175 if (name in var_init && var_init[name] != init)
176 print "#error multiple initializers for " name
177 var_init[name] = init
180 for (i = 0; i < n_opts; i++) {
181 name = var_name(flags[i]);
182 if (name == "")
183 continue;
185 if (name in var_seen)
186 continue;
188 if (name in var_init)
189 init = var_init[name]
190 else
191 init = "0"
193 print " " init ", /* " name " */"
195 var_seen[name] = 1;
197 for (i = 0; i < n_opts; i++) {
198 name = static_var(opts[i], flags[i]);
199 if (name != "") {
200 print " 0, /* " name " (private state) */"
201 print "#undef x_" name
204 for (i = 0; i < n_opts; i++) {
205 if (flag_set_p("SetByCombined", flags[i]))
206 print " false, /* frontend_set_" var_name(flags[i]) " */"
208 print "};"
209 print ""
210 print "struct gcc_options global_options;"
211 print "struct gcc_options global_options_set;"
212 print ""
214 print "const char * const lang_names[] =\n{"
215 for (i = 0; i < n_langs; i++) {
216 macros[i] = "CL_" lang_sanitized_name(langs[i])
217 s = substr(" ", length (macros[i]))
218 print " " quote langs[i] quote ","
221 print " 0\n};\n"
222 print "const unsigned int cl_options_count = N_OPTS;\n"
223 print "#if (1U << " n_langs ") > CL_MIN_OPTION_CLASS"
224 print " #error the number of languages exceeds the implementation limit"
225 print "#endif"
226 print "const unsigned int cl_lang_count = " n_langs ";\n"
228 print "const struct cl_option cl_options[] =\n{"
230 j = 0
231 for (i = 0; i < n_opts; i++) {
232 back_chain[i] = "N_OPTS";
233 indices[opts[i]] = j;
234 # Combine the flags of identical switches. Switches
235 # appear many times if they are handled by many front
236 # ends, for example.
237 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
238 flags[i + 1] = flags[i] " " flags[i + 1];
239 if (help[i + 1] == "")
240 help[i + 1] = help[i]
241 else if (help[i] != "" && help[i + 1] != help[i])
242 print "#error Multiple different help strings for " \
243 opts[i] ":\n\t" help[i] "\n\t" help[i + 1]
245 i++;
246 back_chain[i] = "N_OPTS";
247 indices[opts[i]] = j;
249 j++;
252 for (i = 0; i < n_opts; i++) {
253 # With identical flags, pick only the last one. The
254 # earlier loop ensured that it has all flags merged,
255 # and a nonempty help text if one of the texts was nonempty.
256 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
257 i++;
260 len = length (opts[i]);
261 enum = opt_enum(opts[i])
263 # If this switch takes joined arguments, back-chain all
264 # subsequent switches to it for which it is a prefix. If
265 # a later switch S is a longer prefix of a switch T, T
266 # will be back-chained to S in a later iteration of this
267 # for() loop, which is what we want.
268 if (flag_set_p("Joined.*", flags[i])) {
269 for (j = i + 1; j < n_opts; j++) {
270 if (substr (opts[j], 1, len) != opts[i])
271 break;
272 back_chain[j] = enum;
276 s = substr(" ", length (opts[i]))
277 if (i + 1 == n_opts)
278 comma = ""
280 if (help[i] == "")
281 hlp = "0"
282 else
283 hlp = quote help[i] quote;
285 missing_arg_error = opt_args("MissingArgError", flags[i])
286 if (missing_arg_error == "")
287 missing_arg_error = "0"
288 else
289 missing_arg_error = quote missing_arg_error quote
292 warn_message = opt_args("Warn", flags[i])
293 if (warn_message == "")
294 warn_message = "0"
295 else
296 warn_message = quote warn_message quote
298 alias_arg = opt_args("Alias", flags[i])
299 if (alias_arg == "") {
300 if (flag_set_p("Ignore", flags[i]))
301 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
302 else
303 alias_data = "NULL, NULL, N_OPTS"
304 } else {
305 alias_opt = nth_arg(0, alias_arg)
306 alias_posarg = nth_arg(1, alias_arg)
307 alias_negarg = nth_arg(2, alias_arg)
309 if (var_ref(opts[i], flags[i]) != "-1")
310 print "#error Alias setting variable"
312 if (alias_posarg != "" && alias_negarg == "") {
313 if (!flag_set_p("RejectNegative", flags[i]) \
314 && opts[i] ~ "^[Wfm]")
315 print "#error Alias with single argument " \
316 "allowing negative form"
318 if (alias_posarg != "" \
319 && flag_set_p("NegativeAlias", flags[i])) {
320 print "#error Alias with multiple arguments " \
321 "used with NegativeAlias"
324 alias_opt = opt_enum(alias_opt)
325 if (alias_posarg == "")
326 alias_posarg = "NULL"
327 else
328 alias_posarg = quote alias_posarg quote
329 if (alias_negarg == "")
330 alias_negarg = "NULL"
331 else
332 alias_negarg = quote alias_negarg quote
333 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
336 neg = opt_args("Negative", flags[i]);
337 if (neg != "")
338 idx = indices[neg]
339 else {
340 if (flag_set_p("RejectNegative", flags[i]))
341 idx = -1;
342 else {
343 if (opts[i] ~ "^[Wfm]")
344 idx = indices[opts[i]];
345 else
346 idx = -1;
349 # Split the printf after %u to work around an ia64-hp-hpux11.23
350 # awk bug.
351 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
352 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
353 alias_data, back_chain[i], len)
354 printf(" %d,\n", idx)
355 condition = opt_args("Condition", flags[i])
356 cl_flags = switch_flags(flags[i])
357 cl_bit_fields = switch_bit_fields(flags[i])
358 cl_zero_bit_fields = switch_bit_fields("")
359 if (condition != "")
360 printf("#if %s\n" \
361 " %s,\n" \
362 " 0, %s,\n" \
363 "#else\n" \
364 " 0,\n" \
365 " 1 /* Disabled. */, %s,\n" \
366 "#endif\n",
367 condition, cl_flags, cl_bit_fields, cl_zero_bit_fields)
368 else
369 printf(" %s,\n" \
370 " 0, %s,\n",
371 cl_flags, cl_bit_fields)
372 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
373 var_set(flags[i]), comma)
376 print "};"
378 print "\n\n"
379 print "bool "
380 print "common_handle_option_auto (struct gcc_options *opts, "
381 print " struct gcc_options *opts_set, "
382 print " const struct cl_decoded_option *decoded, "
383 print " unsigned int lang_mask, int kind, "
384 print " location_t loc, "
385 print " const struct cl_option_handlers *handlers, "
386 print " diagnostic_context *dc) "
387 print "{ "
388 print " size_t scode = decoded->opt_index; "
389 print " int value = decoded->value; "
390 print " enum opt_code code = (enum opt_code) scode; "
391 print " "
392 print " gcc_assert (decoded->canonical_option_num_elements <= 2); "
393 print " "
394 print " switch (code) "
395 print " { "
396 # Handle EnabledBy
397 for (i = 0; i < n_enabledby; i++) {
398 enabledby_name = enabledby[i];
399 print " case " opt_enum(enabledby_name) ":"
400 n_enables = split(enables[enabledby_name], thisenable, ";");
401 n_enablesif = split(enablesif[enabledby_name], thisenableif, ";");
402 if (n_enables != n_enablesif) {
403 print "#error n_enables != n_enablesif: Something went wrong!"
405 for (j = 1; j < n_enables; j++) {
406 opt_var_name = var_name(flags[opt_numbers[thisenable[j]]]);
407 if (opt_var_name != "") {
408 condition = "!opts_set->x_" opt_var_name
409 if (thisenableif[j] != "") {
410 condition = condition " && (" thisenableif[j] ")"
412 print " if (" condition ")"
413 print " handle_generated_option (opts, opts_set,"
414 print " " opt_enum(thisenable[j]) ", NULL, value,"
415 print " lang_mask, kind, loc, handlers, dc);"
416 } else {
417 print "#error " thisenable[j] " does not have a Var() flag"
420 print " break;\n"
422 print " default: "
423 print " break; "
424 print " } "
425 print " return true; "
426 print "} "
428 # Handle LangEnabledBy
429 for (i = 0; i < n_langs; i++) {
430 lang_name = lang_sanitized_name(langs[i]);
431 mark_unused = " ATTRIBUTE_UNUSED";
433 print "\n\n"
434 print "bool "
435 print lang_name "_handle_option_auto (struct gcc_options *opts" mark_unused ", "
436 print " struct gcc_options *opts_set" mark_unused ", "
437 print " size_t scode" mark_unused ", const char *arg" mark_unused ", int value" mark_unused ", "
438 print " unsigned int lang_mask" mark_unused ", int kind" mark_unused ", "
439 print " location_t loc" mark_unused ", "
440 print " const struct cl_option_handlers *handlers" mark_unused ", "
441 print " diagnostic_context *dc" mark_unused ") "
442 print "{ "
443 print " enum opt_code code = (enum opt_code) scode; "
444 print " "
445 print " switch (code) "
446 print " { "
448 for (k = 0; k < n_enabledby_lang[i]; k++) {
449 enabledby_name = enabledby[lang_name,k];
450 print " case " opt_enum(enabledby_name) ":"
451 n_thisenable = split(enables[lang_name,enabledby_name], thisenable, ";");
452 for (j = 1; j < n_thisenable; j++) {
453 n_thisenable_args = split(thisenable[j], thisenable_args, ",");
454 if (n_thisenable_args == 1) {
455 thisenable_opt = thisenable[j];
456 value = "value";
457 } else {
458 thisenable_opt = thisenable_args[1];
459 with_posarg = thisenable_args[2];
460 with_negarg = thisenable_args[3];
461 value = "value ? " with_posarg " : " with_negarg;
463 opt_var_name = var_name(flags[opt_numbers[thisenable_opt]]);
464 if (opt_var_name != "") {
465 print " if (!opts_set->x_" opt_var_name ")"
466 print " handle_generated_option (opts, opts_set,"
467 print " " opt_enum(thisenable_opt) ", NULL, " value ","
468 print " lang_mask, kind, loc, handlers, dc);"
469 } else {
470 print "#error " thisenable_opt " does not have a Var() flag"
473 print " break;\n"
475 print " default: "
476 print " break; "
477 print " } "
478 print " return true; "
479 print "} "