OSDN Git Service

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