1 # Copyright (C) 2003, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
2 # Contributed by Kelley Cook, June 2004.
3 # Original code from Neil Booth, May 2003.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; see the file COPYING3. If not see
17 # <http://www.gnu.org/licenses/>.
19 # This Awk script reads in the option records generated from
20 # opt-gather.awk, combines the flags of duplicate options and generates a
23 # This program uses functions from opt-functions.awk
25 # Usage: awk -f opt-functions.awk -f optc-gen.awk \
26 # [-v header_name=header.h] < inputfile > options.c
35 # Default the name of header created from opth-gen.awk to options.h
36 if (header_name ==
"") header_name=
"options.h"
39 # Collect the text and flags of each option into an array
41 if ($
1 ==
"Language") {
45 else if ($
1 ==
"TargetSave") {
46 # Make sure the declarations are put in source order
47 target_save_decl
[n_target_save
] = $
2
51 name = opt_args
("Mask", $
1)
56 for (i =
4; i
<=
NF; i
++)
57 help
[n_opts
] = help
[n_opts
] " " $i
63 # Dump that array of options into a C file.
65 print "/* This file is auto-generated by optc-gen.awk. */"
67 n_headers =
split(header_name
, headers
, " ")
68 for (i =
1; i
<= n_headers
; i
++)
69 print "#include " quote headers
[i
] quote
70 print "#include " quote
"opts.h" quote
71 print "#include " quote
"intl.h" quote
73 print "#ifdef GCC_DRIVER"
74 print "int target_flags;"
76 print "#include " quote
"flags.h" quote
77 print "#include " quote
"target.h" quote
78 print "#endif /* GCC_DRIVER */"
82 for (i =
0; i
< n_opts
; i
++) {
83 if (flag_set_p
("Save", flags
[i
]))
86 name = var_name
(flags
[i
]);
90 if (flag_set_p
("VarExists", flags
[i
])) {
91 # Need it for the gcc driver.
98 init = opt_args
("Init", flags
[i
])
101 else if (name in var_seen
)
107 print "#ifdef GCC_DRIVER"
108 print "/* Set by -" opts
[i
] "."
109 print " " help
[i
] " */"
110 print var_type
(flags
[i
]) name init
";"
112 print "#endif /* GCC_DRIVER */"
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_" opts
[i
]
173 if (opts
[i
] ==
"finline-limit=" || opts
[i
] ==
"Wlarger-than=")
175 gsub ("[^A-Za-z0-9]", "_", enum
)
177 # If this switch takes joined arguments, back-chain all
178 # subsequent switches to it for which it is a prefix. If
179 # a later switch S is a longer prefix of a switch T, T
180 # will be back-chained to S in a later iteration of this
181 # for() loop, which is what we want.
182 if (flag_set_p
("Joined.*", flags
[i
])) {
183 for (j = i
+ 1; j
< n_opts
; j
++) {
184 if (substr (opts
[j
], 1, len
) != opts
[i
])
186 back_chain
[j
] = enum
;
190 s =
substr(" ", length (opts
[i
]))
197 hlp = quote help
[i
] quote
;
199 neg = opt_args
("Negative", flags
[i
]);
203 if (flag_set_p
("RejectNegative", flags
[i
]))
206 if (opts
[i
] ~
"^[Wfm]")
207 idx = indices
[opts
[i
]];
212 # Split the printf after %u to work around an ia64-hp-hpux11.23
214 printf(" { %c-%s%c,\n %s,\n %s, %u,",
215 quote
, opts
[i
], quote
, hlp
, back_chain
[i
], len
)
216 printf(" %d,\n", idx
)
217 condition = opt_args
("Condition", flags
[i
])
218 cl_flags = switch_flags
(flags
[i
])
225 condition
, cl_flags
, cl_flags
)
227 printf(" %s,\n", cl_flags
)
228 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
229 var_set
(flags
[i
]), comma
)
235 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
237 print "/* Save optimization variables into a structure. */"
239 print "cl_optimization_save (struct cl_optimization *ptr)";
246 var_opt_char
[0] =
"optimize";
247 var_opt_char
[1] =
"optimize_size";
248 var_opt_range
["optimize"] =
"0, 255";
249 var_opt_range
["optimize_size"] =
"0, 255";
251 # Sort by size to mimic how the structure is laid out to be friendlier to the
254 for (i =
0; i
< n_opts
; i
++) {
255 if (flag_set_p
("Optimization", flags
[i
])) {
256 name = var_name
(flags
[i
])
260 if(name in var_opt_seen
)
263 var_opt_seen
[name
]++;
264 otype = var_type_struct
(flags
[i
]);
265 if (otype ~
"^((un)?signed +)?int *$")
266 var_opt_int
[n_opt_int
++] = name
;
268 else if (otype ~
"^((un)?signed +)?short *$")
269 var_opt_short
[n_opt_short
++] = name
;
271 else if (otype ~
"^((un)?signed +)?char *$") {
272 var_opt_char
[n_opt_char
++] = name
;
273 if (otype ~
"^unsigned +char *$")
274 var_opt_range
[name
] =
"0, 255"
275 else if (otype ~
"^signed +char *$")
276 var_opt_range
[name
] =
"-128, 127"
279 var_opt_other
[n_opt_other
++] = name
;
283 for (i =
0; i
< n_opt_char
; i
++) {
284 name = var_opt_char
[i
];
285 if (var_opt_range
[name
] != "")
286 print " gcc_assert (IN_RANGE (" name
", " var_opt_range
[name
] "));";
290 for (i =
0; i
< n_opt_other
; i
++) {
291 print " ptr->" var_opt_other
[i
] " = " var_opt_other
[i
] ";";
294 for (i =
0; i
< n_opt_int
; i
++) {
295 print " ptr->" var_opt_int
[i
] " = " var_opt_int
[i
] ";";
298 for (i =
0; i
< n_opt_short
; i
++) {
299 print " ptr->" var_opt_short
[i
] " = " var_opt_short
[i
] ";";
302 for (i =
0; i
< n_opt_char
; i
++) {
303 print " ptr->" var_opt_char
[i
] " = " var_opt_char
[i
] ";";
309 print "/* Restore optimization options from a structure. */";
311 print "cl_optimization_restore (struct cl_optimization *ptr)";
314 for (i =
0; i
< n_opt_other
; i
++) {
315 print " " var_opt_other
[i
] " = ptr->" var_opt_other
[i
] ";";
318 for (i =
0; i
< n_opt_int
; i
++) {
319 print " " var_opt_int
[i
] " = ptr->" var_opt_int
[i
] ";";
322 for (i =
0; i
< n_opt_short
; i
++) {
323 print " " var_opt_short
[i
] " = ptr->" var_opt_short
[i
] ";";
326 for (i =
0; i
< n_opt_char
; i
++) {
327 print " " var_opt_char
[i
] " = ptr->" var_opt_char
[i
] ";";
333 print "/* Print optimization options from a structure. */";
335 print "cl_optimization_print (FILE *file,";
336 print " int indent_to,";
337 print " struct cl_optimization *ptr)";
340 print " fputs (\"\\n\", file);";
341 for (i =
0; i
< n_opt_other
; i
++) {
342 print " if (ptr->" var_opt_other
[i
] ")";
343 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
344 print " indent_to, \"\",";
345 print " \"" var_opt_other
[i
] "\",";
346 print " (unsigned long)ptr->" var_opt_other
[i
] ");";
350 for (i =
0; i
< n_opt_int
; i
++) {
351 print " if (ptr->" var_opt_int
[i
] ")";
352 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
353 print " indent_to, \"\",";
354 print " \"" var_opt_int
[i
] "\",";
355 print " ptr->" var_opt_int
[i
] ");";
359 for (i =
0; i
< n_opt_short
; i
++) {
360 print " if (ptr->" var_opt_short
[i
] ")";
361 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
362 print " indent_to, \"\",";
363 print " \"" var_opt_short
[i
] "\",";
364 print " ptr->" var_opt_short
[i
] ");";
368 for (i =
0; i
< n_opt_char
; i
++) {
369 print " if (ptr->" var_opt_char
[i
] ")";
370 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
371 print " indent_to, \"\",";
372 print " \"" var_opt_char
[i
] "\",";
373 print " ptr->" var_opt_char
[i
] ");";
380 print "/* Save selected option variables into a structure. */"
382 print "cl_target_option_save (struct cl_target_option *ptr)";
391 for (i =
0; i
< n_opts
; i
++) {
392 if (flag_set_p
("Save", flags
[i
])) {
393 name = var_name
(flags
[i
])
395 name =
"target_flags";
397 if(name in var_save_seen
)
400 var_save_seen
[name
]++;
401 otype = var_type_struct
(flags
[i
])
402 if (otype ~
"^((un)?signed +)?int *$")
403 var_target_int
[n_target_int
++] = name
;
405 else if (otype ~
"^((un)?signed +)?short *$")
406 var_target_short
[n_target_short
++] = name
;
408 else if (otype ~
"^((un)?signed +)?char *$") {
409 var_target_char
[n_target_char
++] = name
;
410 if (otype ~
"^unsigned +char *$")
411 var_target_range
[name
] =
"0, 255"
412 else if (otype ~
"^signed +char *$")
413 var_target_range
[name
] =
"-128, 127"
416 var_target_other
[n_target_other
++] = name
;
420 var_target_int
[n_target_int
++] =
"target_flags";
424 for (i =
0; i
< n_target_char
; i
++) {
425 name = var_target_char
[i
];
426 if (var_target_range
[name
] != "") {
428 print " gcc_assert (IN_RANGE (" name
", " var_target_range
[name
] "));";
435 print " if (targetm.target_option.save)";
436 print " targetm.target_option.save (ptr);";
439 for (i =
0; i
< n_target_other
; i
++) {
440 print " ptr->" var_target_other
[i
] " = " var_target_other
[i
] ";";
443 for (i =
0; i
< n_target_int
; i
++) {
444 print " ptr->" var_target_int
[i
] " = " var_target_int
[i
] ";";
447 for (i =
0; i
< n_target_short
; i
++) {
448 print " ptr->" var_target_short
[i
] " = " var_target_short
[i
] ";";
451 for (i =
0; i
< n_target_char
; i
++) {
452 print " ptr->" var_target_char
[i
] " = " var_target_char
[i
] ";";
458 print "/* Restore selected current options from a structure. */";
460 print "cl_target_option_restore (struct cl_target_option *ptr)";
463 for (i =
0; i
< n_target_other
; i
++) {
464 print " " var_target_other
[i
] " = ptr->" var_target_other
[i
] ";";
467 for (i =
0; i
< n_target_int
; i
++) {
468 print " " var_target_int
[i
] " = ptr->" var_target_int
[i
] ";";
471 for (i =
0; i
< n_target_short
; i
++) {
472 print " " var_target_short
[i
] " = ptr->" var_target_short
[i
] ";";
475 for (i =
0; i
< n_target_char
; i
++) {
476 print " " var_target_char
[i
] " = ptr->" var_target_char
[i
] ";";
479 # This must occur after the normal variables in case the code depends on those
482 print " if (targetm.target_option.restore)";
483 print " targetm.target_option.restore (ptr);";
488 print "/* Print optimization options from a structure. */";
490 print "cl_target_option_print (FILE *file,";
491 print " int indent,";
492 print " struct cl_target_option *ptr)";
495 print " fputs (\"\\n\", file);";
496 for (i =
0; i
< n_target_other
; i
++) {
497 print " if (ptr->" var_target_other
[i
] ")";
498 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
499 print " indent, \"\",";
500 print " \"" var_target_other
[i
] "\",";
501 print " (unsigned long)ptr->" var_target_other
[i
] ");";
505 for (i =
0; i
< n_target_int
; i
++) {
506 print " if (ptr->" var_target_int
[i
] ")";
507 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
508 print " indent, \"\",";
509 print " \"" var_target_int
[i
] "\",";
510 print " ptr->" var_target_int
[i
] ");";
514 for (i =
0; i
< n_target_short
; i
++) {
515 print " if (ptr->" var_target_short
[i
] ")";
516 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
517 print " indent, \"\",";
518 print " \"" var_target_short
[i
] "\",";
519 print " ptr->" var_target_short
[i
] ");";
523 for (i =
0; i
< n_target_char
; i
++) {
524 print " if (ptr->" var_target_char
[i
] ")";
525 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
526 print " indent, \"\",";
527 print " \"" var_target_char
[i
] "\",";
528 print " ptr->" var_target_char
[i
] ");";
533 print " if (targetm.target_option.print)";
534 print " targetm.target_option.print (file, indent, ptr);";