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 "struct gcc_options global_options =\n{"
87 for (i = 0; i < n_extra_vars; i++) {
91 sub(".*= *", "", init)
93 sub("^.*[ *]", "", var)
98 print " " init ", /* " var " */"
100 for (i = 0; i < n_opts; i++) {
101 if (flag_set_p("Save", flags[i]))
104 name = var_name(flags[i]);
108 init = opt_args("Init", flags[i])
110 if (name in var_init && var_init[name] != init)
111 print "#error multiple initializers for " name
112 var_init[name] = init
115 for (i = 0; i < n_opts; i++) {
116 name = var_name(flags[i]);
120 if (name in var_seen)
123 if (name in var_init)
124 init = var_init[name]
128 print " " init ", /* " name " */"
132 for (i = 0; i < n_opts; i++) {
133 name = static_var(opts[i], flags[i]);
135 print " 0, /* " name " (private state) */"
136 print "#undef x_" name
141 print "struct gcc_options global_options_set;"
144 print "const char * const lang_names[] =\n{"
145 for (i = 0; i < n_langs; i++) {
146 macros[i] = "CL_" langs[i]
147 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
148 s = substr(" ", length (macros[i]))
149 print " " quote langs[i] quote ","
153 print "const unsigned int cl_options_count = N_OPTS;\n"
154 print "const unsigned int cl_lang_count = " n_langs ";\n"
156 print "const struct cl_option cl_options[] =\n{"
159 for (i = 0; i < n_opts; i++) {
160 back_chain[i] = "N_OPTS";
161 indices[opts[i]] = j;
162 # Combine the flags of identical switches. Switches
163 # appear many times if they are handled by many front
165 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
166 flags[i + 1] = flags[i] " " flags[i + 1];
167 if (help[i + 1] == "")
168 help[i + 1] = help[i]
169 else if (help[i] != "" && help[i + 1] != help[i])
170 print "warning: multiple different help strings for " \
171 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
174 back_chain[i] = "N_OPTS";
175 indices[opts[i]] = j;
180 for (i = 0; i < n_opts; i++) {
181 # With identical flags, pick only the last one. The
182 # earlier loop ensured that it has all flags merged,
183 # and a nonempty help text if one of the texts was nonempty.
184 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
188 len = length (opts[i]);
189 enum = opt_enum(opts[i])
191 # If this switch takes joined arguments, back-chain all
192 # subsequent switches to it for which it is a prefix. If
193 # a later switch S is a longer prefix of a switch T, T
194 # will be back-chained to S in a later iteration of this
195 # for() loop, which is what we want.
196 if (flag_set_p("Joined.*", flags[i])) {
197 for (j = i + 1; j < n_opts; j++) {
198 if (substr (opts[j], 1, len) != opts[i])
200 back_chain[j] = enum;
204 s = substr(" ", length (opts[i]))
211 hlp = quote help[i] quote;
213 missing_arg_error = opt_args("MissingArgError", flags[i])
214 if (missing_arg_error == "")
215 missing_arg_error = "0"
217 missing_arg_error = quote missing_arg_error quote
220 warn_message = opt_args("Warn", flags[i])
221 if (warn_message == "")
224 warn_message = quote warn_message quote
226 alias_arg = opt_args("Alias", flags[i])
227 if (alias_arg == "") {
228 if (flag_set_p("Ignore", flags[i]))
229 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
231 alias_data = "NULL, NULL, N_OPTS"
233 alias_opt = nth_arg(0, alias_arg)
234 alias_posarg = nth_arg(1, alias_arg)
235 alias_negarg = nth_arg(2, alias_arg)
237 if (var_ref(opts[i], flags[i]) != "-1")
238 print "#error Alias setting variable"
240 if (alias_posarg != "" && alias_negarg == "") {
241 if (!flag_set_p("RejectNegative", flags[i]) \
242 && opts[i] ~ "^[Wfm]")
243 print "#error Alias with single argument " \
244 "allowing negative form"
247 alias_opt = opt_enum(alias_opt)
248 if (alias_posarg == "")
249 alias_posarg = "NULL"
251 alias_posarg = quote alias_posarg quote
252 if (alias_negarg == "")
253 alias_negarg = "NULL"
255 alias_negarg = quote alias_negarg quote
256 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
259 neg = opt_args("Negative", flags[i]);
263 if (flag_set_p("RejectNegative", flags[i]))
266 if (opts[i] ~ "^[Wfm]")
267 idx = indices[opts[i]];
272 # Split the printf after %u to work around an ia64-hp-hpux11.23
274 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
275 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
276 alias_data, back_chain[i], len)
277 printf(" %d,\n", idx)
278 condition = opt_args("Condition", flags[i])
279 cl_flags = switch_flags(flags[i])
286 condition, cl_flags, cl_flags)
288 printf(" %s,\n", cl_flags)
289 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
290 var_set(flags[i]), comma)
296 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
298 print "/* Save optimization variables into a structure. */"
300 print "cl_optimization_save (struct cl_optimization *ptr, struct gcc_options *opts)";
307 var_opt_char[0] = "optimize";
308 var_opt_char[1] = "optimize_size";
309 var_opt_range["optimize"] = "0, 255";
310 var_opt_range["optimize_size"] = "0, 255";
312 # Sort by size to mimic how the structure is laid out to be friendlier to the
315 for (i = 0; i < n_opts; i++) {
316 if (flag_set_p("Optimization", flags[i])) {
317 name = var_name(flags[i])
321 if(name in var_opt_seen)
324 var_opt_seen[name]++;
325 otype = var_type_struct(flags[i]);
326 if (otype ~ "^((un)?signed +)?int *$")
327 var_opt_int[n_opt_int++] = name;
329 else if (otype ~ "^((un)?signed +)?short *$")
330 var_opt_short[n_opt_short++] = name;
332 else if (otype ~ "^((un)?signed +)?char *$") {
333 var_opt_char[n_opt_char++] = name;
334 if (otype ~ "^unsigned +char *$")
335 var_opt_range[name] = "0, 255"
336 else if (otype ~ "^signed +char *$")
337 var_opt_range[name] = "-128, 127"
340 var_opt_other[n_opt_other++] = name;
344 for (i = 0; i < n_opt_char; i++) {
345 name = var_opt_char[i];
346 if (var_opt_range[name] != "")
347 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_opt_range[name] "));";
351 for (i = 0; i < n_opt_other; i++) {
352 print " ptr->x_" var_opt_other[i] " = opts->x_" var_opt_other[i] ";";
355 for (i = 0; i < n_opt_int; i++) {
356 print " ptr->x_" var_opt_int[i] " = opts->x_" var_opt_int[i] ";";
359 for (i = 0; i < n_opt_short; i++) {
360 print " ptr->x_" var_opt_short[i] " = opts->x_" var_opt_short[i] ";";
363 for (i = 0; i < n_opt_char; i++) {
364 print " ptr->x_" var_opt_char[i] " = opts->x_" var_opt_char[i] ";";
370 print "/* Restore optimization options from a structure. */";
372 print "cl_optimization_restore (struct gcc_options *opts, struct cl_optimization *ptr)";
375 for (i = 0; i < n_opt_other; i++) {
376 print " opts->x_" var_opt_other[i] " = ptr->x_" var_opt_other[i] ";";
379 for (i = 0; i < n_opt_int; i++) {
380 print " opts->x_" var_opt_int[i] " = ptr->x_" var_opt_int[i] ";";
383 for (i = 0; i < n_opt_short; i++) {
384 print " opts->x_" var_opt_short[i] " = ptr->x_" var_opt_short[i] ";";
387 for (i = 0; i < n_opt_char; i++) {
388 print " opts->x_" var_opt_char[i] " = ptr->x_" var_opt_char[i] ";";
391 print " targetm.override_options_after_change ();";
395 print "/* Print optimization options from a structure. */";
397 print "cl_optimization_print (FILE *file,";
398 print " int indent_to,";
399 print " struct cl_optimization *ptr)";
402 print " fputs (\"\\n\", file);";
403 for (i = 0; i < n_opt_other; i++) {
404 print " if (ptr->x_" var_opt_other[i] ")";
405 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
406 print " indent_to, \"\",";
407 print " \"" var_opt_other[i] "\",";
408 print " (unsigned long)ptr->x_" var_opt_other[i] ");";
412 for (i = 0; i < n_opt_int; i++) {
413 print " if (ptr->x_" var_opt_int[i] ")";
414 print " fprintf (file, \"%*s%s (%#x)\\n\",";
415 print " indent_to, \"\",";
416 print " \"" var_opt_int[i] "\",";
417 print " ptr->x_" var_opt_int[i] ");";
421 for (i = 0; i < n_opt_short; i++) {
422 print " if (ptr->x_" var_opt_short[i] ")";
423 print " fprintf (file, \"%*s%s (%#x)\\n\",";
424 print " indent_to, \"\",";
425 print " \"" var_opt_short[i] "\",";
426 print " ptr->x_" var_opt_short[i] ");";
430 for (i = 0; i < n_opt_char; i++) {
431 print " if (ptr->x_" var_opt_char[i] ")";
432 print " fprintf (file, \"%*s%s (%#x)\\n\",";
433 print " indent_to, \"\",";
434 print " \"" var_opt_char[i] "\",";
435 print " ptr->x_" var_opt_char[i] ");";
442 print "/* Save selected option variables into a structure. */"
444 print "cl_target_option_save (struct cl_target_option *ptr, struct gcc_options *opts)";
453 for (i = 0; i < n_opts; i++) {
454 if (flag_set_p("Save", flags[i])) {
455 name = var_name(flags[i])
457 name = "target_flags";
459 if(name in var_save_seen)
462 var_save_seen[name]++;
463 otype = var_type_struct(flags[i])
464 if (otype ~ "^((un)?signed +)?int *$")
465 var_target_int[n_target_int++] = name;
467 else if (otype ~ "^((un)?signed +)?short *$")
468 var_target_short[n_target_short++] = name;
470 else if (otype ~ "^((un)?signed +)?char *$") {
471 var_target_char[n_target_char++] = name;
472 if (otype ~ "^unsigned +char *$")
473 var_target_range[name] = "0, 255"
474 else if (otype ~ "^signed +char *$")
475 var_target_range[name] = "-128, 127"
478 var_target_other[n_target_other++] = name;
482 var_target_int[n_target_int++] = "target_flags";
486 for (i = 0; i < n_target_char; i++) {
487 name = var_target_char[i];
488 if (var_target_range[name] != "") {
490 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_target_range[name] "));";
497 print " if (targetm.target_option.save)";
498 print " targetm.target_option.save (ptr);";
501 for (i = 0; i < n_target_other; i++) {
502 print " ptr->x_" var_target_other[i] " = opts->x_" var_target_other[i] ";";
505 for (i = 0; i < n_target_int; i++) {
506 print " ptr->x_" var_target_int[i] " = opts->x_" var_target_int[i] ";";
509 for (i = 0; i < n_target_short; i++) {
510 print " ptr->x_" var_target_short[i] " = opts->x_" var_target_short[i] ";";
513 for (i = 0; i < n_target_char; i++) {
514 print " ptr->x_" var_target_char[i] " = opts->x_" var_target_char[i] ";";
520 print "/* Restore selected current options from a structure. */";
522 print "cl_target_option_restore (struct gcc_options *opts, struct cl_target_option *ptr)";
525 for (i = 0; i < n_target_other; i++) {
526 print " opts->x_" var_target_other[i] " = ptr->x_" var_target_other[i] ";";
529 for (i = 0; i < n_target_int; i++) {
530 print " opts->x_" var_target_int[i] " = ptr->x_" var_target_int[i] ";";
533 for (i = 0; i < n_target_short; i++) {
534 print " opts->x_" var_target_short[i] " = ptr->x_" var_target_short[i] ";";
537 for (i = 0; i < n_target_char; i++) {
538 print " opts->x_" var_target_char[i] " = ptr->x_" var_target_char[i] ";";
541 # This must occur after the normal variables in case the code depends on those
544 print " if (targetm.target_option.restore)";
545 print " targetm.target_option.restore (ptr);";
550 print "/* Print optimization options from a structure. */";
552 print "cl_target_option_print (FILE *file,";
553 print " int indent,";
554 print " struct cl_target_option *ptr)";
557 print " fputs (\"\\n\", file);";
558 for (i = 0; i < n_target_other; i++) {
559 print " if (ptr->x_" var_target_other[i] ")";
560 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
561 print " indent, \"\",";
562 print " \"" var_target_other[i] "\",";
563 print " (unsigned long)ptr->x_" var_target_other[i] ");";
567 for (i = 0; i < n_target_int; i++) {
568 print " if (ptr->x_" var_target_int[i] ")";
569 print " fprintf (file, \"%*s%s (%#x)\\n\",";
570 print " indent, \"\",";
571 print " \"" var_target_int[i] "\",";
572 print " ptr->x_" var_target_int[i] ");";
576 for (i = 0; i < n_target_short; i++) {
577 print " if (ptr->x_" var_target_short[i] ")";
578 print " fprintf (file, \"%*s%s (%#x)\\n\",";
579 print " indent, \"\",";
580 print " \"" var_target_short[i] "\",";
581 print " ptr->x_" var_target_short[i] ");";
585 for (i = 0; i < n_target_char; i++) {
586 print " if (ptr->x_" var_target_char[i] ")";
587 print " fprintf (file, \"%*s%s (%#x)\\n\",";
588 print " indent, \"\",";
589 print " \"" var_target_char[i] "\",";
590 print " ptr->x_" var_target_char[i] ");";
595 print " if (targetm.target_option.print)";
596 print " targetm.target_option.print (file, indent, ptr);";