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;"
77 print "#include " quote
"flags.h" quote
78 print "#include " quote
"target.h" quote
79 print "#endif /* GCC_DRIVER */"
83 for (i =
0; i
< n_opts
; i
++) {
84 if (flag_set_p
("Save", flags
[i
]))
87 name = var_name
(flags
[i
]);
91 if (flag_set_p
("VarExists", flags
[i
])) {
92 # Need it for the gcc driver.
99 init = opt_args
("Init", flags
[i
])
102 else if (name in var_seen
)
108 print "#ifdef GCC_DRIVER"
109 print "/* Set by -" opts
[i
] "."
110 print " " help
[i
] " */"
111 print var_type
(flags
[i
]) name init
";"
113 print "#endif /* GCC_DRIVER */"
120 print "/* Local state variables. */"
121 for (i =
0; i
< n_opts
; i
++) {
122 name = static_var
(opts
[i
], flags
[i
]);
124 print "static " var_type
(flags
[i
]) name
";"
128 print "const char * const lang_names[] =\n{"
129 for (i =
0; i
< n_langs
; i
++) {
130 macros
[i
] =
"CL_" langs
[i
]
131 gsub( "[^A-Za-z0-9_]", "X", macros
[i
] )
132 s =
substr(" ", length (macros
[i
]))
133 print " " quote langs
[i
] quote
","
137 print "const unsigned int cl_options_count = N_OPTS;\n"
138 print "const unsigned int cl_lang_count = " n_langs
";\n"
140 print "const struct cl_option cl_options[] =\n{"
143 for (i =
0; i
< n_opts
; i
++) {
144 back_chain
[i
] =
"N_OPTS";
145 indices
[opts
[i
]] = j
;
146 # Combine the flags of identical switches. Switches
147 # appear many times if they are handled by many front
149 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
150 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
151 if (help
[i
+ 1] ==
"")
152 help
[i
+ 1] = help
[i
]
153 else if (help
[i
] != "" && help
[i
+ 1] != help
[i
])
154 print "warning: multiple different help strings for " \
155 opts
[i
] ":\n\t" help
[i
] "\n\t" help
[i
+ 1] \
158 back_chain
[i
] =
"N_OPTS";
159 indices
[opts
[i
]] = j
;
164 for (i =
0; i
< n_opts
; i
++) {
165 # With identical flags, pick only the last one. The
166 # earlier loop ensured that it has all flags merged,
167 # and a nonempty help text if one of the texts was nonempty.
168 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
172 len =
length (opts
[i
]);
173 enum = opt_enum
(opts
[i
])
175 # If this switch takes joined arguments, back-chain all
176 # subsequent switches to it for which it is a prefix. If
177 # a later switch S is a longer prefix of a switch T, T
178 # will be back-chained to S in a later iteration of this
179 # for() loop, which is what we want.
180 if (flag_set_p
("Joined.*", flags
[i
])) {
181 for (j = i
+ 1; j
< n_opts
; j
++) {
182 if (substr (opts
[j
], 1, len
) != opts
[i
])
184 back_chain
[j
] = enum
;
188 s =
substr(" ", length (opts
[i
]))
195 hlp = quote help
[i
] quote
;
197 neg = opt_args
("Negative", flags
[i
]);
201 if (flag_set_p
("RejectNegative", flags
[i
]))
204 if (opts
[i
] ~
"^[Wfm]")
205 idx = indices
[opts
[i
]];
210 # Split the printf after %u to work around an ia64-hp-hpux11.23
212 printf(" { %c-%s%c,\n %s,\n %s, %u,",
213 quote
, opts
[i
], quote
, hlp
, back_chain
[i
], len
)
214 printf(" %d,\n", idx
)
215 condition = opt_args
("Condition", flags
[i
])
216 cl_flags = switch_flags
(flags
[i
])
223 condition
, cl_flags
, cl_flags
)
225 printf(" %s,\n", cl_flags
)
226 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
227 var_set
(flags
[i
]), comma
)
233 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
235 print "/* Save optimization variables into a structure. */"
237 print "cl_optimization_save (struct cl_optimization *ptr)";
244 var_opt_char
[0] =
"optimize";
245 var_opt_char
[1] =
"optimize_size";
246 var_opt_range
["optimize"] =
"0, 255";
247 var_opt_range
["optimize_size"] =
"0, 255";
249 # Sort by size to mimic how the structure is laid out to be friendlier to the
252 for (i =
0; i
< n_opts
; i
++) {
253 if (flag_set_p
("Optimization", flags
[i
])) {
254 name = var_name
(flags
[i
])
258 if(name in var_opt_seen
)
261 var_opt_seen
[name
]++;
262 otype = var_type_struct
(flags
[i
]);
263 if (otype ~
"^((un)?signed +)?int *$")
264 var_opt_int
[n_opt_int
++] = name
;
266 else if (otype ~
"^((un)?signed +)?short *$")
267 var_opt_short
[n_opt_short
++] = name
;
269 else if (otype ~
"^((un)?signed +)?char *$") {
270 var_opt_char
[n_opt_char
++] = name
;
271 if (otype ~
"^unsigned +char *$")
272 var_opt_range
[name
] =
"0, 255"
273 else if (otype ~
"^signed +char *$")
274 var_opt_range
[name
] =
"-128, 127"
277 var_opt_other
[n_opt_other
++] = name
;
281 for (i =
0; i
< n_opt_char
; i
++) {
282 name = var_opt_char
[i
];
283 if (var_opt_range
[name
] != "")
284 print " gcc_assert (IN_RANGE (" name
", " var_opt_range
[name
] "));";
288 for (i =
0; i
< n_opt_other
; i
++) {
289 print " ptr->" var_opt_other
[i
] " = " var_opt_other
[i
] ";";
292 for (i =
0; i
< n_opt_int
; i
++) {
293 print " ptr->" var_opt_int
[i
] " = " var_opt_int
[i
] ";";
296 for (i =
0; i
< n_opt_short
; i
++) {
297 print " ptr->" var_opt_short
[i
] " = " var_opt_short
[i
] ";";
300 for (i =
0; i
< n_opt_char
; i
++) {
301 print " ptr->" var_opt_char
[i
] " = " var_opt_char
[i
] ";";
307 print "/* Restore optimization options from a structure. */";
309 print "cl_optimization_restore (struct cl_optimization *ptr)";
312 for (i =
0; i
< n_opt_other
; i
++) {
313 print " " var_opt_other
[i
] " = ptr->" var_opt_other
[i
] ";";
316 for (i =
0; i
< n_opt_int
; i
++) {
317 print " " var_opt_int
[i
] " = ptr->" var_opt_int
[i
] ";";
320 for (i =
0; i
< n_opt_short
; i
++) {
321 print " " var_opt_short
[i
] " = ptr->" var_opt_short
[i
] ";";
324 for (i =
0; i
< n_opt_char
; i
++) {
325 print " " var_opt_char
[i
] " = ptr->" var_opt_char
[i
] ";";
328 print " targetm.override_options_after_change ();";
332 print "/* Print optimization options from a structure. */";
334 print "cl_optimization_print (FILE *file,";
335 print " int indent_to,";
336 print " struct cl_optimization *ptr)";
339 print " fputs (\"\\n\", file);";
340 for (i =
0; i
< n_opt_other
; i
++) {
341 print " if (ptr->" var_opt_other
[i
] ")";
342 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
343 print " indent_to, \"\",";
344 print " \"" var_opt_other
[i
] "\",";
345 print " (unsigned long)ptr->" var_opt_other
[i
] ");";
349 for (i =
0; i
< n_opt_int
; i
++) {
350 print " if (ptr->" var_opt_int
[i
] ")";
351 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
352 print " indent_to, \"\",";
353 print " \"" var_opt_int
[i
] "\",";
354 print " ptr->" var_opt_int
[i
] ");";
358 for (i =
0; i
< n_opt_short
; i
++) {
359 print " if (ptr->" var_opt_short
[i
] ")";
360 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
361 print " indent_to, \"\",";
362 print " \"" var_opt_short
[i
] "\",";
363 print " ptr->" var_opt_short
[i
] ");";
367 for (i =
0; i
< n_opt_char
; i
++) {
368 print " if (ptr->" var_opt_char
[i
] ")";
369 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
370 print " indent_to, \"\",";
371 print " \"" var_opt_char
[i
] "\",";
372 print " ptr->" var_opt_char
[i
] ");";
379 print "/* Save selected option variables into a structure. */"
381 print "cl_target_option_save (struct cl_target_option *ptr)";
390 for (i =
0; i
< n_opts
; i
++) {
391 if (flag_set_p
("Save", flags
[i
])) {
392 name = var_name
(flags
[i
])
394 name =
"target_flags";
396 if(name in var_save_seen
)
399 var_save_seen
[name
]++;
400 otype = var_type_struct
(flags
[i
])
401 if (otype ~
"^((un)?signed +)?int *$")
402 var_target_int
[n_target_int
++] = name
;
404 else if (otype ~
"^((un)?signed +)?short *$")
405 var_target_short
[n_target_short
++] = name
;
407 else if (otype ~
"^((un)?signed +)?char *$") {
408 var_target_char
[n_target_char
++] = name
;
409 if (otype ~
"^unsigned +char *$")
410 var_target_range
[name
] =
"0, 255"
411 else if (otype ~
"^signed +char *$")
412 var_target_range
[name
] =
"-128, 127"
415 var_target_other
[n_target_other
++] = name
;
419 var_target_int
[n_target_int
++] =
"target_flags";
423 for (i =
0; i
< n_target_char
; i
++) {
424 name = var_target_char
[i
];
425 if (var_target_range
[name
] != "") {
427 print " gcc_assert (IN_RANGE (" name
", " var_target_range
[name
] "));";
434 print " if (targetm.target_option.save)";
435 print " targetm.target_option.save (ptr);";
438 for (i =
0; i
< n_target_other
; i
++) {
439 print " ptr->" var_target_other
[i
] " = " var_target_other
[i
] ";";
442 for (i =
0; i
< n_target_int
; i
++) {
443 print " ptr->" var_target_int
[i
] " = " var_target_int
[i
] ";";
446 for (i =
0; i
< n_target_short
; i
++) {
447 print " ptr->" var_target_short
[i
] " = " var_target_short
[i
] ";";
450 for (i =
0; i
< n_target_char
; i
++) {
451 print " ptr->" var_target_char
[i
] " = " var_target_char
[i
] ";";
457 print "/* Restore selected current options from a structure. */";
459 print "cl_target_option_restore (struct cl_target_option *ptr)";
462 for (i =
0; i
< n_target_other
; i
++) {
463 print " " var_target_other
[i
] " = ptr->" var_target_other
[i
] ";";
466 for (i =
0; i
< n_target_int
; i
++) {
467 print " " var_target_int
[i
] " = ptr->" var_target_int
[i
] ";";
470 for (i =
0; i
< n_target_short
; i
++) {
471 print " " var_target_short
[i
] " = ptr->" var_target_short
[i
] ";";
474 for (i =
0; i
< n_target_char
; i
++) {
475 print " " var_target_char
[i
] " = ptr->" var_target_char
[i
] ";";
478 # This must occur after the normal variables in case the code depends on those
481 print " if (targetm.target_option.restore)";
482 print " targetm.target_option.restore (ptr);";
487 print "/* Print optimization options from a structure. */";
489 print "cl_target_option_print (FILE *file,";
490 print " int indent,";
491 print " struct cl_target_option *ptr)";
494 print " fputs (\"\\n\", file);";
495 for (i =
0; i
< n_target_other
; i
++) {
496 print " if (ptr->" var_target_other
[i
] ")";
497 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
498 print " indent, \"\",";
499 print " \"" var_target_other
[i
] "\",";
500 print " (unsigned long)ptr->" var_target_other
[i
] ");";
504 for (i =
0; i
< n_target_int
; i
++) {
505 print " if (ptr->" var_target_int
[i
] ")";
506 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
507 print " indent, \"\",";
508 print " \"" var_target_int
[i
] "\",";
509 print " ptr->" var_target_int
[i
] ");";
513 for (i =
0; i
< n_target_short
; i
++) {
514 print " if (ptr->" var_target_short
[i
] ")";
515 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
516 print " indent, \"\",";
517 print " \"" var_target_short
[i
] "\",";
518 print " ptr->" var_target_short
[i
] ");";
522 for (i =
0; i
< n_target_char
; i
++) {
523 print " if (ptr->" var_target_char
[i
] ")";
524 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
525 print " indent, \"\",";
526 print " \"" var_target_char
[i
] "\",";
527 print " ptr->" var_target_char
[i
] ");";
532 print " if (targetm.target_option.print)";
533 print " targetm.target_option.print (file, indent, ptr);";