1 # Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
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
24 # This program uses functions from opt-functions.awk
26 # Usage: awk -f opt-functions.awk -f optc-gen.awk \
27 # [-v header_name=header.h] < inputfile > options.c
34 n_extra_target_vars = 0
35 n_extra_c_includes = 0
36 n_extra_h_includes = 0
40 # Default the name of header created from opth-gen.awk to options.h
41 if (header_name == "") header_name="options.h"
44 # Collect the text and flags of each option into an array
46 if ($1 == "Language") {
50 else if ($1 == "TargetSave") {
51 # Make sure the declarations are put in source order
52 target_save_decl[n_target_save] = $2
55 else if ($1 == "Variable") {
56 extra_vars[n_extra_vars] = $2
59 else if ($1 == "TargetVariable") {
60 # Combination of TargetSave and Variable
61 extra_vars[n_extra_vars] = $2
69 sub("^.*[ *]", "", name)
70 sub(" *" name "$", "", type)
71 target_save_decl[n_target_save] = type " x_" name
74 extra_target_vars[n_extra_target_vars] = name
75 n_extra_target_vars++;
77 else if ($1 == "HeaderInclude") {
78 extra_h_includes[n_extra_h_includes++] = $2;
80 else if ($1 == "SourceInclude") {
81 extra_c_includes[n_extra_c_includes++] = $2;
84 name = opt_args("Mask", $1)
89 for (i = 4; i <= NF; i++)
90 help[n_opts] = help[n_opts] " " $i
96 # Dump that array of options into a C file.
98 print "/* This file is auto-generated by optc-gen.awk. */"
100 n_headers = split(header_name, headers, " ")
101 for (i = 1; i <= n_headers; i++)
102 print "#include " quote headers[i] quote
103 print "#include " quote "opts.h" quote
104 print "#include " quote "intl.h" quote
106 print "#ifndef GCC_DRIVER"
107 print "#include " quote "flags.h" quote
108 print "#include " quote "target.h" quote
109 print "#endif /* GCC_DRIVER */"
112 if (n_extra_c_includes > 0) {
113 for (i = 0; i < n_extra_c_includes; i++) {
114 print "#include " quote extra_c_includes[i] quote
120 if (n_extra_target_vars)
123 print "const struct gcc_options global_options_init =\n{"
124 for (i = 0; i < n_extra_vars; i++) {
128 sub(".*= *", "", init)
129 sub(" *=.*", "", var)
130 sub("^.*[ *]", "", var)
131 sub("\\[.*\\]$", "", var)
136 print " " init ", /* " var " */"
138 for (i = 0; i < n_opts; i++) {
139 if (flag_set_p("Save", flags[i]))
142 name = var_name(flags[i]);
146 init = opt_args("Init", flags[i])
148 if (name in var_init && var_init[name] != init)
149 print "#error multiple initializers for " name
150 var_init[name] = init
153 for (i = 0; i < n_opts; i++) {
154 name = var_name(flags[i]);
158 if (name in var_seen)
161 if (name in var_init)
162 init = var_init[name]
166 print " " init ", /* " name " */"
170 for (i = 0; i < n_opts; i++) {
171 name = static_var(opts[i], flags[i]);
173 print " 0, /* " name " (private state) */"
174 print "#undef x_" name
179 print "struct gcc_options global_options;"
180 print "struct gcc_options global_options_set;"
183 print "const char * const lang_names[] =\n{"
184 for (i = 0; i < n_langs; i++) {
185 macros[i] = "CL_" langs[i]
186 gsub( "[^" alnum "_]", "X", macros[i] )
187 s = substr(" ", length (macros[i]))
188 print " " quote langs[i] quote ","
192 print "const unsigned int cl_options_count = N_OPTS;\n"
193 print "const unsigned int cl_lang_count = " n_langs ";\n"
195 print "const struct cl_option cl_options[] =\n{"
198 for (i = 0; i < n_opts; i++) {
199 back_chain[i] = "N_OPTS";
200 indices[opts[i]] = j;
201 # Combine the flags of identical switches. Switches
202 # appear many times if they are handled by many front
204 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
205 flags[i + 1] = flags[i] " " flags[i + 1];
206 if (help[i + 1] == "")
207 help[i + 1] = help[i]
208 else if (help[i] != "" && help[i + 1] != help[i])
209 print "warning: multiple different help strings for " \
210 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
213 back_chain[i] = "N_OPTS";
214 indices[opts[i]] = j;
219 for (i = 0; i < n_opts; i++) {
220 # With identical flags, pick only the last one. The
221 # earlier loop ensured that it has all flags merged,
222 # and a nonempty help text if one of the texts was nonempty.
223 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
227 len = length (opts[i]);
228 enum = opt_enum(opts[i])
230 # If this switch takes joined arguments, back-chain all
231 # subsequent switches to it for which it is a prefix. If
232 # a later switch S is a longer prefix of a switch T, T
233 # will be back-chained to S in a later iteration of this
234 # for() loop, which is what we want.
235 if (flag_set_p("Joined.*", flags[i])) {
236 for (j = i + 1; j < n_opts; j++) {
237 if (substr (opts[j], 1, len) != opts[i])
239 back_chain[j] = enum;
243 s = substr(" ", length (opts[i]))
250 hlp = quote help[i] quote;
252 missing_arg_error = opt_args("MissingArgError", flags[i])
253 if (missing_arg_error == "")
254 missing_arg_error = "0"
256 missing_arg_error = quote missing_arg_error quote
259 warn_message = opt_args("Warn", flags[i])
260 if (warn_message == "")
263 warn_message = quote warn_message quote
265 alias_arg = opt_args("Alias", flags[i])
266 if (alias_arg == "") {
267 if (flag_set_p("Ignore", flags[i]))
268 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
270 alias_data = "NULL, NULL, N_OPTS"
272 alias_opt = nth_arg(0, alias_arg)
273 alias_posarg = nth_arg(1, alias_arg)
274 alias_negarg = nth_arg(2, alias_arg)
276 if (var_ref(opts[i], flags[i]) != "-1")
277 print "#error Alias setting variable"
279 if (alias_posarg != "" && alias_negarg == "") {
280 if (!flag_set_p("RejectNegative", flags[i]) \
281 && opts[i] ~ "^[Wfm]")
282 print "#error Alias with single argument " \
283 "allowing negative form"
286 alias_opt = opt_enum(alias_opt)
287 if (alias_posarg == "")
288 alias_posarg = "NULL"
290 alias_posarg = quote alias_posarg quote
291 if (alias_negarg == "")
292 alias_negarg = "NULL"
294 alias_negarg = quote alias_negarg quote
295 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
298 neg = opt_args("Negative", flags[i]);
302 if (flag_set_p("RejectNegative", flags[i]))
305 if (opts[i] ~ "^[Wfm]")
306 idx = indices[opts[i]];
311 # Split the printf after %u to work around an ia64-hp-hpux11.23
313 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
314 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
315 alias_data, back_chain[i], len)
316 printf(" %d,\n", idx)
317 condition = opt_args("Condition", flags[i])
318 cl_flags = switch_flags(flags[i])
325 condition, cl_flags, cl_flags)
327 printf(" %s,\n", cl_flags)
328 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
329 var_set(flags[i]), comma)
335 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
337 print "/* Save optimization variables into a structure. */"
339 print "cl_optimization_save (struct cl_optimization *ptr, struct gcc_options *opts)";
347 var_opt_char[0] = "optimize";
348 var_opt_char[1] = "optimize_size";
349 var_opt_range["optimize"] = "0, 255";
350 var_opt_range["optimize_size"] = "0, 255";
351 var_opt_enum[0] = "flag_fp_contract_mode";
353 # Sort by size to mimic how the structure is laid out to be friendlier to the
356 for (i = 0; i < n_opts; i++) {
357 if (flag_set_p("Optimization", flags[i])) {
358 name = var_name(flags[i])
362 if(name in var_opt_seen)
365 var_opt_seen[name]++;
366 otype = var_type_struct(flags[i]);
367 if (otype ~ "^((un)?signed +)?int *$")
368 var_opt_int[n_opt_int++] = name;
370 else if (otype ~ "^((un)?signed +)?short *$")
371 var_opt_short[n_opt_short++] = name;
373 else if (otype ~ ("^enum +[_" alnum "]+ *"))
374 var_opt_enum[n_opt_enum++] = name;
376 else if (otype ~ "^((un)?signed +)?char *$") {
377 var_opt_char[n_opt_char++] = name;
378 if (otype ~ "^unsigned +char *$")
379 var_opt_range[name] = "0, 255"
380 else if (otype ~ "^signed +char *$")
381 var_opt_range[name] = "-128, 127"
384 var_opt_other[n_opt_other++] = name;
388 for (i = 0; i < n_opt_char; i++) {
389 name = var_opt_char[i];
390 if (var_opt_range[name] != "")
391 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_opt_range[name] "));";
395 for (i = 0; i < n_opt_other; i++) {
396 print " ptr->x_" var_opt_other[i] " = opts->x_" var_opt_other[i] ";";
399 for (i = 0; i < n_opt_int; i++) {
400 print " ptr->x_" var_opt_int[i] " = opts->x_" var_opt_int[i] ";";
403 for (i = 0; i < n_opt_enum; i++) {
404 print " ptr->x_" var_opt_enum[i] " = opts->x_" var_opt_enum[i] ";";
407 for (i = 0; i < n_opt_short; i++) {
408 print " ptr->x_" var_opt_short[i] " = opts->x_" var_opt_short[i] ";";
411 for (i = 0; i < n_opt_char; i++) {
412 print " ptr->x_" var_opt_char[i] " = opts->x_" var_opt_char[i] ";";
418 print "/* Restore optimization options from a structure. */";
420 print "cl_optimization_restore (struct gcc_options *opts, struct cl_optimization *ptr)";
423 for (i = 0; i < n_opt_other; i++) {
424 print " opts->x_" var_opt_other[i] " = ptr->x_" var_opt_other[i] ";";
427 for (i = 0; i < n_opt_int; i++) {
428 print " opts->x_" var_opt_int[i] " = ptr->x_" var_opt_int[i] ";";
431 for (i = 0; i < n_opt_enum; i++) {
432 print " opts->x_" var_opt_enum[i] " = ptr->x_" var_opt_enum[i] ";";
435 for (i = 0; i < n_opt_short; i++) {
436 print " opts->x_" var_opt_short[i] " = ptr->x_" var_opt_short[i] ";";
439 for (i = 0; i < n_opt_char; i++) {
440 print " opts->x_" var_opt_char[i] " = ptr->x_" var_opt_char[i] ";";
443 print " targetm.override_options_after_change ();";
447 print "/* Print optimization options from a structure. */";
449 print "cl_optimization_print (FILE *file,";
450 print " int indent_to,";
451 print " struct cl_optimization *ptr)";
454 print " fputs (\"\\n\", file);";
455 for (i = 0; i < n_opt_other; i++) {
456 print " if (ptr->x_" var_opt_other[i] ")";
457 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
458 print " indent_to, \"\",";
459 print " \"" var_opt_other[i] "\",";
460 print " (unsigned long)ptr->x_" var_opt_other[i] ");";
464 for (i = 0; i < n_opt_int; i++) {
465 print " if (ptr->x_" var_opt_int[i] ")";
466 print " fprintf (file, \"%*s%s (%#x)\\n\",";
467 print " indent_to, \"\",";
468 print " \"" var_opt_int[i] "\",";
469 print " ptr->x_" var_opt_int[i] ");";
473 for (i = 0; i < n_opt_enum; i++) {
474 print " fprintf (file, \"%*s%s (%#x)\\n\",";
475 print " indent_to, \"\",";
476 print " \"" var_opt_enum[i] "\",";
477 print " (int) ptr->x_" var_opt_enum[i] ");";
481 for (i = 0; i < n_opt_short; i++) {
482 print " if (ptr->x_" var_opt_short[i] ")";
483 print " fprintf (file, \"%*s%s (%#x)\\n\",";
484 print " indent_to, \"\",";
485 print " \"" var_opt_short[i] "\",";
486 print " ptr->x_" var_opt_short[i] ");";
490 for (i = 0; i < n_opt_char; i++) {
491 print " if (ptr->x_" var_opt_char[i] ")";
492 print " fprintf (file, \"%*s%s (%#x)\\n\",";
493 print " indent_to, \"\",";
494 print " \"" var_opt_char[i] "\",";
495 print " ptr->x_" var_opt_char[i] ");";
502 print "/* Save selected option variables into a structure. */"
504 print "cl_target_option_save (struct cl_target_option *ptr, struct gcc_options *opts)";
514 for (i = 0; i < n_opts; i++) {
515 if (flag_set_p("Save", flags[i])) {
516 name = var_name(flags[i])
518 name = "target_flags";
520 if(name in var_save_seen)
523 var_save_seen[name]++;
524 otype = var_type_struct(flags[i])
525 if (otype ~ "^((un)?signed +)?int *$")
526 var_target_int[n_target_int++] = name;
528 else if (otype ~ "^((un)?signed +)?short *$")
529 var_target_short[n_target_short++] = name;
531 else if (otype ~ ("^enum +[_" alnum "]+ *$"))
532 var_target_enum[n_target_enum++] = name;
534 else if (otype ~ "^((un)?signed +)?char *$") {
535 var_target_char[n_target_char++] = name;
536 if (otype ~ "^unsigned +char *$")
537 var_target_range[name] = "0, 255"
538 else if (otype ~ "^signed +char *$")
539 var_target_range[name] = "-128, 127"
542 var_target_other[n_target_other++] = name;
546 var_target_int[n_target_int++] = "target_flags";
550 for (i = 0; i < n_target_char; i++) {
551 name = var_target_char[i];
552 if (var_target_range[name] != "") {
554 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_target_range[name] "));";
561 print " if (targetm.target_option.save)";
562 print " targetm.target_option.save (ptr);";
565 for (i = 0; i < n_extra_target_vars; i++) {
566 print " ptr->x_" extra_target_vars[i] " = opts->x_" extra_target_vars[i] ";";
569 for (i = 0; i < n_target_other; i++) {
570 print " ptr->x_" var_target_other[i] " = opts->x_" var_target_other[i] ";";
573 for (i = 0; i < n_target_enum; i++) {
574 print " ptr->x_" var_target_enum[i] " = opts->x_" var_target_enum[i] ";";
577 for (i = 0; i < n_target_int; i++) {
578 print " ptr->x_" var_target_int[i] " = opts->x_" var_target_int[i] ";";
581 for (i = 0; i < n_target_short; i++) {
582 print " ptr->x_" var_target_short[i] " = opts->x_" var_target_short[i] ";";
585 for (i = 0; i < n_target_char; i++) {
586 print " ptr->x_" var_target_char[i] " = opts->x_" var_target_char[i] ";";
592 print "/* Restore selected current options from a structure. */";
594 print "cl_target_option_restore (struct gcc_options *opts, struct cl_target_option *ptr)";
597 for (i = 0; i < n_extra_target_vars; i++) {
598 print " opts->x_" extra_target_vars[i] " = ptr->x_" extra_target_vars[i] ";";
601 for (i = 0; i < n_target_other; i++) {
602 print " opts->x_" var_target_other[i] " = ptr->x_" var_target_other[i] ";";
605 for (i = 0; i < n_target_enum; i++) {
606 print " opts->x_" var_target_enum[i] " = ptr->x_" var_target_enum[i] ";";
609 for (i = 0; i < n_target_int; i++) {
610 print " opts->x_" var_target_int[i] " = ptr->x_" var_target_int[i] ";";
613 for (i = 0; i < n_target_short; i++) {
614 print " opts->x_" var_target_short[i] " = ptr->x_" var_target_short[i] ";";
617 for (i = 0; i < n_target_char; i++) {
618 print " opts->x_" var_target_char[i] " = ptr->x_" var_target_char[i] ";";
621 # This must occur after the normal variables in case the code depends on those
624 print " if (targetm.target_option.restore)";
625 print " targetm.target_option.restore (ptr);";
630 print "/* Print optimization options from a structure. */";
632 print "cl_target_option_print (FILE *file,";
633 print " int indent,";
634 print " struct cl_target_option *ptr)";
637 print " fputs (\"\\n\", file);";
638 for (i = 0; i < n_target_other; i++) {
639 print " if (ptr->x_" var_target_other[i] ")";
640 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
641 print " indent, \"\",";
642 print " \"" var_target_other[i] "\",";
643 print " (unsigned long)ptr->x_" var_target_other[i] ");";
647 for (i = 0; i < n_target_enum; i++) {
648 print " if (ptr->x_" var_target_enum[i] ")";
649 print " fprintf (file, \"%*s%s (%#x)\\n\",";
650 print " indent, \"\",";
651 print " \"" var_target_enum[i] "\",";
652 print " ptr->x_" var_target_enum[i] ");";
656 for (i = 0; i < n_target_int; i++) {
657 print " if (ptr->x_" var_target_int[i] ")";
658 print " fprintf (file, \"%*s%s (%#x)\\n\",";
659 print " indent, \"\",";
660 print " \"" var_target_int[i] "\",";
661 print " ptr->x_" var_target_int[i] ");";
665 for (i = 0; i < n_target_short; i++) {
666 print " if (ptr->x_" var_target_short[i] ")";
667 print " fprintf (file, \"%*s%s (%#x)\\n\",";
668 print " indent, \"\",";
669 print " \"" var_target_short[i] "\",";
670 print " ptr->x_" var_target_short[i] ");";
674 for (i = 0; i < n_target_char; i++) {
675 print " if (ptr->x_" var_target_char[i] ")";
676 print " fprintf (file, \"%*s%s (%#x)\\n\",";
677 print " indent, \"\",";
678 print " \"" var_target_char[i] "\",";
679 print " ptr->x_" var_target_char[i] ");";
684 print " if (targetm.target_option.print)";
685 print " targetm.target_option.print (file, indent, ptr);";