PR other/18555
[official-gcc.git] / gcc / optc-gen.awk
blobbee780c0b638647384c28ffbbd8732c72aeb9636
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 quote = "\042"
34 comma = ","
35 FS=SUBSEP
36 # Default the name of header created from opth-gen.awk to options.h
37 if (header_name == "") header_name="options.h"
40 # Collect the text and flags of each option into an array
42 if ($1 == "Language") {
43 langs[n_langs] = $2
44 n_langs++;
46 else if ($1 == "TargetSave") {
47 # Make sure the declarations are put in source order
48 target_save_decl[n_target_save] = $2
49 n_target_save++
51 else {
52 name = opt_args("Mask", $1)
53 if (name == "") {
54 opts[n_opts] = $1
55 flags[n_opts] = $2
56 help[n_opts] = $3
57 for (i = 4; i <= NF; i++)
58 help[n_opts] = help[n_opts] " " $i
59 n_opts++;
64 # Dump that array of options into a C file.
65 END {
66 print "/* This file is auto-generated by optc-gen.awk. */"
67 print ""
68 n_headers = split(header_name, headers, " ")
69 for (i = 1; i <= n_headers; i++)
70 print "#include " quote headers[i] quote
71 print "#include " quote "opts.h" quote
72 print "#include " quote "intl.h" quote
73 print ""
74 print "#ifdef GCC_DRIVER"
75 print "int target_flags;"
76 print "int target_flags_explicit;"
77 print "#else"
78 print "#include " quote "flags.h" quote
79 print "#include " quote "target.h" quote
80 print "#endif /* GCC_DRIVER */"
81 print ""
83 have_save = 0;
84 for (i = 0; i < n_opts; i++) {
85 if (flag_set_p("Save", flags[i]))
86 have_save = 1;
88 name = var_name(flags[i]);
89 if (name == "")
90 continue;
92 if (flag_set_p("VarExists", flags[i])) {
93 # Need it for the gcc driver.
94 if (name in var_seen)
95 continue;
96 init = ""
97 gcc_driver = 1
99 else {
100 init = opt_args("Init", flags[i])
101 if (init != "")
102 init = " = " init;
103 else if (name in var_seen)
104 continue;
105 gcc_driver = 0
108 if (gcc_driver == 1)
109 print "#ifdef GCC_DRIVER"
110 print "/* Set by -" opts[i] "."
111 print " " help[i] " */"
112 print var_type(flags[i]) name init ";"
113 if (gcc_driver == 1)
114 print "#endif /* GCC_DRIVER */"
115 print ""
117 var_seen[name] = 1;
120 print ""
121 print "/* Local state variables. */"
122 for (i = 0; i < n_opts; i++) {
123 name = static_var(opts[i], flags[i]);
124 if (name != "")
125 print "static " var_type(flags[i]) name ";"
127 print ""
129 print "const char * const lang_names[] =\n{"
130 for (i = 0; i < n_langs; i++) {
131 macros[i] = "CL_" langs[i]
132 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
133 s = substr(" ", length (macros[i]))
134 print " " quote langs[i] quote ","
137 print " 0\n};\n"
138 print "const unsigned int cl_options_count = N_OPTS;\n"
139 print "const unsigned int cl_lang_count = " n_langs ";\n"
141 print "const struct cl_option cl_options[] =\n{"
143 j = 0
144 for (i = 0; i < n_opts; i++) {
145 back_chain[i] = "N_OPTS";
146 indices[opts[i]] = j;
147 # Combine the flags of identical switches. Switches
148 # appear many times if they are handled by many front
149 # ends, for example.
150 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
151 flags[i + 1] = flags[i] " " flags[i + 1];
152 if (help[i + 1] == "")
153 help[i + 1] = help[i]
154 else if (help[i] != "" && help[i + 1] != help[i])
155 print "warning: multiple different help strings for " \
156 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
157 | "cat 1>&2"
158 i++;
159 back_chain[i] = "N_OPTS";
160 indices[opts[i]] = j;
162 j++;
165 for (i = 0; i < n_opts; i++) {
166 # With identical flags, pick only the last one. The
167 # earlier loop ensured that it has all flags merged,
168 # and a nonempty help text if one of the texts was nonempty.
169 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
170 i++;
173 len = length (opts[i]);
174 enum = opt_enum(opts[i])
176 # If this switch takes joined arguments, back-chain all
177 # subsequent switches to it for which it is a prefix. If
178 # a later switch S is a longer prefix of a switch T, T
179 # will be back-chained to S in a later iteration of this
180 # for() loop, which is what we want.
181 if (flag_set_p("Joined.*", flags[i])) {
182 for (j = i + 1; j < n_opts; j++) {
183 if (substr (opts[j], 1, len) != opts[i])
184 break;
185 back_chain[j] = enum;
189 s = substr(" ", length (opts[i]))
190 if (i + 1 == n_opts)
191 comma = ""
193 if (help[i] == "")
194 hlp = "0"
195 else
196 hlp = quote help[i] quote;
198 missing_arg_error = opt_args("MissingArgError", flags[i])
199 if (missing_arg_error == "")
200 missing_arg_error = "0"
201 else
202 missing_arg_error = quote missing_arg_error quote
205 warn_message = opt_args("Warn", flags[i])
206 if (warn_message == "")
207 warn_message = "0"
208 else
209 warn_message = quote warn_message quote
211 alias_arg = opt_args("Alias", flags[i])
212 if (alias_arg == "") {
213 if (flag_set_p("Ignore", flags[i]))
214 alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
215 else
216 alias_data = "NULL, NULL, N_OPTS"
217 } else {
218 alias_opt = nth_arg(0, alias_arg)
219 alias_posarg = nth_arg(1, alias_arg)
220 alias_negarg = nth_arg(2, alias_arg)
222 if (var_ref(opts[i], flags[i]) != "0")
223 print "#error Alias setting variable"
225 if (alias_posarg != "" && alias_negarg == "") {
226 if (!flag_set_p("RejectNegative", flags[i]) \
227 && opts[i] ~ "^[Wfm]")
228 print "#error Alias with single argument " \
229 "allowing negative form"
232 alias_opt = opt_enum(alias_opt)
233 if (alias_posarg == "")
234 alias_posarg = "NULL"
235 else
236 alias_posarg = quote alias_posarg quote
237 if (alias_negarg == "")
238 alias_negarg = "NULL"
239 else
240 alias_negarg = quote alias_negarg quote
241 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
244 neg = opt_args("Negative", flags[i]);
245 if (neg != "")
246 idx = indices[neg]
247 else {
248 if (flag_set_p("RejectNegative", flags[i]))
249 idx = -1;
250 else {
251 if (opts[i] ~ "^[Wfm]")
252 idx = indices[opts[i]];
253 else
254 idx = -1;
257 # Split the printf after %u to work around an ia64-hp-hpux11.23
258 # awk bug.
259 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
260 quote, opts[i], quote, hlp, missing_arg_error, warn_message,
261 alias_data, back_chain[i], len)
262 printf(" %d,\n", idx)
263 condition = opt_args("Condition", flags[i])
264 cl_flags = switch_flags(flags[i])
265 if (condition != "")
266 printf("#if %s\n" \
267 " %s,\n" \
268 "#else\n" \
269 " CL_DISABLED,\n" \
270 "#endif\n",
271 condition, cl_flags, cl_flags)
272 else
273 printf(" %s,\n", cl_flags)
274 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
275 var_set(flags[i]), comma)
278 print "};"
280 print "";
281 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
282 print "";
283 print "/* Save optimization variables into a structure. */"
284 print "void";
285 print "cl_optimization_save (struct cl_optimization *ptr)";
286 print "{";
288 n_opt_char = 2;
289 n_opt_short = 0;
290 n_opt_int = 0;
291 n_opt_other = 0;
292 var_opt_char[0] = "optimize";
293 var_opt_char[1] = "optimize_size";
294 var_opt_range["optimize"] = "0, 255";
295 var_opt_range["optimize_size"] = "0, 255";
297 # Sort by size to mimic how the structure is laid out to be friendlier to the
298 # cache.
300 for (i = 0; i < n_opts; i++) {
301 if (flag_set_p("Optimization", flags[i])) {
302 name = var_name(flags[i])
303 if(name == "")
304 continue;
306 if(name in var_opt_seen)
307 continue;
309 var_opt_seen[name]++;
310 otype = var_type_struct(flags[i]);
311 if (otype ~ "^((un)?signed +)?int *$")
312 var_opt_int[n_opt_int++] = name;
314 else if (otype ~ "^((un)?signed +)?short *$")
315 var_opt_short[n_opt_short++] = name;
317 else if (otype ~ "^((un)?signed +)?char *$") {
318 var_opt_char[n_opt_char++] = name;
319 if (otype ~ "^unsigned +char *$")
320 var_opt_range[name] = "0, 255"
321 else if (otype ~ "^signed +char *$")
322 var_opt_range[name] = "-128, 127"
324 else
325 var_opt_other[n_opt_other++] = name;
329 for (i = 0; i < n_opt_char; i++) {
330 name = var_opt_char[i];
331 if (var_opt_range[name] != "")
332 print " gcc_assert (IN_RANGE (" name ", " var_opt_range[name] "));";
335 print "";
336 for (i = 0; i < n_opt_other; i++) {
337 print " ptr->" var_opt_other[i] " = " var_opt_other[i] ";";
340 for (i = 0; i < n_opt_int; i++) {
341 print " ptr->" var_opt_int[i] " = " var_opt_int[i] ";";
344 for (i = 0; i < n_opt_short; i++) {
345 print " ptr->" var_opt_short[i] " = " var_opt_short[i] ";";
348 for (i = 0; i < n_opt_char; i++) {
349 print " ptr->" var_opt_char[i] " = " var_opt_char[i] ";";
352 print "}";
354 print "";
355 print "/* Restore optimization options from a structure. */";
356 print "void";
357 print "cl_optimization_restore (struct cl_optimization *ptr)";
358 print "{";
360 for (i = 0; i < n_opt_other; i++) {
361 print " " var_opt_other[i] " = ptr->" var_opt_other[i] ";";
364 for (i = 0; i < n_opt_int; i++) {
365 print " " var_opt_int[i] " = ptr->" var_opt_int[i] ";";
368 for (i = 0; i < n_opt_short; i++) {
369 print " " var_opt_short[i] " = ptr->" var_opt_short[i] ";";
372 for (i = 0; i < n_opt_char; i++) {
373 print " " var_opt_char[i] " = ptr->" var_opt_char[i] ";";
376 print " targetm.override_options_after_change ();";
377 print "}";
379 print "";
380 print "/* Print optimization options from a structure. */";
381 print "void";
382 print "cl_optimization_print (FILE *file,";
383 print " int indent_to,";
384 print " struct cl_optimization *ptr)";
385 print "{";
387 print " fputs (\"\\n\", file);";
388 for (i = 0; i < n_opt_other; i++) {
389 print " if (ptr->" var_opt_other[i] ")";
390 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
391 print " indent_to, \"\",";
392 print " \"" var_opt_other[i] "\",";
393 print " (unsigned long)ptr->" var_opt_other[i] ");";
394 print "";
397 for (i = 0; i < n_opt_int; i++) {
398 print " if (ptr->" var_opt_int[i] ")";
399 print " fprintf (file, \"%*s%s (%#x)\\n\",";
400 print " indent_to, \"\",";
401 print " \"" var_opt_int[i] "\",";
402 print " ptr->" var_opt_int[i] ");";
403 print "";
406 for (i = 0; i < n_opt_short; i++) {
407 print " if (ptr->" var_opt_short[i] ")";
408 print " fprintf (file, \"%*s%s (%#x)\\n\",";
409 print " indent_to, \"\",";
410 print " \"" var_opt_short[i] "\",";
411 print " ptr->" var_opt_short[i] ");";
412 print "";
415 for (i = 0; i < n_opt_char; i++) {
416 print " if (ptr->" var_opt_char[i] ")";
417 print " fprintf (file, \"%*s%s (%#x)\\n\",";
418 print " indent_to, \"\",";
419 print " \"" var_opt_char[i] "\",";
420 print " ptr->" var_opt_char[i] ");";
421 print "";
424 print "}";
426 print "";
427 print "/* Save selected option variables into a structure. */"
428 print "void";
429 print "cl_target_option_save (struct cl_target_option *ptr)";
430 print "{";
432 n_target_char = 0;
433 n_target_short = 0;
434 n_target_int = 0;
435 n_target_other = 0;
437 if (have_save) {
438 for (i = 0; i < n_opts; i++) {
439 if (flag_set_p("Save", flags[i])) {
440 name = var_name(flags[i])
441 if(name == "")
442 name = "target_flags";
444 if(name in var_save_seen)
445 continue;
447 var_save_seen[name]++;
448 otype = var_type_struct(flags[i])
449 if (otype ~ "^((un)?signed +)?int *$")
450 var_target_int[n_target_int++] = name;
452 else if (otype ~ "^((un)?signed +)?short *$")
453 var_target_short[n_target_short++] = name;
455 else if (otype ~ "^((un)?signed +)?char *$") {
456 var_target_char[n_target_char++] = name;
457 if (otype ~ "^unsigned +char *$")
458 var_target_range[name] = "0, 255"
459 else if (otype ~ "^signed +char *$")
460 var_target_range[name] = "-128, 127"
462 else
463 var_target_other[n_target_other++] = name;
466 } else {
467 var_target_int[n_target_int++] = "target_flags";
470 have_assert = 0;
471 for (i = 0; i < n_target_char; i++) {
472 name = var_target_char[i];
473 if (var_target_range[name] != "") {
474 have_assert = 1;
475 print " gcc_assert (IN_RANGE (" name ", " var_target_range[name] "));";
479 if (have_assert)
480 print "";
482 print " if (targetm.target_option.save)";
483 print " targetm.target_option.save (ptr);";
484 print "";
486 for (i = 0; i < n_target_other; i++) {
487 print " ptr->" var_target_other[i] " = " var_target_other[i] ";";
490 for (i = 0; i < n_target_int; i++) {
491 print " ptr->" var_target_int[i] " = " var_target_int[i] ";";
494 for (i = 0; i < n_target_short; i++) {
495 print " ptr->" var_target_short[i] " = " var_target_short[i] ";";
498 for (i = 0; i < n_target_char; i++) {
499 print " ptr->" var_target_char[i] " = " var_target_char[i] ";";
502 print "}";
504 print "";
505 print "/* Restore selected current options from a structure. */";
506 print "void";
507 print "cl_target_option_restore (struct cl_target_option *ptr)";
508 print "{";
510 for (i = 0; i < n_target_other; i++) {
511 print " " var_target_other[i] " = ptr->" var_target_other[i] ";";
514 for (i = 0; i < n_target_int; i++) {
515 print " " var_target_int[i] " = ptr->" var_target_int[i] ";";
518 for (i = 0; i < n_target_short; i++) {
519 print " " var_target_short[i] " = ptr->" var_target_short[i] ";";
522 for (i = 0; i < n_target_char; i++) {
523 print " " var_target_char[i] " = ptr->" var_target_char[i] ";";
526 # This must occur after the normal variables in case the code depends on those
527 # variables.
528 print "";
529 print " if (targetm.target_option.restore)";
530 print " targetm.target_option.restore (ptr);";
532 print "}";
534 print "";
535 print "/* Print optimization options from a structure. */";
536 print "void";
537 print "cl_target_option_print (FILE *file,";
538 print " int indent,";
539 print " struct cl_target_option *ptr)";
540 print "{";
542 print " fputs (\"\\n\", file);";
543 for (i = 0; i < n_target_other; i++) {
544 print " if (ptr->" var_target_other[i] ")";
545 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
546 print " indent, \"\",";
547 print " \"" var_target_other[i] "\",";
548 print " (unsigned long)ptr->" var_target_other[i] ");";
549 print "";
552 for (i = 0; i < n_target_int; i++) {
553 print " if (ptr->" var_target_int[i] ")";
554 print " fprintf (file, \"%*s%s (%#x)\\n\",";
555 print " indent, \"\",";
556 print " \"" var_target_int[i] "\",";
557 print " ptr->" var_target_int[i] ");";
558 print "";
561 for (i = 0; i < n_target_short; i++) {
562 print " if (ptr->" var_target_short[i] ")";
563 print " fprintf (file, \"%*s%s (%#x)\\n\",";
564 print " indent, \"\",";
565 print " \"" var_target_short[i] "\",";
566 print " ptr->" var_target_short[i] ");";
567 print "";
570 for (i = 0; i < n_target_char; i++) {
571 print " if (ptr->" var_target_char[i] ")";
572 print " fprintf (file, \"%*s%s (%#x)\\n\",";
573 print " indent, \"\",";
574 print " \"" var_target_char[i] "\",";
575 print " ptr->" var_target_char[i] ");";
576 print "";
579 print "";
580 print " if (targetm.target_option.print)";
581 print " targetm.target_option.print (file, indent, ptr);";
583 print "}";
584 print "#endif";