OSDN Git Service

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