PR debug/49032
[official-gcc.git] / gcc / optc-gen.awk
blob2c4df708801e09de498b8d9efba09f2bebbcd079
1 # Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
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 {
33 print "/* This file is auto-generated by optc-gen.awk. */"
34 print ""
35 n_headers = split(header_name, headers, " ")
36 for (i = 1; i <= n_headers; i++)
37 print "#include " quote headers[i] quote
38 print "#include " quote "opts.h" quote
39 print "#include " quote "intl.h" quote
40 print ""
41 print "#ifndef GCC_DRIVER"
42 print "#include " quote "flags.h" quote
43 print "#include " quote "target.h" quote
44 print "#endif /* GCC_DRIVER */"
45 print ""
47 if (n_extra_c_includes > 0) {
48 for (i = 0; i < n_extra_c_includes; i++) {
49 print "#include " quote extra_c_includes[i] quote
51 print ""
54 for (i = 0; i < n_enums; i++) {
55 name = enum_names[i]
56 type = enum_type[name]
57 print "static const struct cl_enum_arg cl_enum_" name \
58 "_data[] = "
59 print "{"
60 print enum_data[name] " { NULL, 0, 0 }"
61 print "};"
62 print ""
63 print "static void"
64 print "cl_enum_" name "_set (void *var, int value)"
65 print "{"
66 print " *((" type " *) var) = (" type ") value;"
67 print "}"
68 print ""
69 print "static int"
70 print "cl_enum_" name "_get (const void *var)"
71 print "{"
72 print " return (int) *((const " type " *) var);"
73 print "}"
74 print ""
77 print "const struct cl_enum cl_enums[] ="
78 print "{"
79 for (i = 0; i < n_enums; i++) {
80 name = enum_names[i]
81 ehelp = enum_help[name]
82 if (ehelp == "")
83 ehelp = "NULL"
84 else
85 ehelp = quote ehelp quote
86 unknown_error = enum_unknown_error[name]
87 if (unknown_error == "")
88 unknown_error = "NULL"
89 else
90 unknown_error = quote unknown_error quote
91 print " {"
92 print " " ehelp ","
93 print " " unknown_error ","
94 print " cl_enum_" name "_data,"
95 print " sizeof (" enum_type[name] "),"
96 print " cl_enum_" name "_set,"
97 print " cl_enum_" name "_get"
98 print " },"
100 print "};"
101 print "const unsigned int cl_enums_count = " n_enums ";"
102 print ""
104 have_save = 0;
105 if (n_extra_target_vars)
106 have_save = 1
108 print "const struct gcc_options global_options_init =\n{"
109 for (i = 0; i < n_extra_vars; i++) {
110 var = extra_vars[i]
111 init = extra_vars[i]
112 if (var ~ "=" ) {
113 sub(".*= *", "", init)
114 } else {
115 init = "0"
117 sub(" *=.*", "", var)
118 name = var
119 sub("^.*[ *]", "", name)
120 sub("\\[.*\\]$", "", name)
121 var_seen[name] = 1
122 print " " init ", /* " name " */"
124 for (i = 0; i < n_opts; i++) {
125 if (flag_set_p("Save", flags[i]))
126 have_save = 1;
128 name = var_name(flags[i]);
129 if (name == "")
130 continue;
132 init = opt_args("Init", flags[i])
133 if (init != "") {
134 if (name in var_init && var_init[name] != init)
135 print "#error multiple initializers for " name
136 var_init[name] = init
139 for (i = 0; i < n_opts; i++) {
140 name = var_name(flags[i]);
141 if (name == "")
142 continue;
144 if (name in var_seen)
145 continue;
147 if (name in var_init)
148 init = var_init[name]
149 else
150 init = "0"
152 print " " init ", /* " name " */"
154 var_seen[name] = 1;
156 for (i = 0; i < n_opts; i++) {
157 name = static_var(opts[i], flags[i]);
158 if (name != "") {
159 print " 0, /* " name " (private state) */"
160 print "#undef x_" name
163 for (i = 0; i < n_opts; i++) {
164 if (flag_set_p("SetByCombined", flags[i]))
165 print " false, /* frontend_set_" var_name(flags[i]) " */"
167 print "};"
168 print ""
169 print "struct gcc_options global_options;"
170 print "struct gcc_options global_options_set;"
171 print ""
173 print "const char * const lang_names[] =\n{"
174 for (i = 0; i < n_langs; i++) {
175 macros[i] = "CL_" langs[i]
176 gsub( "[^" alnum "_]", "X", macros[i] )
177 s = substr(" ", length (macros[i]))
178 print " " quote langs[i] quote ","
181 print " 0\n};\n"
182 print "const unsigned int cl_options_count = N_OPTS;\n"
183 print "const unsigned int cl_lang_count = " n_langs ";\n"
185 print "const struct cl_option cl_options[] =\n{"
187 j = 0
188 for (i = 0; i < n_opts; i++) {
189 back_chain[i] = "N_OPTS";
190 indices[opts[i]] = j;
191 # Combine the flags of identical switches. Switches
192 # appear many times if they are handled by many front
193 # ends, for example.
194 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
195 flags[i + 1] = flags[i] " " flags[i + 1];
196 if (help[i + 1] == "")
197 help[i + 1] = help[i]
198 else if (help[i] != "" && help[i + 1] != help[i])
199 print "warning: multiple different help strings for " \
200 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
201 | "cat 1>&2"
202 i++;
203 back_chain[i] = "N_OPTS";
204 indices[opts[i]] = j;
206 j++;
209 for (i = 0; i < n_opts; i++) {
210 # With identical flags, pick only the last one. The
211 # earlier loop ensured that it has all flags merged,
212 # and a nonempty help text if one of the texts was nonempty.
213 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
214 i++;
217 len = length (opts[i]);
218 enum = opt_enum(opts[i])
220 # If this switch takes joined arguments, back-chain all
221 # subsequent switches to it for which it is a prefix. If
222 # a later switch S is a longer prefix of a switch T, T
223 # will be back-chained to S in a later iteration of this
224 # for() loop, which is what we want.
225 if (flag_set_p("Joined.*", flags[i])) {
226 for (j = i + 1; j < n_opts; j++) {
227 if (substr (opts[j], 1, len) != opts[i])
228 break;
229 back_chain[j] = enum;
233 s = substr(" ", length (opts[i]))
234 if (i + 1 == n_opts)
235 comma = ""
237 if (help[i] == "")
238 hlp = "0"
239 else
240 hlp = quote help[i] quote;
242 missing_arg_error = opt_args("MissingArgError", flags[i])
243 if (missing_arg_error == "")
244 missing_arg_error = "0"
245 else
246 missing_arg_error = quote missing_arg_error quote
249 warn_message = opt_args("Warn", flags[i])
250 if (warn_message == "")
251 warn_message = "0"
252 else
253 warn_message = quote warn_message quote
255 alias_arg = opt_args("Alias", flags[i])
256 if (alias_arg == "") {
257 if (flag_set_p("Ignore", flags[i]))
258 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
259 else
260 alias_data = "NULL, NULL, N_OPTS"
261 } else {
262 alias_opt = nth_arg(0, alias_arg)
263 alias_posarg = nth_arg(1, alias_arg)
264 alias_negarg = nth_arg(2, alias_arg)
266 if (var_ref(opts[i], flags[i]) != "-1")
267 print "#error Alias setting variable"
269 if (alias_posarg != "" && alias_negarg == "") {
270 if (!flag_set_p("RejectNegative", flags[i]) \
271 && opts[i] ~ "^[Wfm]")
272 print "#error Alias with single argument " \
273 "allowing negative form"
275 if (alias_posarg != "" \
276 && flag_set_p("NegativeAlias", flags[i])) {
277 print "#error Alias with multiple arguments " \
278 "used with NegativeAlias"
281 alias_opt = opt_enum(alias_opt)
282 if (alias_posarg == "")
283 alias_posarg = "NULL"
284 else
285 alias_posarg = quote alias_posarg quote
286 if (alias_negarg == "")
287 alias_negarg = "NULL"
288 else
289 alias_negarg = quote alias_negarg quote
290 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
293 neg = opt_args("Negative", flags[i]);
294 if (neg != "")
295 idx = indices[neg]
296 else {
297 if (flag_set_p("RejectNegative", flags[i]))
298 idx = -1;
299 else {
300 if (opts[i] ~ "^[Wfm]")
301 idx = indices[opts[i]];
302 else
303 idx = -1;
306 # Split the printf after %u to work around an ia64-hp-hpux11.23
307 # awk bug.
308 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
309 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
310 alias_data, back_chain[i], len)
311 printf(" %d,\n", idx)
312 condition = opt_args("Condition", flags[i])
313 cl_flags = switch_flags(flags[i])
314 cl_bit_fields = switch_bit_fields(flags[i])
315 cl_zero_bit_fields = switch_bit_fields("")
316 if (condition != "")
317 printf("#if %s\n" \
318 " %s,\n" \
319 " 0, %s,\n" \
320 "#else\n" \
321 " 0,\n" \
322 " 1 /* Disabled. */, %s,\n" \
323 "#endif\n",
324 condition, cl_flags, cl_bit_fields, cl_zero_bit_fields)
325 else
326 printf(" %s,\n" \
327 " 0, %s,\n",
328 cl_flags, cl_bit_fields)
329 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
330 var_set(flags[i]), comma)
333 print "};"
335 print "";
336 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
337 print "";
338 print "/* Save optimization variables into a structure. */"
339 print "void";
340 print "cl_optimization_save (struct cl_optimization *ptr, struct gcc_options *opts)";
341 print "{";
343 n_opt_char = 2;
344 n_opt_short = 0;
345 n_opt_int = 0;
346 n_opt_enum = 1;
347 n_opt_other = 0;
348 var_opt_char[0] = "optimize";
349 var_opt_char[1] = "optimize_size";
350 var_opt_range["optimize"] = "0, 255";
351 var_opt_range["optimize_size"] = "0, 255";
352 var_opt_enum[0] = "flag_fp_contract_mode";
354 # Sort by size to mimic how the structure is laid out to be friendlier to the
355 # cache.
357 for (i = 0; i < n_opts; i++) {
358 if (flag_set_p("Optimization", flags[i])) {
359 name = var_name(flags[i])
360 if(name == "")
361 continue;
363 if(name in var_opt_seen)
364 continue;
366 var_opt_seen[name]++;
367 otype = var_type_struct(flags[i]);
368 if (otype ~ "^((un)?signed +)?int *$")
369 var_opt_int[n_opt_int++] = name;
371 else if (otype ~ "^((un)?signed +)?short *$")
372 var_opt_short[n_opt_short++] = name;
374 else if (otype ~ ("^enum +[_" alnum "]+ *"))
375 var_opt_enum[n_opt_enum++] = name;
377 else if (otype ~ "^((un)?signed +)?char *$") {
378 var_opt_char[n_opt_char++] = name;
379 if (otype ~ "^unsigned +char *$")
380 var_opt_range[name] = "0, 255"
381 else if (otype ~ "^signed +char *$")
382 var_opt_range[name] = "-128, 127"
384 else
385 var_opt_other[n_opt_other++] = name;
389 for (i = 0; i < n_opt_char; i++) {
390 name = var_opt_char[i];
391 if (var_opt_range[name] != "")
392 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_opt_range[name] "));";
395 print "";
396 for (i = 0; i < n_opt_other; i++) {
397 print " ptr->x_" var_opt_other[i] " = opts->x_" var_opt_other[i] ";";
400 for (i = 0; i < n_opt_int; i++) {
401 print " ptr->x_" var_opt_int[i] " = opts->x_" var_opt_int[i] ";";
404 for (i = 0; i < n_opt_enum; i++) {
405 print " ptr->x_" var_opt_enum[i] " = opts->x_" var_opt_enum[i] ";";
408 for (i = 0; i < n_opt_short; i++) {
409 print " ptr->x_" var_opt_short[i] " = opts->x_" var_opt_short[i] ";";
412 for (i = 0; i < n_opt_char; i++) {
413 print " ptr->x_" var_opt_char[i] " = opts->x_" var_opt_char[i] ";";
416 print "}";
418 print "";
419 print "/* Restore optimization options from a structure. */";
420 print "void";
421 print "cl_optimization_restore (struct gcc_options *opts, struct cl_optimization *ptr)";
422 print "{";
424 for (i = 0; i < n_opt_other; i++) {
425 print " opts->x_" var_opt_other[i] " = ptr->x_" var_opt_other[i] ";";
428 for (i = 0; i < n_opt_int; i++) {
429 print " opts->x_" var_opt_int[i] " = ptr->x_" var_opt_int[i] ";";
432 for (i = 0; i < n_opt_enum; i++) {
433 print " opts->x_" var_opt_enum[i] " = ptr->x_" var_opt_enum[i] ";";
436 for (i = 0; i < n_opt_short; i++) {
437 print " opts->x_" var_opt_short[i] " = ptr->x_" var_opt_short[i] ";";
440 for (i = 0; i < n_opt_char; i++) {
441 print " opts->x_" var_opt_char[i] " = ptr->x_" var_opt_char[i] ";";
444 print " targetm.override_options_after_change ();";
445 print "}";
447 print "";
448 print "/* Print optimization options from a structure. */";
449 print "void";
450 print "cl_optimization_print (FILE *file,";
451 print " int indent_to,";
452 print " struct cl_optimization *ptr)";
453 print "{";
455 print " fputs (\"\\n\", file);";
456 for (i = 0; i < n_opt_other; i++) {
457 print " if (ptr->x_" var_opt_other[i] ")";
458 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
459 print " indent_to, \"\",";
460 print " \"" var_opt_other[i] "\",";
461 print " (unsigned long)ptr->x_" var_opt_other[i] ");";
462 print "";
465 for (i = 0; i < n_opt_int; i++) {
466 print " if (ptr->x_" var_opt_int[i] ")";
467 print " fprintf (file, \"%*s%s (%#x)\\n\",";
468 print " indent_to, \"\",";
469 print " \"" var_opt_int[i] "\",";
470 print " ptr->x_" var_opt_int[i] ");";
471 print "";
474 for (i = 0; i < n_opt_enum; i++) {
475 print " fprintf (file, \"%*s%s (%#x)\\n\",";
476 print " indent_to, \"\",";
477 print " \"" var_opt_enum[i] "\",";
478 print " (int) ptr->x_" var_opt_enum[i] ");";
479 print "";
482 for (i = 0; i < n_opt_short; i++) {
483 print " if (ptr->x_" var_opt_short[i] ")";
484 print " fprintf (file, \"%*s%s (%#x)\\n\",";
485 print " indent_to, \"\",";
486 print " \"" var_opt_short[i] "\",";
487 print " ptr->x_" var_opt_short[i] ");";
488 print "";
491 for (i = 0; i < n_opt_char; i++) {
492 print " if (ptr->x_" var_opt_char[i] ")";
493 print " fprintf (file, \"%*s%s (%#x)\\n\",";
494 print " indent_to, \"\",";
495 print " \"" var_opt_char[i] "\",";
496 print " ptr->x_" var_opt_char[i] ");";
497 print "";
500 print "}";
502 print "";
503 print "/* Save selected option variables into a structure. */"
504 print "void";
505 print "cl_target_option_save (struct cl_target_option *ptr, struct gcc_options *opts)";
506 print "{";
508 n_target_char = 0;
509 n_target_short = 0;
510 n_target_int = 0;
511 n_target_enum = 0;
512 n_target_other = 0;
514 if (have_save) {
515 for (i = 0; i < n_opts; i++) {
516 if (flag_set_p("Save", flags[i])) {
517 name = var_name(flags[i])
518 if(name == "")
519 name = "target_flags";
521 if(name in var_save_seen)
522 continue;
524 var_save_seen[name]++;
525 otype = var_type_struct(flags[i])
526 if (otype ~ "^((un)?signed +)?int *$")
527 var_target_int[n_target_int++] = name;
529 else if (otype ~ "^((un)?signed +)?short *$")
530 var_target_short[n_target_short++] = name;
532 else if (otype ~ ("^enum +[_" alnum "]+ *$"))
533 var_target_enum[n_target_enum++] = name;
535 else if (otype ~ "^((un)?signed +)?char *$") {
536 var_target_char[n_target_char++] = name;
537 if (otype ~ "^unsigned +char *$")
538 var_target_range[name] = "0, 255"
539 else if (otype ~ "^signed +char *$")
540 var_target_range[name] = "-128, 127"
541 if (otype == var_type(flags[i]))
542 var_target_range[name] = ""
544 else
545 var_target_other[n_target_other++] = name;
548 } else {
549 var_target_int[n_target_int++] = "target_flags";
552 have_assert = 0;
553 for (i = 0; i < n_target_char; i++) {
554 name = var_target_char[i];
555 if (var_target_range[name] != "") {
556 have_assert = 1;
557 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_target_range[name] "));";
561 if (have_assert)
562 print "";
564 print " if (targetm.target_option.save)";
565 print " targetm.target_option.save (ptr);";
566 print "";
568 for (i = 0; i < n_extra_target_vars; i++) {
569 print " ptr->x_" extra_target_vars[i] " = opts->x_" extra_target_vars[i] ";";
572 for (i = 0; i < n_target_other; i++) {
573 print " ptr->x_" var_target_other[i] " = opts->x_" var_target_other[i] ";";
576 for (i = 0; i < n_target_enum; i++) {
577 print " ptr->x_" var_target_enum[i] " = opts->x_" var_target_enum[i] ";";
580 for (i = 0; i < n_target_int; i++) {
581 print " ptr->x_" var_target_int[i] " = opts->x_" var_target_int[i] ";";
584 for (i = 0; i < n_target_short; i++) {
585 print " ptr->x_" var_target_short[i] " = opts->x_" var_target_short[i] ";";
588 for (i = 0; i < n_target_char; i++) {
589 print " ptr->x_" var_target_char[i] " = opts->x_" var_target_char[i] ";";
592 print "}";
594 print "";
595 print "/* Restore selected current options from a structure. */";
596 print "void";
597 print "cl_target_option_restore (struct gcc_options *opts, struct cl_target_option *ptr)";
598 print "{";
600 for (i = 0; i < n_extra_target_vars; i++) {
601 print " opts->x_" extra_target_vars[i] " = ptr->x_" extra_target_vars[i] ";";
604 for (i = 0; i < n_target_other; i++) {
605 print " opts->x_" var_target_other[i] " = ptr->x_" var_target_other[i] ";";
608 for (i = 0; i < n_target_enum; i++) {
609 print " opts->x_" var_target_enum[i] " = ptr->x_" var_target_enum[i] ";";
612 for (i = 0; i < n_target_int; i++) {
613 print " opts->x_" var_target_int[i] " = ptr->x_" var_target_int[i] ";";
616 for (i = 0; i < n_target_short; i++) {
617 print " opts->x_" var_target_short[i] " = ptr->x_" var_target_short[i] ";";
620 for (i = 0; i < n_target_char; i++) {
621 print " opts->x_" var_target_char[i] " = ptr->x_" var_target_char[i] ";";
624 # This must occur after the normal variables in case the code depends on those
625 # variables.
626 print "";
627 print " if (targetm.target_option.restore)";
628 print " targetm.target_option.restore (ptr);";
630 print "}";
632 print "";
633 print "/* Print optimization options from a structure. */";
634 print "void";
635 print "cl_target_option_print (FILE *file,";
636 print " int indent,";
637 print " struct cl_target_option *ptr)";
638 print "{";
640 print " fputs (\"\\n\", file);";
641 for (i = 0; i < n_target_other; i++) {
642 print " if (ptr->x_" var_target_other[i] ")";
643 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
644 print " indent, \"\",";
645 print " \"" var_target_other[i] "\",";
646 print " (unsigned long)ptr->x_" var_target_other[i] ");";
647 print "";
650 for (i = 0; i < n_target_enum; i++) {
651 print " if (ptr->x_" var_target_enum[i] ")";
652 print " fprintf (file, \"%*s%s (%#x)\\n\",";
653 print " indent, \"\",";
654 print " \"" var_target_enum[i] "\",";
655 print " ptr->x_" var_target_enum[i] ");";
656 print "";
659 for (i = 0; i < n_target_int; i++) {
660 print " if (ptr->x_" var_target_int[i] ")";
661 print " fprintf (file, \"%*s%s (%#x)\\n\",";
662 print " indent, \"\",";
663 print " \"" var_target_int[i] "\",";
664 print " ptr->x_" var_target_int[i] ");";
665 print "";
668 for (i = 0; i < n_target_short; i++) {
669 print " if (ptr->x_" var_target_short[i] ")";
670 print " fprintf (file, \"%*s%s (%#x)\\n\",";
671 print " indent, \"\",";
672 print " \"" var_target_short[i] "\",";
673 print " ptr->x_" var_target_short[i] ");";
674 print "";
677 for (i = 0; i < n_target_char; i++) {
678 print " if (ptr->x_" var_target_char[i] ")";
679 print " fprintf (file, \"%*s%s (%#x)\\n\",";
680 print " indent, \"\",";
681 print " \"" var_target_char[i] "\",";
682 print " ptr->x_" var_target_char[i] ");";
683 print "";
686 print "";
687 print " if (targetm.target_option.print)";
688 print " targetm.target_option.print (file, indent, ptr);";
690 print "}";
691 print "#endif";