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
205 warn_message = opt_args
("Warn", flags
[i
])
206 if (warn_message ==
"")
209 warn_message = quote warn_message quote
211 alias_arg = opt_args
("Alias", flags
[i
])
212 if (alias_arg ==
"") {
213 if (flag_set_p
("Ignore", flags
[i
]))
214 alias_data =
"NULL, NULL, OPT_SPECIAL_ignore"
216 alias_data =
"NULL, NULL, N_OPTS"
218 alias_opt = nth_arg
(0, alias_arg
)
219 alias_posarg = nth_arg
(1, alias_arg
)
220 alias_negarg = nth_arg
(2, alias_arg
)
222 if (var_ref
(opts
[i
], flags
[i
]) != "0")
223 print "#error Alias setting variable"
225 if (alias_posarg
!= "" && alias_negarg ==
"") {
226 if (!flag_set_p
("RejectNegative", flags
[i
]) \
227 && opts
[i
] ~
"^[Wfm]")
228 print "#error Alias with single argument " \
229 "allowing negative form"
232 alias_opt = opt_enum
(alias_opt
)
233 if (alias_posarg ==
"")
234 alias_posarg =
"NULL"
236 alias_posarg = quote alias_posarg quote
237 if (alias_negarg ==
"")
238 alias_negarg =
"NULL"
240 alias_negarg = quote alias_negarg quote
241 alias_data = alias_posarg
", " alias_negarg
", " alias_opt
244 neg = opt_args
("Negative", flags
[i
]);
248 if (flag_set_p
("RejectNegative", flags
[i
]))
251 if (opts
[i
] ~
"^[Wfm]")
252 idx = indices
[opts
[i
]];
257 # Split the printf after %u to work around an ia64-hp-hpux11.23
259 printf(" { %c-%s%c,\n %s,\n %s,\n %s,\n %s, %s, %u,",
260 quote
, opts
[i
], quote
, hlp
, missing_arg_error
, warn_message
,
261 alias_data
, back_chain
[i
], len
)
262 printf(" %d,\n", idx
)
263 condition = opt_args
("Condition", flags
[i
])
264 cl_flags = switch_flags
(flags
[i
])
271 condition
, cl_flags
, cl_flags
)
273 printf(" %s,\n", cl_flags
)
274 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
275 var_set
(flags
[i
]), comma
)
281 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
283 print "/* Save optimization variables into a structure. */"
285 print "cl_optimization_save (struct cl_optimization *ptr)";
292 var_opt_char
[0] =
"optimize";
293 var_opt_char
[1] =
"optimize_size";
294 var_opt_range
["optimize"] =
"0, 255";
295 var_opt_range
["optimize_size"] =
"0, 255";
297 # Sort by size to mimic how the structure is laid out to be friendlier to the
300 for (i =
0; i
< n_opts
; i
++) {
301 if (flag_set_p
("Optimization", flags
[i
])) {
302 name = var_name
(flags
[i
])
306 if(name in var_opt_seen
)
309 var_opt_seen
[name
]++;
310 otype = var_type_struct
(flags
[i
]);
311 if (otype ~
"^((un)?signed +)?int *$")
312 var_opt_int
[n_opt_int
++] = name
;
314 else if (otype ~
"^((un)?signed +)?short *$")
315 var_opt_short
[n_opt_short
++] = name
;
317 else if (otype ~
"^((un)?signed +)?char *$") {
318 var_opt_char
[n_opt_char
++] = name
;
319 if (otype ~
"^unsigned +char *$")
320 var_opt_range
[name
] =
"0, 255"
321 else if (otype ~
"^signed +char *$")
322 var_opt_range
[name
] =
"-128, 127"
325 var_opt_other
[n_opt_other
++] = name
;
329 for (i =
0; i
< n_opt_char
; i
++) {
330 name = var_opt_char
[i
];
331 if (var_opt_range
[name
] != "")
332 print " gcc_assert (IN_RANGE (" name
", " var_opt_range
[name
] "));";
336 for (i =
0; i
< n_opt_other
; i
++) {
337 print " ptr->" var_opt_other
[i
] " = " var_opt_other
[i
] ";";
340 for (i =
0; i
< n_opt_int
; i
++) {
341 print " ptr->" var_opt_int
[i
] " = " var_opt_int
[i
] ";";
344 for (i =
0; i
< n_opt_short
; i
++) {
345 print " ptr->" var_opt_short
[i
] " = " var_opt_short
[i
] ";";
348 for (i =
0; i
< n_opt_char
; i
++) {
349 print " ptr->" var_opt_char
[i
] " = " var_opt_char
[i
] ";";
355 print "/* Restore optimization options from a structure. */";
357 print "cl_optimization_restore (struct cl_optimization *ptr)";
360 for (i =
0; i
< n_opt_other
; i
++) {
361 print " " var_opt_other
[i
] " = ptr->" var_opt_other
[i
] ";";
364 for (i =
0; i
< n_opt_int
; i
++) {
365 print " " var_opt_int
[i
] " = ptr->" var_opt_int
[i
] ";";
368 for (i =
0; i
< n_opt_short
; i
++) {
369 print " " var_opt_short
[i
] " = ptr->" var_opt_short
[i
] ";";
372 for (i =
0; i
< n_opt_char
; i
++) {
373 print " " var_opt_char
[i
] " = ptr->" var_opt_char
[i
] ";";
376 print " targetm.override_options_after_change ();";
380 print "/* Print optimization options from a structure. */";
382 print "cl_optimization_print (FILE *file,";
383 print " int indent_to,";
384 print " struct cl_optimization *ptr)";
387 print " fputs (\"\\n\", file);";
388 for (i =
0; i
< n_opt_other
; i
++) {
389 print " if (ptr->" var_opt_other
[i
] ")";
390 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
391 print " indent_to, \"\",";
392 print " \"" var_opt_other
[i
] "\",";
393 print " (unsigned long)ptr->" var_opt_other
[i
] ");";
397 for (i =
0; i
< n_opt_int
; i
++) {
398 print " if (ptr->" var_opt_int
[i
] ")";
399 print " fprintf (file, \"%*s%s (%#x)\\n\",";
400 print " indent_to, \"\",";
401 print " \"" var_opt_int
[i
] "\",";
402 print " ptr->" var_opt_int
[i
] ");";
406 for (i =
0; i
< n_opt_short
; i
++) {
407 print " if (ptr->" var_opt_short
[i
] ")";
408 print " fprintf (file, \"%*s%s (%#x)\\n\",";
409 print " indent_to, \"\",";
410 print " \"" var_opt_short
[i
] "\",";
411 print " ptr->" var_opt_short
[i
] ");";
415 for (i =
0; i
< n_opt_char
; i
++) {
416 print " if (ptr->" var_opt_char
[i
] ")";
417 print " fprintf (file, \"%*s%s (%#x)\\n\",";
418 print " indent_to, \"\",";
419 print " \"" var_opt_char
[i
] "\",";
420 print " ptr->" var_opt_char
[i
] ");";
427 print "/* Save selected option variables into a structure. */"
429 print "cl_target_option_save (struct cl_target_option *ptr)";
438 for (i =
0; i
< n_opts
; i
++) {
439 if (flag_set_p
("Save", flags
[i
])) {
440 name = var_name
(flags
[i
])
442 name =
"target_flags";
444 if(name in var_save_seen
)
447 var_save_seen
[name
]++;
448 otype = var_type_struct
(flags
[i
])
449 if (otype ~
"^((un)?signed +)?int *$")
450 var_target_int
[n_target_int
++] = name
;
452 else if (otype ~
"^((un)?signed +)?short *$")
453 var_target_short
[n_target_short
++] = name
;
455 else if (otype ~
"^((un)?signed +)?char *$") {
456 var_target_char
[n_target_char
++] = name
;
457 if (otype ~
"^unsigned +char *$")
458 var_target_range
[name
] =
"0, 255"
459 else if (otype ~
"^signed +char *$")
460 var_target_range
[name
] =
"-128, 127"
463 var_target_other
[n_target_other
++] = name
;
467 var_target_int
[n_target_int
++] =
"target_flags";
471 for (i =
0; i
< n_target_char
; i
++) {
472 name = var_target_char
[i
];
473 if (var_target_range
[name
] != "") {
475 print " gcc_assert (IN_RANGE (" name
", " var_target_range
[name
] "));";
482 print " if (targetm.target_option.save)";
483 print " targetm.target_option.save (ptr);";
486 for (i =
0; i
< n_target_other
; i
++) {
487 print " ptr->" var_target_other
[i
] " = " var_target_other
[i
] ";";
490 for (i =
0; i
< n_target_int
; i
++) {
491 print " ptr->" var_target_int
[i
] " = " var_target_int
[i
] ";";
494 for (i =
0; i
< n_target_short
; i
++) {
495 print " ptr->" var_target_short
[i
] " = " var_target_short
[i
] ";";
498 for (i =
0; i
< n_target_char
; i
++) {
499 print " ptr->" var_target_char
[i
] " = " var_target_char
[i
] ";";
505 print "/* Restore selected current options from a structure. */";
507 print "cl_target_option_restore (struct cl_target_option *ptr)";
510 for (i =
0; i
< n_target_other
; i
++) {
511 print " " var_target_other
[i
] " = ptr->" var_target_other
[i
] ";";
514 for (i =
0; i
< n_target_int
; i
++) {
515 print " " var_target_int
[i
] " = ptr->" var_target_int
[i
] ";";
518 for (i =
0; i
< n_target_short
; i
++) {
519 print " " var_target_short
[i
] " = ptr->" var_target_short
[i
] ";";
522 for (i =
0; i
< n_target_char
; i
++) {
523 print " " var_target_char
[i
] " = ptr->" var_target_char
[i
] ";";
526 # This must occur after the normal variables in case the code depends on those
529 print " if (targetm.target_option.restore)";
530 print " targetm.target_option.restore (ptr);";
535 print "/* Print optimization options from a structure. */";
537 print "cl_target_option_print (FILE *file,";
538 print " int indent,";
539 print " struct cl_target_option *ptr)";
542 print " fputs (\"\\n\", file);";
543 for (i =
0; i
< n_target_other
; i
++) {
544 print " if (ptr->" var_target_other
[i
] ")";
545 print " fprintf (file, \"%*s%s (%#lx)\\n\",";
546 print " indent, \"\",";
547 print " \"" var_target_other
[i
] "\",";
548 print " (unsigned long)ptr->" var_target_other
[i
] ");";
552 for (i =
0; i
< n_target_int
; i
++) {
553 print " if (ptr->" var_target_int
[i
] ")";
554 print " fprintf (file, \"%*s%s (%#x)\\n\",";
555 print " indent, \"\",";
556 print " \"" var_target_int
[i
] "\",";
557 print " ptr->" var_target_int
[i
] ");";
561 for (i =
0; i
< n_target_short
; i
++) {
562 print " if (ptr->" var_target_short
[i
] ")";
563 print " fprintf (file, \"%*s%s (%#x)\\n\",";
564 print " indent, \"\",";
565 print " \"" var_target_short
[i
] "\",";
566 print " ptr->" var_target_short
[i
] ");";
570 for (i =
0; i
< n_target_char
; i
++) {
571 print " if (ptr->" var_target_char
[i
] ")";
572 print " fprintf (file, \"%*s%s (%#x)\\n\",";
573 print " indent, \"\",";
574 print " \"" var_target_char
[i
] "\",";
575 print " ptr->" var_target_char
[i
] ");";
580 print " if (targetm.target_option.print)";
581 print " targetm.target_option.print (file, indent, ptr);";