1 # Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
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
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
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
34 n_extra_target_vars =
0
35 n_extra_c_includes =
0
36 n_extra_h_includes =
0
41 # Default the name of header created from opth-gen.awk to options.h
42 if (header_name ==
"") header_name=
"options.h"
45 # Collect the text and flags of each option into an array
47 if ($
1 ==
"Language") {
51 else if ($
1 ==
"TargetSave") {
52 # Make sure the declarations are put in source order
53 target_save_decl
[n_target_save
] = $
2
56 else if ($
1 ==
"Variable") {
57 extra_vars
[n_extra_vars
] = $
2
60 else if ($
1 ==
"TargetVariable") {
61 # Combination of TargetSave and Variable
62 extra_vars
[n_extra_vars
] = $
2
70 sub("^.*[ *]", "", name
)
71 sub(" *" name
"$", "", type
)
72 target_save_decl
[n_target_save
] = type
" x_" name
75 extra_target_vars
[n_extra_target_vars
] = name
76 n_extra_target_vars
++;
78 else if ($
1 ==
"HeaderInclude") {
79 extra_h_includes
[n_extra_h_includes
++] = $
2;
81 else if ($
1 ==
"SourceInclude") {
82 extra_c_includes
[n_extra_c_includes
++] = $
2;
84 else if ($
1 ==
"Enum") {
86 name = opt_args
("Name", props
)
87 type = opt_args
("Type", props
)
88 unknown_error = opt_args
("UnknownError", props
)
89 enum_names
[n_enums
] = name
90 enum_type
[name
] = type
91 enum_index
[name
] = n_enums
92 enum_unknown_error
[name
] = unknown_error
96 else if ($
1 ==
"EnumValue") {
98 enum_name = opt_args
("Enum", props
)
99 string = opt_args
("String", props
)
100 value = opt_args
("Value", props
)
102 val_flags = val_flags \
103 test_flag
("Canonical", props
, "| CL_ENUM_CANONICAL") \
104 test_flag
("DriverOnly", props
, "| CL_ENUM_DRIVER_ONLY")
105 enum_data
[enum_name
] = enum_data
[enum_name
] \
106 " { " quote string quote
", " value
", " val_flags \
110 name = opt_args
("Mask", $
1)
115 for (i =
4; i
<=
NF; i
++)
116 help
[n_opts
] = help
[n_opts
] " " $i
122 # Dump that array of options into a C file.
124 print "/* This file is auto-generated by optc-gen.awk. */"
126 n_headers =
split(header_name
, headers
, " ")
127 for (i =
1; i
<= n_headers
; i
++)
128 print "#include " quote headers
[i
] quote
129 print "#include " quote
"opts.h" quote
130 print "#include " quote
"intl.h" quote
132 print "#ifndef GCC_DRIVER"
133 print "#include " quote
"flags.h" quote
134 print "#include " quote
"target.h" quote
135 print "#endif /* GCC_DRIVER */"
138 if (n_extra_c_includes
> 0) {
139 for (i =
0; i
< n_extra_c_includes
; i
++) {
140 print "#include " quote extra_c_includes
[i
] quote
145 for (i =
0; i
< n_enums
; i
++) {
147 type = enum_type
[name
]
148 print "static const struct cl_enum_arg cl_enum_" name \
151 print enum_data
[name
] " { NULL, 0, 0 }"
155 print "cl_enum_" name
"_set (void *var, int value)"
157 print " *((" type
" *) var) = (" type
") value;"
161 print "cl_enum_" name
"_get (const void *var)"
163 print " return (int) *((const " type
" *) var);"
168 print "const struct cl_enum cl_enums[] ="
170 for (i =
0; i
< n_enums
; i
++) {
172 ehelp = enum_help
[name
]
176 ehelp = quote ehelp quote
177 unknown_error = enum_unknown_error
[name
]
178 if (unknown_error ==
"")
179 unknown_error =
"NULL"
181 unknown_error = quote unknown_error quote
184 print " " unknown_error
","
185 print " cl_enum_" name
"_data,"
186 print " sizeof (" enum_type
[name
] "),"
187 print " cl_enum_" name
"_set,"
188 print " cl_enum_" name
"_get"
192 print "const unsigned int cl_enums_count = " n_enums
";"
196 if (n_extra_target_vars
)
199 print "const struct gcc_options global_options_init =\n{"
200 for (i =
0; i
< n_extra_vars
; i
++) {
204 sub(".*= *", "", init
)
205 sub(" *=.*", "", var
)
206 sub("^.*[ *]", "", var
)
207 sub("\\[.*\\]$", "", var
)
212 print " " init
", /* " var
" */"
214 for (i =
0; i
< n_opts
; i
++) {
215 if (flag_set_p
("Save", flags
[i
]))
218 name = var_name
(flags
[i
]);
222 init = opt_args
("Init", flags
[i
])
224 if (name in var_init
&& var_init
[name
] != init
)
225 print "#error multiple initializers for " name
226 var_init
[name
] = init
229 for (i =
0; i
< n_opts
; i
++) {
230 name = var_name
(flags
[i
]);
234 if (name in var_seen
)
237 if (name in var_init
)
238 init = var_init
[name
]
242 print " " init
", /* " name
" */"
246 for (i =
0; i
< n_opts
; i
++) {
247 name = static_var
(opts
[i
], flags
[i
]);
249 print " 0, /* " name
" (private state) */"
250 print "#undef x_" name
253 for (i =
0; i
< n_opts
; i
++) {
254 if (flag_set_p
("SetByCombined", flags
[i
]))
255 print " false, /* frontend_set_" var_name
(flags
[i
]) " */"
259 print "struct gcc_options global_options;"
260 print "struct gcc_options global_options_set;"
263 print "const char * const lang_names[] =\n{"
264 for (i =
0; i
< n_langs
; i
++) {
265 macros
[i
] =
"CL_" langs
[i
]
266 gsub( "[^" alnum
"_]", "X", macros
[i
] )
267 s =
substr(" ", length (macros
[i
]))
268 print " " quote langs
[i
] quote
","
272 print "const unsigned int cl_options_count = N_OPTS;\n"
273 print "const unsigned int cl_lang_count = " n_langs
";\n"
275 print "const struct cl_option cl_options[] =\n{"
278 for (i =
0; i
< n_opts
; i
++) {
279 back_chain
[i
] =
"N_OPTS";
280 indices
[opts
[i
]] = j
;
281 # Combine the flags of identical switches. Switches
282 # appear many times if they are handled by many front
284 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
285 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
286 if (help
[i
+ 1] ==
"")
287 help
[i
+ 1] = help
[i
]
288 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
])
289 print "warning: multiple different help strings for " \
290 opts
[i
] ":\n\t" help
[i
] "\n\t" help
[i
+ 1] \
293 back_chain
[i
] =
"N_OPTS";
294 indices
[opts
[i
]] = j
;
299 for (i =
0; i
< n_opts
; i
++) {
300 # With identical flags, pick only the last one. The
301 # earlier loop ensured that it has all flags merged,
302 # and a nonempty help text if one of the texts was nonempty.
303 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
307 len =
length (opts
[i
]);
308 enum = opt_enum
(opts
[i
])
310 # If this switch takes joined arguments, back-chain all
311 # subsequent switches to it for which it is a prefix. If
312 # a later switch S is a longer prefix of a switch T, T
313 # will be back-chained to S in a later iteration of this
314 # for() loop, which is what we want.
315 if (flag_set_p
("Joined.*", flags
[i
])) {
316 for (j = i
+ 1; j
< n_opts
; j
++) {
317 if (substr (opts
[j
], 1, len
) != opts
[i
])
319 back_chain
[j
] = enum
;
323 s =
substr(" ", length (opts
[i
]))
330 hlp = quote help
[i
] quote
;
332 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
333 if (missing_arg_error ==
"")
334 missing_arg_error =
"0"
336 missing_arg_error = quote missing_arg_error quote
339 warn_message = opt_args
("Warn", flags
[i
])
340 if (warn_message ==
"")
343 warn_message = quote warn_message quote
345 alias_arg = opt_args
("Alias", flags
[i
])
346 if (alias_arg ==
"") {
347 if (flag_set_p
("Ignore", flags
[i
]))
348 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
350 alias_data =
"NULL, NULL, N_OPTS"
352 alias_opt = nth_arg
(0, alias_arg
)
353 alias_posarg = nth_arg
(1, alias_arg
)
354 alias_negarg = nth_arg
(2, alias_arg
)
356 if (var_ref
(opts
[i
], flags
[i
]) != "-1")
357 print "#error Alias setting variable"
359 if (alias_posarg
!= "" && alias_negarg ==
"") {
360 if (!flag_set_p
("RejectNegative", flags
[i
]) \
361 && opts
[i
] ~
"^[Wfm]")
362 print "#error Alias with single argument " \
363 "allowing negative form"
366 alias_opt = opt_enum
(alias_opt
)
367 if (alias_posarg ==
"")
368 alias_posarg =
"NULL"
370 alias_posarg = quote alias_posarg quote
371 if (alias_negarg ==
"")
372 alias_negarg =
"NULL"
374 alias_negarg = quote alias_negarg quote
375 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
378 neg = opt_args
("Negative", flags
[i
]);
382 if (flag_set_p
("RejectNegative", flags
[i
]))
385 if (opts
[i
] ~
"^[Wfm]")
386 idx = indices
[opts
[i
]];
391 # Split the printf after %u to work around an ia64-hp-hpux11.23
393 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
394 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
395 alias_data
, back_chain
[i
], len
)
396 printf(" %d,\n", idx
)
397 condition = opt_args
("Condition", flags
[i
])
398 cl_flags = switch_flags
(flags
[i
])
405 condition
, cl_flags
, cl_flags
)
407 printf(" %s,\n", cl_flags
)
408 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
409 var_set
(flags
[i
]), comma
)
415 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
417 print "/* Save optimization variables into a structure. */"
419 print "cl_optimization_save (struct cl_optimization *ptr, struct gcc_options *opts)";
427 var_opt_char
[0] =
"optimize";
428 var_opt_char
[1] =
"optimize_size";
429 var_opt_range
["optimize"] =
"0, 255";
430 var_opt_range
["optimize_size"] =
"0, 255";
431 var_opt_enum
[0] =
"flag_fp_contract_mode";
433 # Sort by size to mimic how the structure is laid out to be friendlier to the
436 for (i =
0; i
< n_opts
; i
++) {
437 if (flag_set_p
("Optimization", flags
[i
])) {
438 name = var_name
(flags
[i
])
442 if(name in var_opt_seen
)
445 var_opt_seen
[name
]++;
446 otype = var_type_struct
(flags
[i
]);
447 if (otype ~
"^((un)?signed +)?int *$")
448 var_opt_int
[n_opt_int
++] = name
;
450 else if (otype ~
"^((un)?signed +)?short *$")
451 var_opt_short
[n_opt_short
++] = name
;
453 else if (otype ~
("^enum +[_" alnum
"]+ *"))
454 var_opt_enum
[n_opt_enum
++] = name
;
456 else if (otype ~
"^((un)?signed +)?char *$") {
457 var_opt_char
[n_opt_char
++] = name
;
458 if (otype ~
"^unsigned +char *$")
459 var_opt_range
[name
] =
"0, 255"
460 else if (otype ~
"^signed +char *$")
461 var_opt_range
[name
] =
"-128, 127"
464 var_opt_other
[n_opt_other
++] = name
;
468 for (i =
0; i
< n_opt_char
; i
++) {
469 name = var_opt_char
[i
];
470 if (var_opt_range
[name
] != "")
471 print " gcc_assert (IN_RANGE (opts->x_" name
", " var_opt_range
[name
] "));";
475 for (i =
0; i
< n_opt_other
; i
++) {
476 print " ptr->x_" var_opt_other
[i
] " = opts->x_" var_opt_other
[i
] ";";
479 for (i =
0; i
< n_opt_int
; i
++) {
480 print " ptr->x_" var_opt_int
[i
] " = opts->x_" var_opt_int
[i
] ";";
483 for (i =
0; i
< n_opt_enum
; i
++) {
484 print " ptr->x_" var_opt_enum
[i
] " = opts->x_" var_opt_enum
[i
] ";";
487 for (i =
0; i
< n_opt_short
; i
++) {
488 print " ptr->x_" var_opt_short
[i
] " = opts->x_" var_opt_short
[i
] ";";
491 for (i =
0; i
< n_opt_char
; i
++) {
492 print " ptr->x_" var_opt_char
[i
] " = opts->x_" var_opt_char
[i
] ";";
498 print "/* Restore optimization options from a structure. */";
500 print "cl_optimization_restore (struct gcc_options *opts, struct cl_optimization *ptr)";
503 for (i =
0; i
< n_opt_other
; i
++) {
504 print " opts->x_" var_opt_other
[i
] " = ptr->x_" var_opt_other
[i
] ";";
507 for (i =
0; i
< n_opt_int
; i
++) {
508 print " opts->x_" var_opt_int
[i
] " = ptr->x_" var_opt_int
[i
] ";";
511 for (i =
0; i
< n_opt_enum
; i
++) {
512 print " opts->x_" var_opt_enum
[i
] " = ptr->x_" var_opt_enum
[i
] ";";
515 for (i =
0; i
< n_opt_short
; i
++) {
516 print " opts->x_" var_opt_short
[i
] " = ptr->x_" var_opt_short
[i
] ";";
519 for (i =
0; i
< n_opt_char
; i
++) {
520 print " opts->x_" var_opt_char
[i
] " = ptr->x_" var_opt_char
[i
] ";";
523 print " targetm.override_options_after_change ();";
527 print "/* Print optimization options from a structure. */";
529 print "cl_optimization_print (FILE *file,";
530 print " int indent_to,";
531 print " struct cl_optimization *ptr)";
534 print " fputs (\"\\n\", file);";
535 for (i =
0; i
< n_opt_other
; i
++) {
536 print " if (ptr->x_" var_opt_other
[i
] ")";
537 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
538 print " indent_to, \"\",";
539 print " \"" var_opt_other
[i
] "\",";
540 print " (unsigned long)ptr->x_" var_opt_other
[i
] ");";
544 for (i =
0; i
< n_opt_int
; i
++) {
545 print " if (ptr->x_" var_opt_int
[i
] ")";
546 print " fprintf (file, \"%*s%s (%#x)\\n\",";
547 print " indent_to, \"\",";
548 print " \"" var_opt_int
[i
] "\",";
549 print " ptr->x_" var_opt_int
[i
] ");";
553 for (i =
0; i
< n_opt_enum
; i
++) {
554 print " fprintf (file, \"%*s%s (%#x)\\n\",";
555 print " indent_to, \"\",";
556 print " \"" var_opt_enum
[i
] "\",";
557 print " (int) ptr->x_" var_opt_enum
[i
] ");";
561 for (i =
0; i
< n_opt_short
; i
++) {
562 print " if (ptr->x_" var_opt_short
[i
] ")";
563 print " fprintf (file, \"%*s%s (%#x)\\n\",";
564 print " indent_to, \"\",";
565 print " \"" var_opt_short
[i
] "\",";
566 print " ptr->x_" var_opt_short
[i
] ");";
570 for (i =
0; i
< n_opt_char
; i
++) {
571 print " if (ptr->x_" var_opt_char
[i
] ")";
572 print " fprintf (file, \"%*s%s (%#x)\\n\",";
573 print " indent_to, \"\",";
574 print " \"" var_opt_char
[i
] "\",";
575 print " ptr->x_" var_opt_char
[i
] ");";
582 print "/* Save selected option variables into a structure. */"
584 print "cl_target_option_save (struct cl_target_option *ptr, struct gcc_options *opts)";
594 for (i =
0; i
< n_opts
; i
++) {
595 if (flag_set_p
("Save", flags
[i
])) {
596 name = var_name
(flags
[i
])
598 name =
"target_flags";
600 if(name in var_save_seen
)
603 var_save_seen
[name
]++;
604 otype = var_type_struct
(flags
[i
])
605 if (otype ~
"^((un)?signed +)?int *$")
606 var_target_int
[n_target_int
++] = name
;
608 else if (otype ~
"^((un)?signed +)?short *$")
609 var_target_short
[n_target_short
++] = name
;
611 else if (otype ~
("^enum +[_" alnum
"]+ *$"))
612 var_target_enum
[n_target_enum
++] = name
;
614 else if (otype ~
"^((un)?signed +)?char *$") {
615 var_target_char
[n_target_char
++] = name
;
616 if (otype ~
"^unsigned +char *$")
617 var_target_range
[name
] =
"0, 255"
618 else if (otype ~
"^signed +char *$")
619 var_target_range
[name
] =
"-128, 127"
622 var_target_other
[n_target_other
++] = name
;
626 var_target_int
[n_target_int
++] =
"target_flags";
630 for (i =
0; i
< n_target_char
; i
++) {
631 name = var_target_char
[i
];
632 if (var_target_range
[name
] != "") {
634 print " gcc_assert (IN_RANGE (opts->x_" name
", " var_target_range
[name
] "));";
641 print " if (targetm.target_option.save)";
642 print " targetm.target_option.save (ptr);";
645 for (i =
0; i
< n_extra_target_vars
; i
++) {
646 print " ptr->x_" extra_target_vars
[i
] " = opts->x_" extra_target_vars
[i
] ";";
649 for (i =
0; i
< n_target_other
; i
++) {
650 print " ptr->x_" var_target_other
[i
] " = opts->x_" var_target_other
[i
] ";";
653 for (i =
0; i
< n_target_enum
; i
++) {
654 print " ptr->x_" var_target_enum
[i
] " = opts->x_" var_target_enum
[i
] ";";
657 for (i =
0; i
< n_target_int
; i
++) {
658 print " ptr->x_" var_target_int
[i
] " = opts->x_" var_target_int
[i
] ";";
661 for (i =
0; i
< n_target_short
; i
++) {
662 print " ptr->x_" var_target_short
[i
] " = opts->x_" var_target_short
[i
] ";";
665 for (i =
0; i
< n_target_char
; i
++) {
666 print " ptr->x_" var_target_char
[i
] " = opts->x_" var_target_char
[i
] ";";
672 print "/* Restore selected current options from a structure. */";
674 print "cl_target_option_restore (struct gcc_options *opts, struct cl_target_option *ptr)";
677 for (i =
0; i
< n_extra_target_vars
; i
++) {
678 print " opts->x_" extra_target_vars
[i
] " = ptr->x_" extra_target_vars
[i
] ";";
681 for (i =
0; i
< n_target_other
; i
++) {
682 print " opts->x_" var_target_other
[i
] " = ptr->x_" var_target_other
[i
] ";";
685 for (i =
0; i
< n_target_enum
; i
++) {
686 print " opts->x_" var_target_enum
[i
] " = ptr->x_" var_target_enum
[i
] ";";
689 for (i =
0; i
< n_target_int
; i
++) {
690 print " opts->x_" var_target_int
[i
] " = ptr->x_" var_target_int
[i
] ";";
693 for (i =
0; i
< n_target_short
; i
++) {
694 print " opts->x_" var_target_short
[i
] " = ptr->x_" var_target_short
[i
] ";";
697 for (i =
0; i
< n_target_char
; i
++) {
698 print " opts->x_" var_target_char
[i
] " = ptr->x_" var_target_char
[i
] ";";
701 # This must occur after the normal variables in case the code depends on those
704 print " if (targetm.target_option.restore)";
705 print " targetm.target_option.restore (ptr);";
710 print "/* Print optimization options from a structure. */";
712 print "cl_target_option_print (FILE *file,";
713 print " int indent,";
714 print " struct cl_target_option *ptr)";
717 print " fputs (\"\\n\", file);";
718 for (i =
0; i
< n_target_other
; i
++) {
719 print " if (ptr->x_" var_target_other
[i
] ")";
720 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
721 print " indent, \"\",";
722 print " \"" var_target_other
[i
] "\",";
723 print " (unsigned long)ptr->x_" var_target_other
[i
] ");";
727 for (i =
0; i
< n_target_enum
; i
++) {
728 print " if (ptr->x_" var_target_enum
[i
] ")";
729 print " fprintf (file, \"%*s%s (%#x)\\n\",";
730 print " indent, \"\",";
731 print " \"" var_target_enum
[i
] "\",";
732 print " ptr->x_" var_target_enum
[i
] ");";
736 for (i =
0; i
< n_target_int
; i
++) {
737 print " if (ptr->x_" var_target_int
[i
] ")";
738 print " fprintf (file, \"%*s%s (%#x)\\n\",";
739 print " indent, \"\",";
740 print " \"" var_target_int
[i
] "\",";
741 print " ptr->x_" var_target_int
[i
] ");";
745 for (i =
0; i
< n_target_short
; i
++) {
746 print " if (ptr->x_" var_target_short
[i
] ")";
747 print " fprintf (file, \"%*s%s (%#x)\\n\",";
748 print " indent, \"\",";
749 print " \"" var_target_short
[i
] "\",";
750 print " ptr->x_" var_target_short
[i
] ");";
754 for (i =
0; i
< n_target_char
; i
++) {
755 print " if (ptr->x_" var_target_char
[i
] ")";
756 print " fprintf (file, \"%*s%s (%#x)\\n\",";
757 print " indent, \"\",";
758 print " \"" var_target_char
[i
] "\",";
759 print " ptr->x_" var_target_char
[i
] ");";
764 print " if (targetm.target_option.print)";
765 print " targetm.target_option.print (file, indent, ptr);";