fix pr/45972
[official-gcc.git] / gcc / optc-gen.awk
blobfbe8007c8a211ad0f9cac324af32b23cee6419ea
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
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.
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
29 BEGIN {
30 n_opts = 0
31 n_langs = 0
32 n_target_save = 0
33 n_extra_vars = 0
34 quote = "\042"
35 comma = ","
36 FS=SUBSEP
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") {
44 langs[n_langs] = $2
45 n_langs++;
47 else if ($1 == "TargetSave") {
48 # Make sure the declarations are put in source order
49 target_save_decl[n_target_save] = $2
50 n_target_save++
52 else if ($1 == "Variable") {
53 extra_vars[n_extra_vars] = $2
54 n_extra_vars++
56 else {
57 name = opt_args("Mask", $1)
58 if (name == "") {
59 opts[n_opts] = $1
60 flags[n_opts] = $2
61 help[n_opts] = $3
62 for (i = 4; i <= NF; i++)
63 help[n_opts] = help[n_opts] " " $i
64 n_opts++;
69 # Dump that array of options into a C file.
70 END {
71 print "/* This file is auto-generated by optc-gen.awk. */"
72 print ""
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
78 print ""
79 print "#ifndef GCC_DRIVER"
80 print "#include " quote "flags.h" quote
81 print "#include " quote "target.h" quote
82 print "#endif /* GCC_DRIVER */"
83 print ""
85 have_save = 0;
86 print "const struct gcc_options global_options_init =\n{"
87 for (i = 0; i < n_extra_vars; i++) {
88 var = extra_vars[i]
89 init = extra_vars[i]
90 if (var ~ "=" ) {
91 sub(".*= *", "", init)
92 sub(" *=.*", "", var)
93 sub("^.*[ *]", "", var)
94 } else {
95 init = "0"
97 var_seen[var] = 1
98 print " " init ", /* " var " */"
100 for (i = 0; i < n_opts; i++) {
101 if (flag_set_p("Save", flags[i]))
102 have_save = 1;
104 name = var_name(flags[i]);
105 if (name == "")
106 continue;
108 init = opt_args("Init", flags[i])
109 if (init != "") {
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]);
117 if (name == "")
118 continue;
120 if (name in var_seen)
121 continue;
123 if (name in var_init)
124 init = var_init[name]
125 else
126 init = "0"
128 print " " init ", /* " name " */"
130 var_seen[name] = 1;
132 for (i = 0; i < n_opts; i++) {
133 name = static_var(opts[i], flags[i]);
134 if (name != "") {
135 print " 0, /* " name " (private state) */"
136 print "#undef x_" name
139 print "};"
140 print ""
141 print "struct gcc_options global_options;"
142 print "struct gcc_options global_options_set;"
143 print ""
145 print "const char * const lang_names[] =\n{"
146 for (i = 0; i < n_langs; i++) {
147 macros[i] = "CL_" langs[i]
148 gsub( "[^" alnum "_]", "X", macros[i] )
149 s = substr(" ", length (macros[i]))
150 print " " quote langs[i] quote ","
153 print " 0\n};\n"
154 print "const unsigned int cl_options_count = N_OPTS;\n"
155 print "const unsigned int cl_lang_count = " n_langs ";\n"
157 print "const struct cl_option cl_options[] =\n{"
159 j = 0
160 for (i = 0; i < n_opts; i++) {
161 back_chain[i] = "N_OPTS";
162 indices[opts[i]] = j;
163 # Combine the flags of identical switches. Switches
164 # appear many times if they are handled by many front
165 # ends, for example.
166 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
167 flags[i + 1] = flags[i] " " flags[i + 1];
168 if (help[i + 1] == "")
169 help[i + 1] = help[i]
170 else if (help[i] != "" && help[i + 1] != help[i])
171 print "warning: multiple different help strings for " \
172 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
173 | "cat 1>&2"
174 i++;
175 back_chain[i] = "N_OPTS";
176 indices[opts[i]] = j;
178 j++;
181 for (i = 0; i < n_opts; i++) {
182 # With identical flags, pick only the last one. The
183 # earlier loop ensured that it has all flags merged,
184 # and a nonempty help text if one of the texts was nonempty.
185 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
186 i++;
189 len = length (opts[i]);
190 enum = opt_enum(opts[i])
192 # If this switch takes joined arguments, back-chain all
193 # subsequent switches to it for which it is a prefix. If
194 # a later switch S is a longer prefix of a switch T, T
195 # will be back-chained to S in a later iteration of this
196 # for() loop, which is what we want.
197 if (flag_set_p("Joined.*", flags[i])) {
198 for (j = i + 1; j < n_opts; j++) {
199 if (substr (opts[j], 1, len) != opts[i])
200 break;
201 back_chain[j] = enum;
205 s = substr(" ", length (opts[i]))
206 if (i + 1 == n_opts)
207 comma = ""
209 if (help[i] == "")
210 hlp = "0"
211 else
212 hlp = quote help[i] quote;
214 missing_arg_error = opt_args("MissingArgError", flags[i])
215 if (missing_arg_error == "")
216 missing_arg_error = "0"
217 else
218 missing_arg_error = quote missing_arg_error quote
221 warn_message = opt_args("Warn", flags[i])
222 if (warn_message == "")
223 warn_message = "0"
224 else
225 warn_message = quote warn_message quote
227 alias_arg = opt_args("Alias", flags[i])
228 if (alias_arg == "") {
229 if (flag_set_p("Ignore", flags[i]))
230 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
231 else
232 alias_data = "NULL, NULL, N_OPTS"
233 } else {
234 alias_opt = nth_arg(0, alias_arg)
235 alias_posarg = nth_arg(1, alias_arg)
236 alias_negarg = nth_arg(2, alias_arg)
238 if (var_ref(opts[i], flags[i]) != "-1")
239 print "#error Alias setting variable"
241 if (alias_posarg != "" && alias_negarg == "") {
242 if (!flag_set_p("RejectNegative", flags[i]) \
243 && opts[i] ~ "^[Wfm]")
244 print "#error Alias with single argument " \
245 "allowing negative form"
248 alias_opt = opt_enum(alias_opt)
249 if (alias_posarg == "")
250 alias_posarg = "NULL"
251 else
252 alias_posarg = quote alias_posarg quote
253 if (alias_negarg == "")
254 alias_negarg = "NULL"
255 else
256 alias_negarg = quote alias_negarg quote
257 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
260 neg = opt_args("Negative", flags[i]);
261 if (neg != "")
262 idx = indices[neg]
263 else {
264 if (flag_set_p("RejectNegative", flags[i]))
265 idx = -1;
266 else {
267 if (opts[i] ~ "^[Wfm]")
268 idx = indices[opts[i]];
269 else
270 idx = -1;
273 # Split the printf after %u to work around an ia64-hp-hpux11.23
274 # awk bug.
275 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
276 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
277 alias_data, back_chain[i], len)
278 printf(" %d,\n", idx)
279 condition = opt_args("Condition", flags[i])
280 cl_flags = switch_flags(flags[i])
281 if (condition != "")
282 printf("#if %s\n" \
283 " %s,\n" \
284 "#else\n" \
285 " CL_DISABLED,\n" \
286 "#endif\n",
287 condition, cl_flags, cl_flags)
288 else
289 printf(" %s,\n", cl_flags)
290 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
291 var_set(flags[i]), comma)
294 print "};"
296 print "";
297 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
298 print "";
299 print "/* Save optimization variables into a structure. */"
300 print "void";
301 print "cl_optimization_save (struct cl_optimization *ptr, struct gcc_options *opts)";
302 print "{";
304 n_opt_char = 2;
305 n_opt_short = 0;
306 n_opt_int = 0;
307 n_opt_other = 0;
308 var_opt_char[0] = "optimize";
309 var_opt_char[1] = "optimize_size";
310 var_opt_range["optimize"] = "0, 255";
311 var_opt_range["optimize_size"] = "0, 255";
313 # Sort by size to mimic how the structure is laid out to be friendlier to the
314 # cache.
316 for (i = 0; i < n_opts; i++) {
317 if (flag_set_p("Optimization", flags[i])) {
318 name = var_name(flags[i])
319 if(name == "")
320 continue;
322 if(name in var_opt_seen)
323 continue;
325 var_opt_seen[name]++;
326 otype = var_type_struct(flags[i]);
327 if (otype ~ "^((un)?signed +)?int *$")
328 var_opt_int[n_opt_int++] = name;
330 else if (otype ~ "^((un)?signed +)?short *$")
331 var_opt_short[n_opt_short++] = name;
333 else if (otype ~ "^((un)?signed +)?char *$") {
334 var_opt_char[n_opt_char++] = name;
335 if (otype ~ "^unsigned +char *$")
336 var_opt_range[name] = "0, 255"
337 else if (otype ~ "^signed +char *$")
338 var_opt_range[name] = "-128, 127"
340 else
341 var_opt_other[n_opt_other++] = name;
345 for (i = 0; i < n_opt_char; i++) {
346 name = var_opt_char[i];
347 if (var_opt_range[name] != "")
348 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_opt_range[name] "));";
351 print "";
352 for (i = 0; i < n_opt_other; i++) {
353 print " ptr->x_" var_opt_other[i] " = opts->x_" var_opt_other[i] ";";
356 for (i = 0; i < n_opt_int; i++) {
357 print " ptr->x_" var_opt_int[i] " = opts->x_" var_opt_int[i] ";";
360 for (i = 0; i < n_opt_short; i++) {
361 print " ptr->x_" var_opt_short[i] " = opts->x_" var_opt_short[i] ";";
364 for (i = 0; i < n_opt_char; i++) {
365 print " ptr->x_" var_opt_char[i] " = opts->x_" var_opt_char[i] ";";
368 print "}";
370 print "";
371 print "/* Restore optimization options from a structure. */";
372 print "void";
373 print "cl_optimization_restore (struct gcc_options *opts, struct cl_optimization *ptr)";
374 print "{";
376 for (i = 0; i < n_opt_other; i++) {
377 print " opts->x_" var_opt_other[i] " = ptr->x_" var_opt_other[i] ";";
380 for (i = 0; i < n_opt_int; i++) {
381 print " opts->x_" var_opt_int[i] " = ptr->x_" var_opt_int[i] ";";
384 for (i = 0; i < n_opt_short; i++) {
385 print " opts->x_" var_opt_short[i] " = ptr->x_" var_opt_short[i] ";";
388 for (i = 0; i < n_opt_char; i++) {
389 print " opts->x_" var_opt_char[i] " = ptr->x_" var_opt_char[i] ";";
392 print " targetm.override_options_after_change ();";
393 print "}";
395 print "";
396 print "/* Print optimization options from a structure. */";
397 print "void";
398 print "cl_optimization_print (FILE *file,";
399 print " int indent_to,";
400 print " struct cl_optimization *ptr)";
401 print "{";
403 print " fputs (\"\\n\", file);";
404 for (i = 0; i < n_opt_other; i++) {
405 print " if (ptr->x_" var_opt_other[i] ")";
406 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
407 print " indent_to, \"\",";
408 print " \"" var_opt_other[i] "\",";
409 print " (unsigned long)ptr->x_" var_opt_other[i] ");";
410 print "";
413 for (i = 0; i < n_opt_int; i++) {
414 print " if (ptr->x_" var_opt_int[i] ")";
415 print " fprintf (file, \"%*s%s (%#x)\\n\",";
416 print " indent_to, \"\",";
417 print " \"" var_opt_int[i] "\",";
418 print " ptr->x_" var_opt_int[i] ");";
419 print "";
422 for (i = 0; i < n_opt_short; i++) {
423 print " if (ptr->x_" var_opt_short[i] ")";
424 print " fprintf (file, \"%*s%s (%#x)\\n\",";
425 print " indent_to, \"\",";
426 print " \"" var_opt_short[i] "\",";
427 print " ptr->x_" var_opt_short[i] ");";
428 print "";
431 for (i = 0; i < n_opt_char; i++) {
432 print " if (ptr->x_" var_opt_char[i] ")";
433 print " fprintf (file, \"%*s%s (%#x)\\n\",";
434 print " indent_to, \"\",";
435 print " \"" var_opt_char[i] "\",";
436 print " ptr->x_" var_opt_char[i] ");";
437 print "";
440 print "}";
442 print "";
443 print "/* Save selected option variables into a structure. */"
444 print "void";
445 print "cl_target_option_save (struct cl_target_option *ptr, struct gcc_options *opts)";
446 print "{";
448 n_target_char = 0;
449 n_target_short = 0;
450 n_target_int = 0;
451 n_target_other = 0;
453 if (have_save) {
454 for (i = 0; i < n_opts; i++) {
455 if (flag_set_p("Save", flags[i])) {
456 name = var_name(flags[i])
457 if(name == "")
458 name = "target_flags";
460 if(name in var_save_seen)
461 continue;
463 var_save_seen[name]++;
464 otype = var_type_struct(flags[i])
465 if (otype ~ "^((un)?signed +)?int *$")
466 var_target_int[n_target_int++] = name;
468 else if (otype ~ "^((un)?signed +)?short *$")
469 var_target_short[n_target_short++] = name;
471 else if (otype ~ "^((un)?signed +)?char *$") {
472 var_target_char[n_target_char++] = name;
473 if (otype ~ "^unsigned +char *$")
474 var_target_range[name] = "0, 255"
475 else if (otype ~ "^signed +char *$")
476 var_target_range[name] = "-128, 127"
478 else
479 var_target_other[n_target_other++] = name;
482 } else {
483 var_target_int[n_target_int++] = "target_flags";
486 have_assert = 0;
487 for (i = 0; i < n_target_char; i++) {
488 name = var_target_char[i];
489 if (var_target_range[name] != "") {
490 have_assert = 1;
491 print " gcc_assert (IN_RANGE (opts->x_" name ", " var_target_range[name] "));";
495 if (have_assert)
496 print "";
498 print " if (targetm.target_option.save)";
499 print " targetm.target_option.save (ptr);";
500 print "";
502 for (i = 0; i < n_target_other; i++) {
503 print " ptr->x_" var_target_other[i] " = opts->x_" var_target_other[i] ";";
506 for (i = 0; i < n_target_int; i++) {
507 print " ptr->x_" var_target_int[i] " = opts->x_" var_target_int[i] ";";
510 for (i = 0; i < n_target_short; i++) {
511 print " ptr->x_" var_target_short[i] " = opts->x_" var_target_short[i] ";";
514 for (i = 0; i < n_target_char; i++) {
515 print " ptr->x_" var_target_char[i] " = opts->x_" var_target_char[i] ";";
518 print "}";
520 print "";
521 print "/* Restore selected current options from a structure. */";
522 print "void";
523 print "cl_target_option_restore (struct gcc_options *opts, struct cl_target_option *ptr)";
524 print "{";
526 for (i = 0; i < n_target_other; i++) {
527 print " opts->x_" var_target_other[i] " = ptr->x_" var_target_other[i] ";";
530 for (i = 0; i < n_target_int; i++) {
531 print " opts->x_" var_target_int[i] " = ptr->x_" var_target_int[i] ";";
534 for (i = 0; i < n_target_short; i++) {
535 print " opts->x_" var_target_short[i] " = ptr->x_" var_target_short[i] ";";
538 for (i = 0; i < n_target_char; i++) {
539 print " opts->x_" var_target_char[i] " = ptr->x_" var_target_char[i] ";";
542 # This must occur after the normal variables in case the code depends on those
543 # variables.
544 print "";
545 print " if (targetm.target_option.restore)";
546 print " targetm.target_option.restore (ptr);";
548 print "}";
550 print "";
551 print "/* Print optimization options from a structure. */";
552 print "void";
553 print "cl_target_option_print (FILE *file,";
554 print " int indent,";
555 print " struct cl_target_option *ptr)";
556 print "{";
558 print " fputs (\"\\n\", file);";
559 for (i = 0; i < n_target_other; i++) {
560 print " if (ptr->x_" var_target_other[i] ")";
561 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
562 print " indent, \"\",";
563 print " \"" var_target_other[i] "\",";
564 print " (unsigned long)ptr->x_" var_target_other[i] ");";
565 print "";
568 for (i = 0; i < n_target_int; i++) {
569 print " if (ptr->x_" var_target_int[i] ")";
570 print " fprintf (file, \"%*s%s (%#x)\\n\",";
571 print " indent, \"\",";
572 print " \"" var_target_int[i] "\",";
573 print " ptr->x_" var_target_int[i] ");";
574 print "";
577 for (i = 0; i < n_target_short; i++) {
578 print " if (ptr->x_" var_target_short[i] ")";
579 print " fprintf (file, \"%*s%s (%#x)\\n\",";
580 print " indent, \"\",";
581 print " \"" var_target_short[i] "\",";
582 print " ptr->x_" var_target_short[i] ");";
583 print "";
586 for (i = 0; i < n_target_char; i++) {
587 print " if (ptr->x_" var_target_char[i] ")";
588 print " fprintf (file, \"%*s%s (%#x)\\n\",";
589 print " indent, \"\",";
590 print " \"" var_target_char[i] "\",";
591 print " ptr->x_" var_target_char[i] ");";
592 print "";
595 print "";
596 print " if (targetm.target_option.print)";
597 print " targetm.target_option.print (file, indent, ptr);";
599 print "}";
600 print "#endif";