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
37 # Default the name of header created from opth-gen.awk to options.h
38 if (header_name == "") header_name="options.h"
41 # Collect the text and flags of each option into an array
43 if ($1 == "Language") {
47 else if ($1 == "TargetSave") {
48 # Make sure the declarations are put in source order
49 target_save_decl[n_target_save] = $2
52 else if ($1 == "Variable") {
53 extra_vars[n_extra_vars] = $2
57 name = opt_args("Mask", $1)
62 for (i = 4; i <= NF; i++)
63 help[n_opts] = help[n_opts] " " $i
69 # Dump that array of options into a C file.
71 print "/* This file is auto-generated by optc-gen.awk. */"
73 n_headers = split(header_name, headers, " ")
74 for (i = 1; i <= n_headers; i++)
75 print "#include " quote headers[i] quote
76 print "#include " quote "opts.h" quote
77 print "#include " quote "intl.h" quote
79 print "#ifndef GCC_DRIVER"
80 print "#include " quote "flags.h" quote
81 print "#include " quote "target.h" quote
82 print "#endif /* GCC_DRIVER */"
86 print "const struct gcc_options global_options_init =\n{"
87 for (i = 0; i < n_extra_vars; i++) {
91 sub(".*= *", "", init)
93 sub("^.*[ *]", "", var)
94 sub("\\[.*\\]$", "", var)
99 print " " init ", /* " var " */"
101 for (i = 0; i < n_opts; i++) {
102 if (flag_set_p("Save", flags[i]))
105 name = var_name(flags[i]);
109 init = opt_args("Init", flags[i])
111 if (name in var_init && var_init[name] != init)
112 print "#error multiple initializers for " name
113 var_init[name] = init
116 for (i = 0; i < n_opts; i++) {
117 name = var_name(flags[i]);
121 if (name in var_seen)
124 if (name in var_init)
125 init = var_init[name]
129 print " " init ", /* " name " */"
133 for (i = 0; i < n_opts; i++) {
134 name = static_var(opts[i], flags[i]);
136 print " 0, /* " name " (private state) */"
137 print "#undef x_" name
142 print "struct gcc_options global_options;"
143 print "struct gcc_options global_options_set;"
146 print "const char * const lang_names[] =\n{"
147 for (i = 0; i < n_langs; i++) {
148 macros[i] = "CL_" langs[i]
149 gsub( "[^" alnum "_]", "X", macros[i] )
150 s = substr(" ", length (macros[i]))
151 print " " quote langs[i] quote ","
155 print "const unsigned int cl_options_count = N_OPTS;\n"
156 print "const unsigned int cl_lang_count = " n_langs ";\n"
158 print "const struct cl_option cl_options[] =\n{"
161 for (i = 0; i < n_opts; i++) {
162 back_chain[i] = "N_OPTS";
163 indices[opts[i]] = j;
164 # Combine the flags of identical switches. Switches
165 # appear many times if they are handled by many front
167 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
168 flags[i + 1] = flags[i] " " flags[i + 1];
169 if (help[i + 1] == "")
170 help[i + 1] = help[i]
171 else if (help[i] != "" && help[i + 1] != help[i])
172 print "warning: multiple different help strings for " \
173 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
176 back_chain[i] = "N_OPTS";
177 indices[opts[i]] = j;
182 for (i = 0; i < n_opts; i++) {
183 # With identical flags, pick only the last one. The
184 # earlier loop ensured that it has all flags merged,
185 # and a nonempty help text if one of the texts was nonempty.
186 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
190 len = length (opts[i]);
191 enum = opt_enum(opts[i])
193 # If this switch takes joined arguments, back-chain all
194 # subsequent switches to it for which it is a prefix. If
195 # a later switch S is a longer prefix of a switch T, T
196 # will be back-chained to S in a later iteration of this
197 # for() loop, which is what we want.
198 if (flag_set_p("Joined.*", flags[i])) {
199 for (j = i + 1; j < n_opts; j++) {
200 if (substr (opts[j], 1, len) != opts[i])
202 back_chain[j] = enum;
206 s = substr(" ", length (opts[i]))
213 hlp = quote help[i] quote;
215 missing_arg_error = opt_args("MissingArgError", flags[i])
216 if (missing_arg_error == "")
217 missing_arg_error = "0"
219 missing_arg_error = quote missing_arg_error quote
222 warn_message = opt_args("Warn", flags[i])
223 if (warn_message == "")
226 warn_message = quote warn_message quote
228 alias_arg = opt_args("Alias", flags[i])
229 if (alias_arg == "") {
230 if (flag_set_p("Ignore", flags[i]))
231 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
233 alias_data = "NULL, NULL, N_OPTS"
235 alias_opt = nth_arg(0, alias_arg)
236 alias_posarg = nth_arg(1, alias_arg)
237 alias_negarg = nth_arg(2, alias_arg)
239 if (var_ref(opts[i], flags[i]) != "-1")
240 print "#error Alias setting variable"
242 if (alias_posarg != "" && alias_negarg == "") {
243 if (!flag_set_p("RejectNegative", flags[i]) \
244 && opts[i] ~ "^[Wfm]")
245 print "#error Alias with single argument " \
246 "allowing negative form"
249 alias_opt = opt_enum(alias_opt)
250 if (alias_posarg == "")
251 alias_posarg = "NULL"
253 alias_posarg = quote alias_posarg quote
254 if (alias_negarg == "")
255 alias_negarg = "NULL"
257 alias_negarg = quote alias_negarg quote
258 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
261 neg = opt_args("Negative", flags[i]);
265 if (flag_set_p("RejectNegative", flags[i]))
268 if (opts[i] ~ "^[Wfm]")
269 idx = indices[opts[i]];
274 # Split the printf after %u to work around an ia64-hp-hpux11.23
276 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
277 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
278 alias_data, back_chain[i], len)
279 printf(" %d,\n", idx)
280 condition = opt_args("Condition", flags[i])
281 cl_flags = switch_flags(flags[i])
288 condition, cl_flags, cl_flags)
290 printf(" %s,\n", cl_flags)
291 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
292 var_set(flags[i]), comma)
298 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
300 print "/* Save optimization variables into a structure. */"
302 print "cl_optimization_save (struct cl_optimization *ptr, struct gcc_options *opts)";
309 var_opt_char[0] = "optimize";
310 var_opt_char[1] = "optimize_size";
311 var_opt_range["optimize"] = "0, 255";
312 var_opt_range["optimize_size"] = "0, 255";
314 # Sort by size to mimic how the structure is laid out to be friendlier to the
317 for (i = 0; i < n_opts; i++) {
318 if (flag_set_p("Optimization", flags[i])) {
319 name = var_name(flags[i])
323 if(name in var_opt_seen)
326 var_opt_seen[name]++;
327 otype = var_type_struct(flags[i]);
328 if (otype ~ "^((un)?signed +)?int *$")
329 var_opt_int[n_opt_int++] = name;
331 else if (otype ~ "^((un)?signed +)?short *$")
332 var_opt_short[n_opt_short++] = name;
334 else if (otype ~ "^((un)?signed +)?char *$") {
335 var_opt_char[n_opt_char++] = name;
336 if (otype ~ "^unsigned +char *$")
337 var_opt_range[name] = "0, 255"
338 else if (otype ~ "^signed +char *$")
339 var_opt_range[name] = "-128, 127"
342 var_opt_other[n_opt_other++] = name;
346 for (i = 0; i < n_opt_char; i++) {
347 name = var_opt_char[i];
348 if (var_opt_range[name] != "")
349 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_opt_range[name] "));";
353 for (i = 0; i < n_opt_other; i++) {
354 print " ptr->x_" var_opt_other[i] " = opts->x_" var_opt_other[i] ";";
357 for (i = 0; i < n_opt_int; i++) {
358 print " ptr->x_" var_opt_int[i] " = opts->x_" var_opt_int[i] ";";
361 for (i = 0; i < n_opt_short; i++) {
362 print " ptr->x_" var_opt_short[i] " = opts->x_" var_opt_short[i] ";";
365 for (i = 0; i < n_opt_char; i++) {
366 print " ptr->x_" var_opt_char[i] " = opts->x_" var_opt_char[i] ";";
372 print "/* Restore optimization options from a structure. */";
374 print "cl_optimization_restore (struct gcc_options *opts, struct cl_optimization *ptr)";
377 for (i = 0; i < n_opt_other; i++) {
378 print " opts->x_" var_opt_other[i] " = ptr->x_" var_opt_other[i] ";";
381 for (i = 0; i < n_opt_int; i++) {
382 print " opts->x_" var_opt_int[i] " = ptr->x_" var_opt_int[i] ";";
385 for (i = 0; i < n_opt_short; i++) {
386 print " opts->x_" var_opt_short[i] " = ptr->x_" var_opt_short[i] ";";
389 for (i = 0; i < n_opt_char; i++) {
390 print " opts->x_" var_opt_char[i] " = ptr->x_" var_opt_char[i] ";";
393 print " targetm.override_options_after_change ();";
397 print "/* Print optimization options from a structure. */";
399 print "cl_optimization_print (FILE *file,";
400 print " int indent_to,";
401 print " struct cl_optimization *ptr)";
404 print " fputs (\"\\n\", file);";
405 for (i = 0; i < n_opt_other; i++) {
406 print " if (ptr->x_" var_opt_other[i] ")";
407 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
408 print " indent_to, \"\",";
409 print " \"" var_opt_other[i] "\",";
410 print " (unsigned long)ptr->x_" var_opt_other[i] ");";
414 for (i = 0; i < n_opt_int; i++) {
415 print " if (ptr->x_" var_opt_int[i] ")";
416 print " fprintf (file, \"%*s%s (%#x)\\n\",";
417 print " indent_to, \"\",";
418 print " \"" var_opt_int[i] "\",";
419 print " ptr->x_" var_opt_int[i] ");";
423 for (i = 0; i < n_opt_short; i++) {
424 print " if (ptr->x_" var_opt_short[i] ")";
425 print " fprintf (file, \"%*s%s (%#x)\\n\",";
426 print " indent_to, \"\",";
427 print " \"" var_opt_short[i] "\",";
428 print " ptr->x_" var_opt_short[i] ");";
432 for (i = 0; i < n_opt_char; i++) {
433 print " if (ptr->x_" var_opt_char[i] ")";
434 print " fprintf (file, \"%*s%s (%#x)\\n\",";
435 print " indent_to, \"\",";
436 print " \"" var_opt_char[i] "\",";
437 print " ptr->x_" var_opt_char[i] ");";
444 print "/* Save selected option variables into a structure. */"
446 print "cl_target_option_save (struct cl_target_option *ptr, struct gcc_options *opts)";
455 for (i = 0; i < n_opts; i++) {
456 if (flag_set_p("Save", flags[i])) {
457 name = var_name(flags[i])
459 name = "target_flags";
461 if(name in var_save_seen)
464 var_save_seen[name]++;
465 otype = var_type_struct(flags[i])
466 if (otype ~ "^((un)?signed +)?int *$")
467 var_target_int[n_target_int++] = name;
469 else if (otype ~ "^((un)?signed +)?short *$")
470 var_target_short[n_target_short++] = name;
472 else if (otype ~ "^((un)?signed +)?char *$") {
473 var_target_char[n_target_char++] = name;
474 if (otype ~ "^unsigned +char *$")
475 var_target_range[name] = "0, 255"
476 else if (otype ~ "^signed +char *$")
477 var_target_range[name] = "-128, 127"
480 var_target_other[n_target_other++] = name;
484 var_target_int[n_target_int++] = "target_flags";
488 for (i = 0; i < n_target_char; i++) {
489 name = var_target_char[i];
490 if (var_target_range[name] != "") {
492 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_target_range[name] "));";
499 print " if (targetm.target_option.save)";
500 print " targetm.target_option.save (ptr);";
503 for (i = 0; i < n_target_other; i++) {
504 print " ptr->x_" var_target_other[i] " = opts->x_" var_target_other[i] ";";
507 for (i = 0; i < n_target_int; i++) {
508 print " ptr->x_" var_target_int[i] " = opts->x_" var_target_int[i] ";";
511 for (i = 0; i < n_target_short; i++) {
512 print " ptr->x_" var_target_short[i] " = opts->x_" var_target_short[i] ";";
515 for (i = 0; i < n_target_char; i++) {
516 print " ptr->x_" var_target_char[i] " = opts->x_" var_target_char[i] ";";
522 print "/* Restore selected current options from a structure. */";
524 print "cl_target_option_restore (struct gcc_options *opts, struct cl_target_option *ptr)";
527 for (i = 0; i < n_target_other; i++) {
528 print " opts->x_" var_target_other[i] " = ptr->x_" var_target_other[i] ";";
531 for (i = 0; i < n_target_int; i++) {
532 print " opts->x_" var_target_int[i] " = ptr->x_" var_target_int[i] ";";
535 for (i = 0; i < n_target_short; i++) {
536 print " opts->x_" var_target_short[i] " = ptr->x_" var_target_short[i] ";";
539 for (i = 0; i < n_target_char; i++) {
540 print " opts->x_" var_target_char[i] " = ptr->x_" var_target_char[i] ";";
543 # This must occur after the normal variables in case the code depends on those
546 print " if (targetm.target_option.restore)";
547 print " targetm.target_option.restore (ptr);";
552 print "/* Print optimization options from a structure. */";
554 print "cl_target_option_print (FILE *file,";
555 print " int indent,";
556 print " struct cl_target_option *ptr)";
559 print " fputs (\"\\n\", file);";
560 for (i = 0; i < n_target_other; i++) {
561 print " if (ptr->x_" var_target_other[i] ")";
562 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
563 print " indent, \"\",";
564 print " \"" var_target_other[i] "\",";
565 print " (unsigned long)ptr->x_" var_target_other[i] ");";
569 for (i = 0; i < n_target_int; i++) {
570 print " if (ptr->x_" var_target_int[i] ")";
571 print " fprintf (file, \"%*s%s (%#x)\\n\",";
572 print " indent, \"\",";
573 print " \"" var_target_int[i] "\",";
574 print " ptr->x_" var_target_int[i] ");";
578 for (i = 0; i < n_target_short; i++) {
579 print " if (ptr->x_" var_target_short[i] ")";
580 print " fprintf (file, \"%*s%s (%#x)\\n\",";
581 print " indent, \"\",";
582 print " \"" var_target_short[i] "\",";
583 print " ptr->x_" var_target_short[i] ");";
587 for (i = 0; i < n_target_char; i++) {
588 print " if (ptr->x_" var_target_char[i] ")";
589 print " fprintf (file, \"%*s%s (%#x)\\n\",";
590 print " indent, \"\",";
591 print " \"" var_target_char[i] "\",";
592 print " ptr->x_" var_target_char[i] ");";
597 print " if (targetm.target_option.print)";
598 print " targetm.target_option.print (file, indent, ptr);";