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
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
37 # Default the name of header created from opth-gen.awk to options.h
38 if (header_name ==
"") header_name=
"options.h"
41 # Collect the text and flags of each option into an array
43 if ($
1 ==
"Language") {
47 else if ($
1 ==
"TargetSave") {
48 # Make sure the declarations are put in source order
49 target_save_decl
[n_target_save
] = $
2
52 else if ($
1 ==
"Variable") {
53 extra_vars
[n_extra_vars
] = $
2
57 name = opt_args
("Mask", $
1)
62 for (i =
4; i
<=
NF; i
++)
63 help
[n_opts
] = help
[n_opts
] " " $i
69 # Dump that array of options into a C file.
71 print "/* This file is auto-generated by optc-gen.awk. */"
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
79 print "#ifdef GCC_DRIVER"
80 print "int target_flags_explicit;"
82 print "#include " quote
"flags.h" quote
83 print "#include " quote
"target.h" quote
84 print "#endif /* GCC_DRIVER */"
88 for (i =
0; i
< n_extra_vars
; i
++) {
89 print extra_vars
[i
] ";"
91 for (i =
0; i
< n_opts
; i
++) {
92 if (flag_set_p
("Save", flags
[i
]))
95 name = var_name
(flags
[i
]);
99 if (flag_set_p
("VarExists", flags
[i
])) {
103 init = opt_args
("Init", flags
[i
])
106 else if (name in var_seen
)
110 print "/* Set by -" opts
[i
] "."
111 print " " help
[i
] " */"
112 print var_type
(flags
[i
]) name init
";"
119 print "/* Local state variables. */"
120 for (i =
0; i
< n_opts
; i
++) {
121 name = static_var
(opts
[i
], flags
[i
]);
123 print "static " var_type
(flags
[i
]) name
";"
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
","
136 print "const unsigned int cl_options_count = N_OPTS;\n"
137 print "const unsigned int cl_lang_count = " n_langs
";\n"
139 print "const struct cl_option cl_options[] =\n{"
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
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] \
157 back_chain
[i
] =
"N_OPTS";
158 indices
[opts
[i
]] = j
;
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] ) {
171 len =
length (opts
[i
]);
172 enum = opt_enum
(opts
[i
])
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
])
183 back_chain
[j
] = enum
;
187 s =
substr(" ", length (opts
[i
]))
194 hlp = quote help
[i
] quote
;
196 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
197 if (missing_arg_error ==
"")
198 missing_arg_error =
"0"
200 missing_arg_error = quote missing_arg_error quote
203 warn_message = opt_args
("Warn", flags
[i
])
204 if (warn_message ==
"")
207 warn_message = quote warn_message quote
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"
214 alias_data =
"NULL, NULL, N_OPTS"
216 alias_opt = nth_arg
(0, alias_arg
)
217 alias_posarg = nth_arg
(1, alias_arg
)
218 alias_negarg = nth_arg
(2, alias_arg
)
220 if (var_ref
(opts
[i
], flags
[i
]) != "0")
221 print "#error Alias setting variable"
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"
230 alias_opt = opt_enum
(alias_opt
)
231 if (alias_posarg ==
"")
232 alias_posarg =
"NULL"
234 alias_posarg = quote alias_posarg quote
235 if (alias_negarg ==
"")
236 alias_negarg =
"NULL"
238 alias_negarg = quote alias_negarg quote
239 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
242 neg = opt_args
("Negative", flags
[i
]);
246 if (flag_set_p
("RejectNegative", flags
[i
]))
249 if (opts
[i
] ~
"^[Wfm]")
250 idx = indices
[opts
[i
]];
255 # Split the printf after %u to work around an ia64-hp-hpux11.23
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
])
269 condition
, cl_flags
, cl_flags
)
271 printf(" %s,\n", cl_flags
)
272 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
273 var_set
(flags
[i
]), comma
)
279 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
281 print "/* Save optimization variables into a structure. */"
283 print "cl_optimization_save (struct cl_optimization *ptr)";
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";
295 # Sort by size to mimic how the structure is laid out to be friendlier to the
298 for (i =
0; i
< n_opts
; i
++) {
299 if (flag_set_p
("Optimization", flags
[i
])) {
300 name = var_name
(flags
[i
])
304 if(name in var_opt_seen
)
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
;
312 else if (otype ~
"^((un)?signed +)?short *$")
313 var_opt_short
[n_opt_short
++] = name
;
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"
323 var_opt_other
[n_opt_other
++] = name
;
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
] "));";
334 for (i =
0; i
< n_opt_other
; i
++) {
335 print " ptr->" var_opt_other
[i
] " = " var_opt_other
[i
] ";";
338 for (i =
0; i
< n_opt_int
; i
++) {
339 print " ptr->" var_opt_int
[i
] " = " var_opt_int
[i
] ";";
342 for (i =
0; i
< n_opt_short
; i
++) {
343 print " ptr->" var_opt_short
[i
] " = " var_opt_short
[i
] ";";
346 for (i =
0; i
< n_opt_char
; i
++) {
347 print " ptr->" var_opt_char
[i
] " = " var_opt_char
[i
] ";";
353 print "/* Restore optimization options from a structure. */";
355 print "cl_optimization_restore (struct cl_optimization *ptr)";
358 for (i =
0; i
< n_opt_other
; i
++) {
359 print " " var_opt_other
[i
] " = ptr->" var_opt_other
[i
] ";";
362 for (i =
0; i
< n_opt_int
; i
++) {
363 print " " var_opt_int
[i
] " = ptr->" var_opt_int
[i
] ";";
366 for (i =
0; i
< n_opt_short
; i
++) {
367 print " " var_opt_short
[i
] " = ptr->" var_opt_short
[i
] ";";
370 for (i =
0; i
< n_opt_char
; i
++) {
371 print " " var_opt_char
[i
] " = ptr->" var_opt_char
[i
] ";";
374 print " targetm.override_options_after_change ();";
378 print "/* Print optimization options from a structure. */";
380 print "cl_optimization_print (FILE *file,";
381 print " int indent_to,";
382 print " struct cl_optimization *ptr)";
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
] ");";
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
] ");";
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
] ");";
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
] ");";
425 print "/* Save selected option variables into a structure. */"
427 print "cl_target_option_save (struct cl_target_option *ptr)";
436 for (i =
0; i
< n_opts
; i
++) {
437 if (flag_set_p
("Save", flags
[i
])) {
438 name = var_name
(flags
[i
])
440 name =
"target_flags";
442 if(name in var_save_seen
)
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
;
450 else if (otype ~
"^((un)?signed +)?short *$")
451 var_target_short
[n_target_short
++] = name
;
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"
461 var_target_other
[n_target_other
++] = name
;
465 var_target_int
[n_target_int
++] =
"target_flags";
469 for (i =
0; i
< n_target_char
; i
++) {
470 name = var_target_char
[i
];
471 if (var_target_range
[name
] != "") {
473 print " gcc_assert (IN_RANGE (" name
", " var_target_range
[name
] "));";
480 print " if (targetm.target_option.save)";
481 print " targetm.target_option.save (ptr);";
484 for (i =
0; i
< n_target_other
; i
++) {
485 print " ptr->" var_target_other
[i
] " = " var_target_other
[i
] ";";
488 for (i =
0; i
< n_target_int
; i
++) {
489 print " ptr->" var_target_int
[i
] " = " var_target_int
[i
] ";";
492 for (i =
0; i
< n_target_short
; i
++) {
493 print " ptr->" var_target_short
[i
] " = " var_target_short
[i
] ";";
496 for (i =
0; i
< n_target_char
; i
++) {
497 print " ptr->" var_target_char
[i
] " = " var_target_char
[i
] ";";
503 print "/* Restore selected current options from a structure. */";
505 print "cl_target_option_restore (struct cl_target_option *ptr)";
508 for (i =
0; i
< n_target_other
; i
++) {
509 print " " var_target_other
[i
] " = ptr->" var_target_other
[i
] ";";
512 for (i =
0; i
< n_target_int
; i
++) {
513 print " " var_target_int
[i
] " = ptr->" var_target_int
[i
] ";";
516 for (i =
0; i
< n_target_short
; i
++) {
517 print " " var_target_short
[i
] " = ptr->" var_target_short
[i
] ";";
520 for (i =
0; i
< n_target_char
; i
++) {
521 print " " var_target_char
[i
] " = ptr->" var_target_char
[i
] ";";
524 # This must occur after the normal variables in case the code depends on those
527 print " if (targetm.target_option.restore)";
528 print " targetm.target_option.restore (ptr);";
533 print "/* Print optimization options from a structure. */";
535 print "cl_target_option_print (FILE *file,";
536 print " int indent,";
537 print " struct cl_target_option *ptr)";
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
] ");";
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
] ");";
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
] ");";
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
] ");";
578 print " if (targetm.target_option.print)";
579 print " targetm.target_option.print (file, indent, ptr);";