PR tree-optimization/43833
[official-gcc/alias-decl.git] / gcc / optc-gen.awk
blobda595e94bc0d324829d73885a872659b13bfb7ce
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 "#else"
77 print "#include " quote "flags.h" quote
78 print "#include " quote "target.h" quote
79 print "#endif /* GCC_DRIVER */"
80 print ""
82 have_save = 0;
83 for (i = 0; i < n_opts; i++) {
84 if (flag_set_p("Save", flags[i]))
85 have_save = 1;
87 name = var_name(flags[i]);
88 if (name == "")
89 continue;
91 if (flag_set_p("VarExists", flags[i])) {
92 # Need it for the gcc driver.
93 if (name in var_seen)
94 continue;
95 init = ""
96 gcc_driver = 1
98 else {
99 init = opt_args("Init", flags[i])
100 if (init != "")
101 init = " = " init;
102 else if (name in var_seen)
103 continue;
104 gcc_driver = 0
107 if (gcc_driver == 1)
108 print "#ifdef GCC_DRIVER"
109 print "/* Set by -" opts[i] "."
110 print " " help[i] " */"
111 print var_type(flags[i]) name init ";"
112 if (gcc_driver == 1)
113 print "#endif /* GCC_DRIVER */"
114 print ""
116 var_seen[name] = 1;
119 print ""
120 print "/* Local state variables. */"
121 for (i = 0; i < n_opts; i++) {
122 name = static_var(opts[i], flags[i]);
123 if (name != "")
124 print "static " var_type(flags[i]) name ";"
126 print ""
128 print "const char * const lang_names[] =\n{"
129 for (i = 0; i < n_langs; i++) {
130 macros[i] = "CL_" langs[i]
131 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
132 s = substr(" ", length (macros[i]))
133 print " " quote langs[i] quote ","
136 print " 0\n};\n"
137 print "const unsigned int cl_options_count = N_OPTS;\n"
138 print "const unsigned int cl_lang_count = " n_langs ";\n"
140 print "const struct cl_option cl_options[] =\n{"
142 j = 0
143 for (i = 0; i < n_opts; i++) {
144 back_chain[i] = "N_OPTS";
145 indices[opts[i]] = j;
146 # Combine the flags of identical switches. Switches
147 # appear many times if they are handled by many front
148 # ends, for example.
149 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
150 flags[i + 1] = flags[i] " " flags[i + 1];
151 if (help[i + 1] == "")
152 help[i + 1] = help[i]
153 else if (help[i] != "" && help[i + 1] != help[i])
154 print "warning: multiple different help strings for " \
155 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
156 | "cat 1>&2"
157 i++;
158 back_chain[i] = "N_OPTS";
159 indices[opts[i]] = j;
161 j++;
164 for (i = 0; i < n_opts; i++) {
165 # With identical flags, pick only the last one. The
166 # earlier loop ensured that it has all flags merged,
167 # and a nonempty help text if one of the texts was nonempty.
168 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
169 i++;
172 len = length (opts[i]);
173 enum = opt_enum(opts[i])
175 # If this switch takes joined arguments, back-chain all
176 # subsequent switches to it for which it is a prefix. If
177 # a later switch S is a longer prefix of a switch T, T
178 # will be back-chained to S in a later iteration of this
179 # for() loop, which is what we want.
180 if (flag_set_p("Joined.*", flags[i])) {
181 for (j = i + 1; j < n_opts; j++) {
182 if (substr (opts[j], 1, len) != opts[i])
183 break;
184 back_chain[j] = enum;
188 s = substr(" ", length (opts[i]))
189 if (i + 1 == n_opts)
190 comma = ""
192 if (help[i] == "")
193 hlp = "0"
194 else
195 hlp = quote help[i] quote;
197 neg = opt_args("Negative", flags[i]);
198 if (neg != "")
199 idx = indices[neg]
200 else {
201 if (flag_set_p("RejectNegative", flags[i]))
202 idx = -1;
203 else {
204 if (opts[i] ~ "^[Wfm]")
205 idx = indices[opts[i]];
206 else
207 idx = -1;
210 # Split the printf after %u to work around an ia64-hp-hpux11.23
211 # awk bug.
212 printf(" { %c-%s%c,\n %s,\n %s, %u,",
213 quote, opts[i], quote, hlp, back_chain[i], len)
214 printf(" %d,\n", idx)
215 condition = opt_args("Condition", flags[i])
216 cl_flags = switch_flags(flags[i])
217 if (condition != "")
218 printf("#if %s\n" \
219 " %s,\n" \
220 "#else\n" \
221 " CL_DISABLED,\n" \
222 "#endif\n",
223 condition, cl_flags, cl_flags)
224 else
225 printf(" %s,\n", cl_flags)
226 printf(" %s, %s }%s\n", var_ref(opts[i], flags[i]),
227 var_set(flags[i]), comma)
230 print "};"
232 print "";
233 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
234 print "";
235 print "/* Save optimization variables into a structure. */"
236 print "void";
237 print "cl_optimization_save (struct cl_optimization *ptr)";
238 print "{";
240 n_opt_char = 2;
241 n_opt_short = 0;
242 n_opt_int = 0;
243 n_opt_other = 0;
244 var_opt_char[0] = "optimize";
245 var_opt_char[1] = "optimize_size";
246 var_opt_range["optimize"] = "0, 255";
247 var_opt_range["optimize_size"] = "0, 255";
249 # Sort by size to mimic how the structure is laid out to be friendlier to the
250 # cache.
252 for (i = 0; i < n_opts; i++) {
253 if (flag_set_p("Optimization", flags[i])) {
254 name = var_name(flags[i])
255 if(name == "")
256 continue;
258 if(name in var_opt_seen)
259 continue;
261 var_opt_seen[name]++;
262 otype = var_type_struct(flags[i]);
263 if (otype ~ "^((un)?signed +)?int *$")
264 var_opt_int[n_opt_int++] = name;
266 else if (otype ~ "^((un)?signed +)?short *$")
267 var_opt_short[n_opt_short++] = name;
269 else if (otype ~ "^((un)?signed +)?char *$") {
270 var_opt_char[n_opt_char++] = name;
271 if (otype ~ "^unsigned +char *$")
272 var_opt_range[name] = "0, 255"
273 else if (otype ~ "^signed +char *$")
274 var_opt_range[name] = "-128, 127"
276 else
277 var_opt_other[n_opt_other++] = name;
281 for (i = 0; i < n_opt_char; i++) {
282 name = var_opt_char[i];
283 if (var_opt_range[name] != "")
284 print " gcc_assert (IN_RANGE (" name ", " var_opt_range[name] "));";
287 print "";
288 for (i = 0; i < n_opt_other; i++) {
289 print " ptr->" var_opt_other[i] " = " var_opt_other[i] ";";
292 for (i = 0; i < n_opt_int; i++) {
293 print " ptr->" var_opt_int[i] " = " var_opt_int[i] ";";
296 for (i = 0; i < n_opt_short; i++) {
297 print " ptr->" var_opt_short[i] " = " var_opt_short[i] ";";
300 for (i = 0; i < n_opt_char; i++) {
301 print " ptr->" var_opt_char[i] " = " var_opt_char[i] ";";
304 print "}";
306 print "";
307 print "/* Restore optimization options from a structure. */";
308 print "void";
309 print "cl_optimization_restore (struct cl_optimization *ptr)";
310 print "{";
312 for (i = 0; i < n_opt_other; i++) {
313 print " " var_opt_other[i] " = ptr->" var_opt_other[i] ";";
316 for (i = 0; i < n_opt_int; i++) {
317 print " " var_opt_int[i] " = ptr->" var_opt_int[i] ";";
320 for (i = 0; i < n_opt_short; i++) {
321 print " " var_opt_short[i] " = ptr->" var_opt_short[i] ";";
324 for (i = 0; i < n_opt_char; i++) {
325 print " " var_opt_char[i] " = ptr->" var_opt_char[i] ";";
328 print " targetm.override_options_after_change ();";
329 print "}";
331 print "";
332 print "/* Print optimization options from a structure. */";
333 print "void";
334 print "cl_optimization_print (FILE *file,";
335 print " int indent_to,";
336 print " struct cl_optimization *ptr)";
337 print "{";
339 print " fputs (\"\\n\", file);";
340 for (i = 0; i < n_opt_other; i++) {
341 print " if (ptr->" var_opt_other[i] ")";
342 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
343 print " indent_to, \"\",";
344 print " \"" var_opt_other[i] "\",";
345 print " (unsigned long)ptr->" var_opt_other[i] ");";
346 print "";
349 for (i = 0; i < n_opt_int; i++) {
350 print " if (ptr->" var_opt_int[i] ")";
351 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
352 print " indent_to, \"\",";
353 print " \"" var_opt_int[i] "\",";
354 print " ptr->" var_opt_int[i] ");";
355 print "";
358 for (i = 0; i < n_opt_short; i++) {
359 print " if (ptr->" var_opt_short[i] ")";
360 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
361 print " indent_to, \"\",";
362 print " \"" var_opt_short[i] "\",";
363 print " ptr->" var_opt_short[i] ");";
364 print "";
367 for (i = 0; i < n_opt_char; i++) {
368 print " if (ptr->" var_opt_char[i] ")";
369 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
370 print " indent_to, \"\",";
371 print " \"" var_opt_char[i] "\",";
372 print " ptr->" var_opt_char[i] ");";
373 print "";
376 print "}";
378 print "";
379 print "/* Save selected option variables into a structure. */"
380 print "void";
381 print "cl_target_option_save (struct cl_target_option *ptr)";
382 print "{";
384 n_target_char = 0;
385 n_target_short = 0;
386 n_target_int = 0;
387 n_target_other = 0;
389 if (have_save) {
390 for (i = 0; i < n_opts; i++) {
391 if (flag_set_p("Save", flags[i])) {
392 name = var_name(flags[i])
393 if(name == "")
394 name = "target_flags";
396 if(name in var_save_seen)
397 continue;
399 var_save_seen[name]++;
400 otype = var_type_struct(flags[i])
401 if (otype ~ "^((un)?signed +)?int *$")
402 var_target_int[n_target_int++] = name;
404 else if (otype ~ "^((un)?signed +)?short *$")
405 var_target_short[n_target_short++] = name;
407 else if (otype ~ "^((un)?signed +)?char *$") {
408 var_target_char[n_target_char++] = name;
409 if (otype ~ "^unsigned +char *$")
410 var_target_range[name] = "0, 255"
411 else if (otype ~ "^signed +char *$")
412 var_target_range[name] = "-128, 127"
414 else
415 var_target_other[n_target_other++] = name;
418 } else {
419 var_target_int[n_target_int++] = "target_flags";
422 have_assert = 0;
423 for (i = 0; i < n_target_char; i++) {
424 name = var_target_char[i];
425 if (var_target_range[name] != "") {
426 have_assert = 1;
427 print " gcc_assert (IN_RANGE (" name ", " var_target_range[name] "));";
431 if (have_assert)
432 print "";
434 print " if (targetm.target_option.save)";
435 print " targetm.target_option.save (ptr);";
436 print "";
438 for (i = 0; i < n_target_other; i++) {
439 print " ptr->" var_target_other[i] " = " var_target_other[i] ";";
442 for (i = 0; i < n_target_int; i++) {
443 print " ptr->" var_target_int[i] " = " var_target_int[i] ";";
446 for (i = 0; i < n_target_short; i++) {
447 print " ptr->" var_target_short[i] " = " var_target_short[i] ";";
450 for (i = 0; i < n_target_char; i++) {
451 print " ptr->" var_target_char[i] " = " var_target_char[i] ";";
454 print "}";
456 print "";
457 print "/* Restore selected current options from a structure. */";
458 print "void";
459 print "cl_target_option_restore (struct cl_target_option *ptr)";
460 print "{";
462 for (i = 0; i < n_target_other; i++) {
463 print " " var_target_other[i] " = ptr->" var_target_other[i] ";";
466 for (i = 0; i < n_target_int; i++) {
467 print " " var_target_int[i] " = ptr->" var_target_int[i] ";";
470 for (i = 0; i < n_target_short; i++) {
471 print " " var_target_short[i] " = ptr->" var_target_short[i] ";";
474 for (i = 0; i < n_target_char; i++) {
475 print " " var_target_char[i] " = ptr->" var_target_char[i] ";";
478 # This must occur after the normal variables in case the code depends on those
479 # variables.
480 print "";
481 print " if (targetm.target_option.restore)";
482 print " targetm.target_option.restore (ptr);";
484 print "}";
486 print "";
487 print "/* Print optimization options from a structure. */";
488 print "void";
489 print "cl_target_option_print (FILE *file,";
490 print " int indent,";
491 print " struct cl_target_option *ptr)";
492 print "{";
494 print " fputs (\"\\n\", file);";
495 for (i = 0; i < n_target_other; i++) {
496 print " if (ptr->" var_target_other[i] ")";
497 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
498 print " indent, \"\",";
499 print " \"" var_target_other[i] "\",";
500 print " (unsigned long)ptr->" var_target_other[i] ");";
501 print "";
504 for (i = 0; i < n_target_int; i++) {
505 print " if (ptr->" var_target_int[i] ")";
506 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
507 print " indent, \"\",";
508 print " \"" var_target_int[i] "\",";
509 print " ptr->" var_target_int[i] ");";
510 print "";
513 for (i = 0; i < n_target_short; i++) {
514 print " if (ptr->" var_target_short[i] ")";
515 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
516 print " indent, \"\",";
517 print " \"" var_target_short[i] "\",";
518 print " ptr->" var_target_short[i] ");";
519 print "";
522 for (i = 0; i < n_target_char; i++) {
523 print " if (ptr->" var_target_char[i] ")";
524 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
525 print " indent, \"\",";
526 print " \"" var_target_char[i] "\",";
527 print " ptr->" var_target_char[i] ");";
528 print "";
531 print "";
532 print " if (targetm.target_option.print)";
533 print " targetm.target_option.print (file, indent, ptr);";
535 print "}";
536 print "#endif";