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
36 # Default the name of header created from opth-gen.awk to options.h
37 if (header_name ==
"") header_name=
"options.h"
40 # Collect the text and flags of each option into an array
42 if ($
1 ==
"Language") {
46 else if ($
1 ==
"TargetSave") {
47 # Make sure the declarations are put in source order
48 target_save_decl
[n_target_save
] = $
2
52 name = opt_args
("Mask", $
1)
57 for (i =
4; i
<=
NF; i
++)
58 help
[n_opts
] = help
[n_opts
] " " $i
64 # Dump that array of options into a C file.
66 print "/* This file is auto-generated by optc-gen.awk. */"
68 n_headers =
split(header_name
, headers
, " ")
69 for (i =
1; i
<= n_headers
; i
++)
70 print "#include " quote headers
[i
] quote
71 print "#include " quote
"opts.h" quote
72 print "#include " quote
"intl.h" quote
74 print "#ifdef GCC_DRIVER"
75 print "int target_flags;"
76 print "int target_flags_explicit;"
78 print "#include " quote
"flags.h" quote
79 print "#include " quote
"target.h" quote
80 print "#endif /* GCC_DRIVER */"
84 for (i =
0; i
< n_opts
; i
++) {
85 if (flag_set_p
("Save", flags
[i
]))
88 name = var_name
(flags
[i
]);
92 if (flag_set_p
("VarExists", flags
[i
])) {
93 # Need it for the gcc driver.
100 init = opt_args
("Init", flags
[i
])
103 else if (name in var_seen
)
109 print "#ifdef GCC_DRIVER"
110 print "/* Set by -" opts
[i
] "."
111 print " " help
[i
] " */"
112 print var_type
(flags
[i
]) name init
";"
114 print "#endif /* GCC_DRIVER */"
121 print "/* Local state variables. */"
122 for (i =
0; i
< n_opts
; i
++) {
123 name = static_var
(opts
[i
], flags
[i
]);
125 print "static " var_type
(flags
[i
]) name
";"
129 print "const char * const lang_names[] =\n{"
130 for (i =
0; i
< n_langs
; i
++) {
131 macros
[i
] =
"CL_" langs
[i
]
132 gsub( "[^A-Za-z0-9_]", "X", macros
[i
] )
133 s =
substr(" ", length (macros
[i
]))
134 print " " quote langs
[i
] quote
","
138 print "const unsigned int cl_options_count = N_OPTS;\n"
139 print "const unsigned int cl_lang_count = " n_langs
";\n"
141 print "const struct cl_option cl_options[] =\n{"
144 for (i =
0; i
< n_opts
; i
++) {
145 back_chain
[i
] =
"N_OPTS";
146 indices
[opts
[i
]] = j
;
147 # Combine the flags of identical switches. Switches
148 # appear many times if they are handled by many front
150 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
151 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
152 if (help
[i
+ 1] ==
"")
153 help
[i
+ 1] = help
[i
]
154 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
])
155 print "warning: multiple different help strings for " \
156 opts
[i
] ":\n\t" help
[i
] "\n\t" help
[i
+ 1] \
159 back_chain
[i
] =
"N_OPTS";
160 indices
[opts
[i
]] = j
;
165 for (i =
0; i
< n_opts
; i
++) {
166 # With identical flags, pick only the last one. The
167 # earlier loop ensured that it has all flags merged,
168 # and a nonempty help text if one of the texts was nonempty.
169 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
173 len =
length (opts
[i
]);
174 enum = opt_enum
(opts
[i
])
176 # If this switch takes joined arguments, back-chain all
177 # subsequent switches to it for which it is a prefix. If
178 # a later switch S is a longer prefix of a switch T, T
179 # will be back-chained to S in a later iteration of this
180 # for() loop, which is what we want.
181 if (flag_set_p
("Joined.*", flags
[i
])) {
182 for (j = i
+ 1; j
< n_opts
; j
++) {
183 if (substr (opts
[j
], 1, len
) != opts
[i
])
185 back_chain
[j
] = enum
;
189 s =
substr(" ", length (opts
[i
]))
196 hlp = quote help
[i
] quote
;
198 missing_arg_error = opt_args
("MissingArgError", flags
[i
])
199 if (missing_arg_error ==
"")
200 missing_arg_error =
"0"
202 missing_arg_error = quote missing_arg_error quote
204 alias_arg = opt_args
("Alias", flags
[i
])
205 if (alias_arg ==
"") {
206 alias_data =
"NULL, NULL, N_OPTS"
208 alias_opt = nth_arg
(0, alias_arg
)
209 alias_posarg = nth_arg
(1, alias_arg
)
210 alias_negarg = nth_arg
(2, alias_arg
)
212 if (var_ref
(opts
[i
], flags
[i
]) != "0")
213 print "#error Alias setting variable"
215 if (alias_posarg
!= "" && alias_negarg ==
"") {
216 if (!flag_set_p
("RejectNegative", flags
[i
]) \
217 && opts
[i
] ~
"^[Wfm]")
218 print "#error Alias with single argument " \
219 "allowing negative form"
222 alias_opt = opt_enum
(alias_opt
)
223 if (alias_posarg ==
"")
224 alias_posarg =
"NULL"
226 alias_posarg = quote alias_posarg quote
227 if (alias_negarg ==
"")
228 alias_negarg =
"NULL"
230 alias_negarg = quote alias_negarg quote
231 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
234 neg = opt_args
("Negative", flags
[i
]);
238 if (flag_set_p
("RejectNegative", flags
[i
]))
241 if (opts
[i
] ~
"^[Wfm]")
242 idx = indices
[opts
[i
]];
247 # Split the printf after %u to work around an ia64-hp-hpux11.23
249 printf(" { %c-%s%c,\n %s,\n %s,\n %s, %s, %u,",
250 quote
, opts
[i
], quote
, hlp
, missing_arg_error
,
251 alias_data
, back_chain
[i
], len
)
252 printf(" %d,\n", idx
)
253 condition = opt_args
("Condition", flags
[i
])
254 cl_flags = switch_flags
(flags
[i
])
261 condition
, cl_flags
, cl_flags
)
263 printf(" %s,\n", cl_flags
)
264 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
265 var_set
(flags
[i
]), comma
)
271 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
273 print "/* Save optimization variables into a structure. */"
275 print "cl_optimization_save (struct cl_optimization *ptr)";
282 var_opt_char
[0] =
"optimize";
283 var_opt_char
[1] =
"optimize_size";
284 var_opt_range
["optimize"] =
"0, 255";
285 var_opt_range
["optimize_size"] =
"0, 255";
287 # Sort by size to mimic how the structure is laid out to be friendlier to the
290 for (i =
0; i
< n_opts
; i
++) {
291 if (flag_set_p
("Optimization", flags
[i
])) {
292 name = var_name
(flags
[i
])
296 if(name in var_opt_seen
)
299 var_opt_seen
[name
]++;
300 otype = var_type_struct
(flags
[i
]);
301 if (otype ~
"^((un)?signed +)?int *$")
302 var_opt_int
[n_opt_int
++] = name
;
304 else if (otype ~
"^((un)?signed +)?short *$")
305 var_opt_short
[n_opt_short
++] = name
;
307 else if (otype ~
"^((un)?signed +)?char *$") {
308 var_opt_char
[n_opt_char
++] = name
;
309 if (otype ~
"^unsigned +char *$")
310 var_opt_range
[name
] =
"0, 255"
311 else if (otype ~
"^signed +char *$")
312 var_opt_range
[name
] =
"-128, 127"
315 var_opt_other
[n_opt_other
++] = name
;
319 for (i =
0; i
< n_opt_char
; i
++) {
320 name = var_opt_char
[i
];
321 if (var_opt_range
[name
] != "")
322 print " gcc_assert (IN_RANGE (" name
", " var_opt_range
[name
] "));";
326 for (i =
0; i
< n_opt_other
; i
++) {
327 print " ptr->" var_opt_other
[i
] " = " var_opt_other
[i
] ";";
330 for (i =
0; i
< n_opt_int
; i
++) {
331 print " ptr->" var_opt_int
[i
] " = " var_opt_int
[i
] ";";
334 for (i =
0; i
< n_opt_short
; i
++) {
335 print " ptr->" var_opt_short
[i
] " = " var_opt_short
[i
] ";";
338 for (i =
0; i
< n_opt_char
; i
++) {
339 print " ptr->" var_opt_char
[i
] " = " var_opt_char
[i
] ";";
345 print "/* Restore optimization options from a structure. */";
347 print "cl_optimization_restore (struct cl_optimization *ptr)";
350 for (i =
0; i
< n_opt_other
; i
++) {
351 print " " var_opt_other
[i
] " = ptr->" var_opt_other
[i
] ";";
354 for (i =
0; i
< n_opt_int
; i
++) {
355 print " " var_opt_int
[i
] " = ptr->" var_opt_int
[i
] ";";
358 for (i =
0; i
< n_opt_short
; i
++) {
359 print " " var_opt_short
[i
] " = ptr->" var_opt_short
[i
] ";";
362 for (i =
0; i
< n_opt_char
; i
++) {
363 print " " var_opt_char
[i
] " = ptr->" var_opt_char
[i
] ";";
366 print " targetm.override_options_after_change ();";
370 print "/* Print optimization options from a structure. */";
372 print "cl_optimization_print (FILE *file,";
373 print " int indent_to,";
374 print " struct cl_optimization *ptr)";
377 print " fputs (\"\\n\", file);";
378 for (i =
0; i
< n_opt_other
; i
++) {
379 print " if (ptr->" var_opt_other
[i
] ")";
380 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
381 print " indent_to, \"\",";
382 print " \"" var_opt_other
[i
] "\",";
383 print " (unsigned long)ptr->" var_opt_other
[i
] ");";
387 for (i =
0; i
< n_opt_int
; i
++) {
388 print " if (ptr->" var_opt_int
[i
] ")";
389 print " fprintf (file, \"%*s%s (%#x)\\n\",";
390 print " indent_to, \"\",";
391 print " \"" var_opt_int
[i
] "\",";
392 print " ptr->" var_opt_int
[i
] ");";
396 for (i =
0; i
< n_opt_short
; i
++) {
397 print " if (ptr->" var_opt_short
[i
] ")";
398 print " fprintf (file, \"%*s%s (%#x)\\n\",";
399 print " indent_to, \"\",";
400 print " \"" var_opt_short
[i
] "\",";
401 print " ptr->" var_opt_short
[i
] ");";
405 for (i =
0; i
< n_opt_char
; i
++) {
406 print " if (ptr->" var_opt_char
[i
] ")";
407 print " fprintf (file, \"%*s%s (%#x)\\n\",";
408 print " indent_to, \"\",";
409 print " \"" var_opt_char
[i
] "\",";
410 print " ptr->" var_opt_char
[i
] ");";
417 print "/* Save selected option variables into a structure. */"
419 print "cl_target_option_save (struct cl_target_option *ptr)";
428 for (i =
0; i
< n_opts
; i
++) {
429 if (flag_set_p
("Save", flags
[i
])) {
430 name = var_name
(flags
[i
])
432 name =
"target_flags";
434 if(name in var_save_seen
)
437 var_save_seen
[name
]++;
438 otype = var_type_struct
(flags
[i
])
439 if (otype ~
"^((un)?signed +)?int *$")
440 var_target_int
[n_target_int
++] = name
;
442 else if (otype ~
"^((un)?signed +)?short *$")
443 var_target_short
[n_target_short
++] = name
;
445 else if (otype ~
"^((un)?signed +)?char *$") {
446 var_target_char
[n_target_char
++] = name
;
447 if (otype ~
"^unsigned +char *$")
448 var_target_range
[name
] =
"0, 255"
449 else if (otype ~
"^signed +char *$")
450 var_target_range
[name
] =
"-128, 127"
453 var_target_other
[n_target_other
++] = name
;
457 var_target_int
[n_target_int
++] =
"target_flags";
461 for (i =
0; i
< n_target_char
; i
++) {
462 name = var_target_char
[i
];
463 if (var_target_range
[name
] != "") {
465 print " gcc_assert (IN_RANGE (" name
", " var_target_range
[name
] "));";
472 print " if (targetm.target_option.save)";
473 print " targetm.target_option.save (ptr);";
476 for (i =
0; i
< n_target_other
; i
++) {
477 print " ptr->" var_target_other
[i
] " = " var_target_other
[i
] ";";
480 for (i =
0; i
< n_target_int
; i
++) {
481 print " ptr->" var_target_int
[i
] " = " var_target_int
[i
] ";";
484 for (i =
0; i
< n_target_short
; i
++) {
485 print " ptr->" var_target_short
[i
] " = " var_target_short
[i
] ";";
488 for (i =
0; i
< n_target_char
; i
++) {
489 print " ptr->" var_target_char
[i
] " = " var_target_char
[i
] ";";
495 print "/* Restore selected current options from a structure. */";
497 print "cl_target_option_restore (struct cl_target_option *ptr)";
500 for (i =
0; i
< n_target_other
; i
++) {
501 print " " var_target_other
[i
] " = ptr->" var_target_other
[i
] ";";
504 for (i =
0; i
< n_target_int
; i
++) {
505 print " " var_target_int
[i
] " = ptr->" var_target_int
[i
] ";";
508 for (i =
0; i
< n_target_short
; i
++) {
509 print " " var_target_short
[i
] " = ptr->" var_target_short
[i
] ";";
512 for (i =
0; i
< n_target_char
; i
++) {
513 print " " var_target_char
[i
] " = ptr->" var_target_char
[i
] ";";
516 # This must occur after the normal variables in case the code depends on those
519 print " if (targetm.target_option.restore)";
520 print " targetm.target_option.restore (ptr);";
525 print "/* Print optimization options from a structure. */";
527 print "cl_target_option_print (FILE *file,";
528 print " int indent,";
529 print " struct cl_target_option *ptr)";
532 print " fputs (\"\\n\", file);";
533 for (i =
0; i
< n_target_other
; i
++) {
534 print " if (ptr->" var_target_other
[i
] ")";
535 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
536 print " indent, \"\",";
537 print " \"" var_target_other
[i
] "\",";
538 print " (unsigned long)ptr->" var_target_other
[i
] ");";
542 for (i =
0; i
< n_target_int
; i
++) {
543 print " if (ptr->" var_target_int
[i
] ")";
544 print " fprintf (file, \"%*s%s (%#x)\\n\",";
545 print " indent, \"\",";
546 print " \"" var_target_int
[i
] "\",";
547 print " ptr->" var_target_int
[i
] ");";
551 for (i =
0; i
< n_target_short
; i
++) {
552 print " if (ptr->" var_target_short
[i
] ")";
553 print " fprintf (file, \"%*s%s (%#x)\\n\",";
554 print " indent, \"\",";
555 print " \"" var_target_short
[i
] "\",";
556 print " ptr->" var_target_short
[i
] ");";
560 for (i =
0; i
< n_target_char
; i
++) {
561 print " if (ptr->" var_target_char
[i
] ")";
562 print " fprintf (file, \"%*s%s (%#x)\\n\",";
563 print " indent, \"\",";
564 print " \"" var_target_char
[i
] "\",";
565 print " ptr->" var_target_char
[i
] ");";
570 print " if (targetm.target_option.print)";
571 print " targetm.target_option.print (file, indent, ptr);";