re PR target/35659 (Miscompiled code with -O2 (but not with -O2 -funroll-loops) on...
[official-gcc.git] / gcc / optc-gen.awk
blobd53ba69aa76c11ff991b92a3d2db7fdc47b60062
1 # Copyright (C) 2003, 2004, 2007 Free Software Foundation, Inc.
2 # Contributed by Kelley Cook, June 2004.
3 # Original code from Neil Booth, May 2003.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; see the file COPYING3. If not see
17 # <http://www.gnu.org/licenses/>.
19 # This Awk script reads in the option records generated from
20 # opt-gather.awk, combines the flags of duplicate options and generates a
21 # C file.
23 # This program uses functions from opt-functions.awk
25 # Usage: awk -f opt-functions.awk -f optc-gen.awk \
26 # [-v header_name=header.h] < inputfile > options.c
28 BEGIN {
29 n_opts = 0
30 n_langs = 0
31 n_target_save = 0
32 quote = "\042"
33 comma = ","
34 FS=SUBSEP
35 # Default the name of header created from opth-gen.awk to options.h
36 if (header_name == "") header_name="options.h"
39 # Collect the text and flags of each option into an array
41 if ($1 == "Language") {
42 langs[n_langs] = $2
43 n_langs++;
45 else if ($1 == "TargetSave") {
46 # Make sure the declarations are put in source order
47 target_save_decl[n_target_save] = $2
48 n_target_save++
50 else {
51 name = opt_args("Mask", $1)
52 if (name == "") {
53 opts[n_opts] = $1
54 flags[n_opts] = $2
55 help[n_opts] = $3
56 n_opts++;
61 # Dump that array of options into a C file.
62 END {
63 print "/* This file is auto-generated by optc-gen.awk. */"
64 print ""
65 n_headers = split(header_name, headers, " ")
66 for (i = 1; i <= n_headers; i++)
67 print "#include " quote headers[i] quote
68 print "#include " quote "opts.h" quote
69 print "#include " quote "intl.h" quote
70 print ""
71 print "#ifdef GCC_DRIVER"
72 print "int target_flags;"
73 print "#else"
74 print "#include " quote "flags.h" quote
75 print "#include " quote "target.h" quote
76 print "#endif /* GCC_DRIVER */"
77 print ""
79 have_save = 0;
80 for (i = 0; i < n_opts; i++) {
81 if (flag_set_p("Save", flags[i]))
82 have_save = 1;
84 name = var_name(flags[i]);
85 if (name == "")
86 continue;
88 if (flag_set_p("VarExists", flags[i])) {
89 # Need it for the gcc driver.
90 if (name in var_seen)
91 continue;
92 init = ""
93 gcc_driver = 1
95 else {
96 init = opt_args("Init", flags[i])
97 if (init != "")
98 init = " = " init;
99 else if (name in var_seen)
100 continue;
101 gcc_driver = 0
104 if (gcc_driver == 1)
105 print "#ifdef GCC_DRIVER"
106 print "/* Set by -" opts[i] "."
107 print " " help[i] " */"
108 print var_type(flags[i]) name init ";"
109 if (gcc_driver == 1)
110 print "#endif /* GCC_DRIVER */"
111 print ""
113 var_seen[name] = 1;
116 print ""
117 print "/* Local state variables. */"
118 for (i = 0; i < n_opts; i++) {
119 name = static_var(opts[i], flags[i]);
120 if (name != "")
121 print "static " var_type(flags[i]) name ";"
123 print ""
125 print "const char * const lang_names[] =\n{"
126 for (i = 0; i < n_langs; i++) {
127 macros[i] = "CL_" langs[i]
128 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
129 s = substr(" ", length (macros[i]))
130 print " " quote langs[i] quote ","
133 print " 0\n};\n"
134 print "const unsigned int cl_options_count = N_OPTS;\n"
135 print "const unsigned int cl_lang_count = " n_langs ";\n"
137 print "const struct cl_option cl_options[] =\n{"
139 j = 0
140 for (i = 0; i < n_opts; i++) {
141 back_chain[i] = "N_OPTS";
142 indices[opts[i]] = j;
143 # Combine the flags of identical switches. Switches
144 # appear many times if they are handled by many front
145 # ends, for example.
146 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
147 flags[i + 1] = flags[i] " " flags[i + 1];
148 i++;
149 back_chain[i] = "N_OPTS";
150 indices[opts[i]] = j;
152 j++;
155 for (i = 0; i < n_opts; i++) {
156 # Combine the flags of identical switches. Switches
157 # appear many times if they are handled by many front
158 # ends, for example.
159 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
160 flags[i + 1] = flags[i] " " flags[i + 1];
161 i++;
164 len = length (opts[i]);
165 enum = "OPT_" opts[i]
166 if (opts[i] == "finline-limit=" || opts[i] == "Wlarger-than=")
167 enum = enum "eq"
168 gsub ("[^A-Za-z0-9]", "_", enum)
170 # If this switch takes joined arguments, back-chain all
171 # subsequent switches to it for which it is a prefix. If
172 # a later switch S is a longer prefix of a switch T, T
173 # will be back-chained to S in a later iteration of this
174 # for() loop, which is what we want.
175 if (flag_set_p("Joined.*", flags[i])) {
176 for (j = i + 1; j < n_opts; j++) {
177 if (substr (opts[j], 1, len) != opts[i])
178 break;
179 back_chain[j] = enum;
183 s = substr(" ", length (opts[i]))
184 if (i + 1 == n_opts)
185 comma = ""
187 if (help[i] == "")
188 hlp = "0"
189 else
190 hlp = quote help[i] quote;
192 neg = opt_args("Negative", flags[i]);
193 if (neg != "")
194 idx = indices[neg]
195 else {
196 if (flag_set_p("RejectNegative", flags[i]))
197 idx = -1;
198 else {
199 if (opts[i] ~ "^[Wfm]")
200 idx = indices[opts[i]];
201 else
202 idx = -1;
205 # Split the printf after %u to work around an ia64-hp-hpux11.23
206 # awk bug.
207 printf(" { %c-%s%c,\n %s,\n %s, %u,",
208 quote, opts[i], quote, hlp, back_chain[i], len)
209 printf(" %d,\n", idx)
210 condition = opt_args("Condition", flags[i])
211 cl_flags = switch_flags(flags[i])
212 if (condition != "")
213 printf("#if %s\n" \
214 " %s,\n" \
215 "#else\n" \
216 " CL_DISABLED,\n" \
217 "#endif\n",
218 condition, cl_flags, cl_flags)
219 else
220 printf(" %s,\n", cl_flags)
221 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
222 var_set(flags[i]), comma)
225 print "};"
227 print "";
228 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
229 print "";
230 print "/* Save optimization variables into a structure. */"
231 print "void";
232 print "cl_optimization_save (struct cl_optimization *ptr)";
233 print "{";
235 n_opt_char = 2;
236 n_opt_short = 0;
237 n_opt_int = 0;
238 n_opt_other = 0;
239 var_opt_char[0] = "optimize";
240 var_opt_char[1] = "optimize_size";
241 var_opt_range["optimize"] = "0, 255";
242 var_opt_range["optimize_size"] = "0, 255";
244 # Sort by size to mimic how the structure is laid out to be friendlier to the
245 # cache.
247 for (i = 0; i < n_opts; i++) {
248 if (flag_set_p("Optimization", flags[i])) {
249 name = var_name(flags[i])
250 if(name == "")
251 continue;
253 if(name in var_opt_seen)
254 continue;
256 var_opt_seen[name]++;
257 otype = var_type_struct(flags[i]);
258 if (otype ~ "^((un)?signed +)?int *$")
259 var_opt_int[n_opt_int++] = name;
261 else if (otype ~ "^((un)?signed +)?short *$")
262 var_opt_short[n_opt_short++] = name;
264 else if (otype ~ "^((un)?signed +)?char *$") {
265 var_opt_char[n_opt_char++] = name;
266 if (otype ~ "^unsigned +char *$")
267 var_opt_range[name] = "0, 255"
268 else if (otype ~ "^signed +char *$")
269 var_opt_range[name] = "-128, 127"
271 else
272 var_opt_other[n_opt_other++] = name;
276 for (i = 0; i < n_opt_char; i++) {
277 name = var_opt_char[i];
278 if (var_opt_range[name] != "")
279 print " gcc_assert (IN_RANGE (" name ", " var_opt_range[name] "));";
282 print "";
283 for (i = 0; i < n_opt_other; i++) {
284 print " ptr->" var_opt_other[i] " = " var_opt_other[i] ";";
287 for (i = 0; i < n_opt_int; i++) {
288 print " ptr->" var_opt_int[i] " = " var_opt_int[i] ";";
291 for (i = 0; i < n_opt_short; i++) {
292 print " ptr->" var_opt_short[i] " = " var_opt_short[i] ";";
295 for (i = 0; i < n_opt_char; i++) {
296 print " ptr->" var_opt_char[i] " = " var_opt_char[i] ";";
299 print "}";
301 print "";
302 print "/* Restore optimization options from a structure. */";
303 print "void";
304 print "cl_optimization_restore (struct cl_optimization *ptr)";
305 print "{";
307 for (i = 0; i < n_opt_other; i++) {
308 print " " var_opt_other[i] " = ptr->" var_opt_other[i] ";";
311 for (i = 0; i < n_opt_int; i++) {
312 print " " var_opt_int[i] " = ptr->" var_opt_int[i] ";";
315 for (i = 0; i < n_opt_short; i++) {
316 print " " var_opt_short[i] " = ptr->" var_opt_short[i] ";";
319 for (i = 0; i < n_opt_char; i++) {
320 print " " var_opt_char[i] " = ptr->" var_opt_char[i] ";";
323 print "}";
325 print "";
326 print "/* Print optimization options from a structure. */";
327 print "void";
328 print "cl_optimization_print (FILE *file,";
329 print " int indent_to,";
330 print " struct cl_optimization *ptr)";
331 print "{";
333 print " fputs (\"\\n\", file);";
334 for (i = 0; i < n_opt_other; i++) {
335 print " if (ptr->" var_opt_other[i] ")";
336 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
337 print " indent_to, \"\",";
338 print " \"" var_opt_other[i] "\",";
339 print " (unsigned long)ptr->" var_opt_other[i] ");";
340 print "";
343 for (i = 0; i < n_opt_int; i++) {
344 print " if (ptr->" var_opt_int[i] ")";
345 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
346 print " indent_to, \"\",";
347 print " \"" var_opt_int[i] "\",";
348 print " ptr->" var_opt_int[i] ");";
349 print "";
352 for (i = 0; i < n_opt_short; i++) {
353 print " if (ptr->" var_opt_short[i] ")";
354 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
355 print " indent_to, \"\",";
356 print " \"" var_opt_short[i] "\",";
357 print " ptr->" var_opt_short[i] ");";
358 print "";
361 for (i = 0; i < n_opt_char; i++) {
362 print " if (ptr->" var_opt_char[i] ")";
363 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
364 print " indent_to, \"\",";
365 print " \"" var_opt_char[i] "\",";
366 print " ptr->" var_opt_char[i] ");";
367 print "";
370 print "}";
372 print "";
373 print "/* Save selected option variables into a structure. */"
374 print "void";
375 print "cl_target_option_save (struct cl_target_option *ptr)";
376 print "{";
378 n_target_char = 0;
379 n_target_short = 0;
380 n_target_int = 0;
381 n_target_other = 0;
383 if (have_save) {
384 for (i = 0; i < n_opts; i++) {
385 if (flag_set_p("Save", flags[i])) {
386 name = var_name(flags[i])
387 if(name == "")
388 name = "target_flags";
390 if(name in var_save_seen)
391 continue;
393 var_save_seen[name]++;
394 otype = var_type_struct(flags[i])
395 if (otype ~ "^((un)?signed +)?int *$")
396 var_target_int[n_target_int++] = name;
398 else if (otype ~ "^((un)?signed +)?short *$")
399 var_target_short[n_target_short++] = name;
401 else if (otype ~ "^((un)?signed +)?char *$") {
402 var_target_char[n_target_char++] = name;
403 if (otype ~ "^unsigned +char *$")
404 var_target_range[name] = "0, 255"
405 else if (otype ~ "^signed +char *$")
406 var_target_range[name] = "-128, 127"
408 else
409 var_target_other[n_target_other++] = name;
412 } else {
413 var_target_int[n_target_int++] = "target_flags";
416 have_assert = 0;
417 for (i = 0; i < n_target_char; i++) {
418 name = var_target_char[i];
419 if (var_target_range[name] != "") {
420 have_assert = 1;
421 print " gcc_assert (IN_RANGE (" name ", " var_target_range[name] "));";
425 if (have_assert)
426 print "";
428 print " if (targetm.target_option.save)";
429 print " targetm.target_option.save (ptr);";
430 print "";
432 for (i = 0; i < n_target_other; i++) {
433 print " ptr->" var_target_other[i] " = " var_target_other[i] ";";
436 for (i = 0; i < n_target_int; i++) {
437 print " ptr->" var_target_int[i] " = " var_target_int[i] ";";
440 for (i = 0; i < n_target_short; i++) {
441 print " ptr->" var_target_short[i] " = " var_target_short[i] ";";
444 for (i = 0; i < n_target_char; i++) {
445 print " ptr->" var_target_char[i] " = " var_target_char[i] ";";
448 print "}";
450 print "";
451 print "/* Restore selected current options from a structure. */";
452 print "void";
453 print "cl_target_option_restore (struct cl_target_option *ptr)";
454 print "{";
456 for (i = 0; i < n_target_other; i++) {
457 print " " var_target_other[i] " = ptr->" var_target_other[i] ";";
460 for (i = 0; i < n_target_int; i++) {
461 print " " var_target_int[i] " = ptr->" var_target_int[i] ";";
464 for (i = 0; i < n_target_short; i++) {
465 print " " var_target_short[i] " = ptr->" var_target_short[i] ";";
468 for (i = 0; i < n_target_char; i++) {
469 print " " var_target_char[i] " = ptr->" var_target_char[i] ";";
472 # This must occur after the normal variables in case the code depends on those
473 # variables.
474 print "";
475 print " if (targetm.target_option.restore)";
476 print " targetm.target_option.restore (ptr);";
478 print "}";
480 print "";
481 print "/* Print optimization options from a structure. */";
482 print "void";
483 print "cl_target_option_print (FILE *file,";
484 print " int indent,";
485 print " struct cl_target_option *ptr)";
486 print "{";
488 print " fputs (\"\\n\", file);";
489 for (i = 0; i < n_target_other; i++) {
490 print " if (ptr->" var_target_other[i] ")";
491 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
492 print " indent, \"\",";
493 print " \"" var_target_other[i] "\",";
494 print " (unsigned long)ptr->" var_target_other[i] ");";
495 print "";
498 for (i = 0; i < n_target_int; i++) {
499 print " if (ptr->" var_target_int[i] ")";
500 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
501 print " indent, \"\",";
502 print " \"" var_target_int[i] "\",";
503 print " ptr->" var_target_int[i] ");";
504 print "";
507 for (i = 0; i < n_target_short; i++) {
508 print " if (ptr->" var_target_short[i] ")";
509 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
510 print " indent, \"\",";
511 print " \"" var_target_short[i] "\",";
512 print " ptr->" var_target_short[i] ");";
513 print "";
516 for (i = 0; i < n_target_char; i++) {
517 print " if (ptr->" var_target_char[i] ")";
518 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
519 print " indent, \"\",";
520 print " \"" var_target_char[i] "\",";
521 print " ptr->" var_target_char[i] ");";
522 print "";
525 print "";
526 print " if (targetm.target_option.print)";
527 print " targetm.target_option.print (file, indent, ptr);";
529 print "}";
530 print "#endif";