OSDN Git Service

gcc/fortran/
[pf3gnuchains/gcc-fork.git] / gcc / optc-gen.awk
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.
5 #
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.
10
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.
15
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/>.
19
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.
23 #
24 # This program uses functions from opt-functions.awk
25 #
26 # Usage: awk -f opt-functions.awk -f optc-gen.awk \
27 #            [-v header_name=header.h] < inputfile > options.c
28
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"
39 }
40
41 # Collect the text and flags of each option into an array
42         {
43                 if ($1 == "Language") {
44                         langs[n_langs] = $2
45                         n_langs++;
46                 }
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++
51                 }
52                 else if ($1 == "Variable") {
53                         extra_vars[n_extra_vars] = $2
54                         n_extra_vars++
55                 }
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++;
65                         }
66                 }
67         }
68
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 "#ifdef GCC_DRIVER"
80 print "int target_flags_explicit;"
81 print "#else"
82 print "#include " quote "flags.h" quote
83 print "#include " quote "target.h" quote
84 print "#endif /* GCC_DRIVER */"
85 print ""
86
87 have_save = 0;
88 for (i = 0; i < n_extra_vars; i++) {
89         print extra_vars[i] ";"
90 }
91 for (i = 0; i < n_opts; i++) {
92         if (flag_set_p("Save", flags[i]))
93                 have_save = 1;
94
95         name = var_name(flags[i]);
96         if (name == "")
97                 continue;
98
99         if (flag_set_p("VarExists", flags[i])) {
100                 continue;
101         }
102         else {
103                 init = opt_args("Init", flags[i])
104                 if (init != "")
105                         init = " = " init;
106                 else if (name in var_seen)
107                         continue;
108         }
109
110         print "/* Set by -" opts[i] "."
111         print "   " help[i] "  */"
112         print var_type(flags[i]) name init ";"
113         print ""
114
115         var_seen[name] = 1;
116 }
117
118 print ""
119 print "/* Local state variables.  */"
120 for (i = 0; i < n_opts; i++) {
121         name = static_var(opts[i], flags[i]);
122         if (name != "")
123                 print "static " var_type(flags[i]) name ";"
124 }
125 print ""
126
127 print "const char * const lang_names[] =\n{"
128 for (i = 0; i < n_langs; i++) {
129         macros[i] = "CL_" langs[i]
130         gsub( "[^A-Za-z0-9_]", "X", macros[i] )
131         s = substr("         ", length (macros[i]))
132         print "  " quote langs[i] quote ","
133     }
134
135 print "  0\n};\n"
136 print "const unsigned int cl_options_count = N_OPTS;\n"
137 print "const unsigned int cl_lang_count = " n_langs ";\n"
138
139 print "const struct cl_option cl_options[] =\n{"
140
141 j = 0
142 for (i = 0; i < n_opts; i++) {
143         back_chain[i] = "N_OPTS";
144         indices[opts[i]] = j;
145         # Combine the flags of identical switches.  Switches
146         # appear many times if they are handled by many front
147         # ends, for example.
148         while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
149                 flags[i + 1] = flags[i] " " flags[i + 1];
150                 if (help[i + 1] == "")
151                         help[i + 1] = help[i]
152                 else if (help[i] != "" && help[i + 1] != help[i])
153                         print "warning: multiple different help strings for " \
154                                 opts[i] ":\n\t" help[i] "\n\t" help[i + 1] \
155                                 | "cat 1>&2"
156                 i++;
157                 back_chain[i] = "N_OPTS";
158                 indices[opts[i]] = j;
159         }
160         j++;
161 }
162
163 for (i = 0; i < n_opts; i++) {
164         # With identical flags, pick only the last one.  The
165         # earlier loop ensured that it has all flags merged,
166         # and a nonempty help text if one of the texts was nonempty.
167         while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
168                 i++;
169         }
170
171         len = length (opts[i]);
172         enum = opt_enum(opts[i])
173
174         # If this switch takes joined arguments, back-chain all
175         # subsequent switches to it for which it is a prefix.  If
176         # a later switch S is a longer prefix of a switch T, T
177         # will be back-chained to S in a later iteration of this
178         # for() loop, which is what we want.
179         if (flag_set_p("Joined.*", flags[i])) {
180                 for (j = i + 1; j < n_opts; j++) {
181                         if (substr (opts[j], 1, len) != opts[i])
182                                 break;
183                         back_chain[j] = enum;
184                 }
185         }
186
187         s = substr("                                  ", length (opts[i]))
188         if (i + 1 == n_opts)
189                 comma = ""
190
191         if (help[i] == "")
192                 hlp = "0"
193         else
194                 hlp = quote help[i] quote;
195
196         missing_arg_error = opt_args("MissingArgError", flags[i])
197         if (missing_arg_error == "")
198                 missing_arg_error = "0"
199         else
200                 missing_arg_error = quote missing_arg_error quote
201
202
203         warn_message = opt_args("Warn", flags[i])
204         if (warn_message == "")
205                 warn_message = "0"
206         else
207                 warn_message = quote warn_message quote
208
209         alias_arg = opt_args("Alias", flags[i])
210         if (alias_arg == "") {
211                 if (flag_set_p("Ignore", flags[i]))
212                         alias_data = "NULL, NULL, OPT_SPECIAL_ignore"
213                 else
214                         alias_data = "NULL, NULL, N_OPTS"
215         } else {
216                 alias_opt = nth_arg(0, alias_arg)
217                 alias_posarg = nth_arg(1, alias_arg)
218                 alias_negarg = nth_arg(2, alias_arg)
219
220                 if (var_ref(opts[i], flags[i]) != "0")
221                         print "#error Alias setting variable"
222
223                 if (alias_posarg != "" && alias_negarg == "") {
224                         if (!flag_set_p("RejectNegative", flags[i]) \
225                             && opts[i] ~ "^[Wfm]")
226                                 print "#error Alias with single argument " \
227                                         "allowing negative form"
228                 }
229
230                 alias_opt = opt_enum(alias_opt)
231                 if (alias_posarg == "")
232                         alias_posarg = "NULL"
233                 else
234                         alias_posarg = quote alias_posarg quote
235                 if (alias_negarg == "")
236                         alias_negarg = "NULL"
237                 else
238                         alias_negarg = quote alias_negarg quote
239                 alias_data = alias_posarg ", " alias_negarg ", " alias_opt
240         }
241
242         neg = opt_args("Negative", flags[i]);
243         if (neg != "")
244                 idx = indices[neg]
245         else {
246                 if (flag_set_p("RejectNegative", flags[i]))
247                         idx = -1;
248                 else {
249                         if (opts[i] ~ "^[Wfm]")
250                                 idx = indices[opts[i]];
251                         else
252                                 idx = -1;
253                 }
254         }
255         # Split the printf after %u to work around an ia64-hp-hpux11.23
256         # awk bug.
257         printf("  { %c-%s%c,\n    %s,\n    %s,\n    %s,\n    %s, %s, %u,",
258                quote, opts[i], quote, hlp, missing_arg_error, warn_message,
259                alias_data, back_chain[i], len)
260         printf(" %d,\n", idx)
261         condition = opt_args("Condition", flags[i])
262         cl_flags = switch_flags(flags[i])
263         if (condition != "")
264                 printf("#if %s\n" \
265                        "    %s,\n" \
266                        "#else\n" \
267                        "    CL_DISABLED,\n" \
268                        "#endif\n",
269                        condition, cl_flags, cl_flags)
270         else
271                 printf("    %s,\n", cl_flags)
272         printf("    %s, %s }%s\n", var_ref(opts[i], flags[i]),
273                var_set(flags[i]), comma)
274 }
275
276 print "};"
277
278 print "";
279 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
280 print "";
281 print "/* Save optimization variables into a structure.  */"
282 print "void";
283 print "cl_optimization_save (struct cl_optimization *ptr)";
284 print "{";
285
286 n_opt_char = 2;
287 n_opt_short = 0;
288 n_opt_int = 0;
289 n_opt_other = 0;
290 var_opt_char[0] = "optimize";
291 var_opt_char[1] = "optimize_size";
292 var_opt_range["optimize"] = "0, 255";
293 var_opt_range["optimize_size"] = "0, 255";
294
295 # Sort by size to mimic how the structure is laid out to be friendlier to the
296 # cache.
297
298 for (i = 0; i < n_opts; i++) {
299         if (flag_set_p("Optimization", flags[i])) {
300                 name = var_name(flags[i])
301                 if(name == "")
302                         continue;
303
304                 if(name in var_opt_seen)
305                         continue;
306
307                 var_opt_seen[name]++;
308                 otype = var_type_struct(flags[i]);
309                 if (otype ~ "^((un)?signed +)?int *$")
310                         var_opt_int[n_opt_int++] = name;
311
312                 else if (otype ~ "^((un)?signed +)?short *$")
313                         var_opt_short[n_opt_short++] = name;
314
315                 else if (otype ~ "^((un)?signed +)?char *$") {
316                         var_opt_char[n_opt_char++] = name;
317                         if (otype ~ "^unsigned +char *$")
318                                 var_opt_range[name] = "0, 255"
319                         else if (otype ~ "^signed +char *$")
320                                 var_opt_range[name] = "-128, 127"
321                 }
322                 else
323                         var_opt_other[n_opt_other++] = name;
324         }
325 }
326
327 for (i = 0; i < n_opt_char; i++) {
328         name = var_opt_char[i];
329         if (var_opt_range[name] != "")
330                 print "  gcc_assert (IN_RANGE (" name ", " var_opt_range[name] "));";
331 }
332
333 print "";
334 for (i = 0; i < n_opt_other; i++) {
335         print "  ptr->" var_opt_other[i] " = " var_opt_other[i] ";";
336 }
337
338 for (i = 0; i < n_opt_int; i++) {
339         print "  ptr->" var_opt_int[i] " = " var_opt_int[i] ";";
340 }
341
342 for (i = 0; i < n_opt_short; i++) {
343         print "  ptr->" var_opt_short[i] " = " var_opt_short[i] ";";
344 }
345
346 for (i = 0; i < n_opt_char; i++) {
347         print "  ptr->" var_opt_char[i] " = " var_opt_char[i] ";";
348 }
349
350 print "}";
351
352 print "";
353 print "/* Restore optimization options from a structure.  */";
354 print "void";
355 print "cl_optimization_restore (struct cl_optimization *ptr)";
356 print "{";
357
358 for (i = 0; i < n_opt_other; i++) {
359         print "  " var_opt_other[i] " = ptr->" var_opt_other[i] ";";
360 }
361
362 for (i = 0; i < n_opt_int; i++) {
363         print "  " var_opt_int[i] " = ptr->" var_opt_int[i] ";";
364 }
365
366 for (i = 0; i < n_opt_short; i++) {
367         print "  " var_opt_short[i] " = ptr->" var_opt_short[i] ";";
368 }
369
370 for (i = 0; i < n_opt_char; i++) {
371         print "  " var_opt_char[i] " = ptr->" var_opt_char[i] ";";
372 }
373
374 print "  targetm.override_options_after_change ();";
375 print "}";
376
377 print "";
378 print "/* Print optimization options from a structure.  */";
379 print "void";
380 print "cl_optimization_print (FILE *file,";
381 print "                       int indent_to,";
382 print "                       struct cl_optimization *ptr)";
383 print "{";
384
385 print "  fputs (\"\\n\", file);";
386 for (i = 0; i < n_opt_other; i++) {
387         print "  if (ptr->" var_opt_other[i] ")";
388         print "    fprintf (file, \"%*s%s (%#lx)\\n\",";
389         print "             indent_to, \"\",";
390         print "             \"" var_opt_other[i] "\",";
391         print "             (unsigned long)ptr->" var_opt_other[i] ");";
392         print "";
393 }
394
395 for (i = 0; i < n_opt_int; i++) {
396         print "  if (ptr->" var_opt_int[i] ")";
397         print "    fprintf (file, \"%*s%s (%#x)\\n\",";
398         print "             indent_to, \"\",";
399         print "             \"" var_opt_int[i] "\",";
400         print "             ptr->" var_opt_int[i] ");";
401         print "";
402 }
403
404 for (i = 0; i < n_opt_short; i++) {
405         print "  if (ptr->" var_opt_short[i] ")";
406         print "    fprintf (file, \"%*s%s (%#x)\\n\",";
407         print "             indent_to, \"\",";
408         print "             \"" var_opt_short[i] "\",";
409         print "             ptr->" var_opt_short[i] ");";
410         print "";
411 }
412
413 for (i = 0; i < n_opt_char; i++) {
414         print "  if (ptr->" var_opt_char[i] ")";
415         print "    fprintf (file, \"%*s%s (%#x)\\n\",";
416         print "             indent_to, \"\",";
417         print "             \"" var_opt_char[i] "\",";
418         print "             ptr->" var_opt_char[i] ");";
419         print "";
420 }
421
422 print "}";
423
424 print "";
425 print "/* Save selected option variables into a structure.  */"
426 print "void";
427 print "cl_target_option_save (struct cl_target_option *ptr)";
428 print "{";
429
430 n_target_char = 0;
431 n_target_short = 0;
432 n_target_int = 0;
433 n_target_other = 0;
434
435 if (have_save) {
436         for (i = 0; i < n_opts; i++) {
437                 if (flag_set_p("Save", flags[i])) {
438                         name = var_name(flags[i])
439                         if(name == "")
440                                 name = "target_flags";
441
442                         if(name in var_save_seen)
443                                 continue;
444
445                         var_save_seen[name]++;
446                         otype = var_type_struct(flags[i])
447                         if (otype ~ "^((un)?signed +)?int *$")
448                                 var_target_int[n_target_int++] = name;
449
450                         else if (otype ~ "^((un)?signed +)?short *$")
451                                 var_target_short[n_target_short++] = name;
452
453                         else if (otype ~ "^((un)?signed +)?char *$") {
454                                 var_target_char[n_target_char++] = name;
455                                 if (otype ~ "^unsigned +char *$")
456                                         var_target_range[name] = "0, 255"
457                                 else if (otype ~ "^signed +char *$")
458                                         var_target_range[name] = "-128, 127"
459                         }
460                         else
461                                 var_target_other[n_target_other++] = name;
462                 }
463         }
464 } else {
465         var_target_int[n_target_int++] = "target_flags";
466 }
467
468 have_assert = 0;
469 for (i = 0; i < n_target_char; i++) {
470         name = var_target_char[i];
471         if (var_target_range[name] != "") {
472                 have_assert = 1;
473                 print "  gcc_assert (IN_RANGE (" name ", " var_target_range[name] "));";
474         }
475 }
476
477 if (have_assert)
478         print "";
479
480 print "  if (targetm.target_option.save)";
481 print "    targetm.target_option.save (ptr);";
482 print "";
483
484 for (i = 0; i < n_target_other; i++) {
485         print "  ptr->" var_target_other[i] " = " var_target_other[i] ";";
486 }
487
488 for (i = 0; i < n_target_int; i++) {
489         print "  ptr->" var_target_int[i] " = " var_target_int[i] ";";
490 }
491
492 for (i = 0; i < n_target_short; i++) {
493         print "  ptr->" var_target_short[i] " = " var_target_short[i] ";";
494 }
495
496 for (i = 0; i < n_target_char; i++) {
497         print "  ptr->" var_target_char[i] " = " var_target_char[i] ";";
498 }
499
500 print "}";
501
502 print "";
503 print "/* Restore selected current options from a structure.  */";
504 print "void";
505 print "cl_target_option_restore (struct cl_target_option *ptr)";
506 print "{";
507
508 for (i = 0; i < n_target_other; i++) {
509         print "  " var_target_other[i] " = ptr->" var_target_other[i] ";";
510 }
511
512 for (i = 0; i < n_target_int; i++) {
513         print "  " var_target_int[i] " = ptr->" var_target_int[i] ";";
514 }
515
516 for (i = 0; i < n_target_short; i++) {
517         print "  " var_target_short[i] " = ptr->" var_target_short[i] ";";
518 }
519
520 for (i = 0; i < n_target_char; i++) {
521         print "  " var_target_char[i] " = ptr->" var_target_char[i] ";";
522 }
523
524 # This must occur after the normal variables in case the code depends on those
525 # variables.
526 print "";
527 print "  if (targetm.target_option.restore)";
528 print "    targetm.target_option.restore (ptr);";
529
530 print "}";
531
532 print "";
533 print "/* Print optimization options from a structure.  */";
534 print "void";
535 print "cl_target_option_print (FILE *file,";
536 print "                        int indent,";
537 print "                        struct cl_target_option *ptr)";
538 print "{";
539
540 print "  fputs (\"\\n\", file);";
541 for (i = 0; i < n_target_other; i++) {
542         print "  if (ptr->" var_target_other[i] ")";
543         print "    fprintf (file, \"%*s%s (%#lx)\\n\",";
544         print "             indent, \"\",";
545         print "             \"" var_target_other[i] "\",";
546         print "             (unsigned long)ptr->" var_target_other[i] ");";
547         print "";
548 }
549
550 for (i = 0; i < n_target_int; i++) {
551         print "  if (ptr->" var_target_int[i] ")";
552         print "    fprintf (file, \"%*s%s (%#x)\\n\",";
553         print "             indent, \"\",";
554         print "             \"" var_target_int[i] "\",";
555         print "             ptr->" var_target_int[i] ");";
556         print "";
557 }
558
559 for (i = 0; i < n_target_short; i++) {
560         print "  if (ptr->" var_target_short[i] ")";
561         print "    fprintf (file, \"%*s%s (%#x)\\n\",";
562         print "             indent, \"\",";
563         print "             \"" var_target_short[i] "\",";
564         print "             ptr->" var_target_short[i] ");";
565         print "";
566 }
567
568 for (i = 0; i < n_target_char; i++) {
569         print "  if (ptr->" var_target_char[i] ")";
570         print "    fprintf (file, \"%*s%s (%#x)\\n\",";
571         print "             indent, \"\",";
572         print "             \"" var_target_char[i] "\",";
573         print "             ptr->" var_target_char[i] ");";
574         print "";
575 }
576
577 print "";
578 print "  if (targetm.target_option.print)";
579 print "    targetm.target_option.print (file, indent, ptr);";
580
581 print "}";
582 print "#endif";
583
584 }