1 # Copyright (C) 2003, 2004, 2007 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)
61 # Dump that array of options into a C file.
63 print "/* This file is auto-generated by optc-gen.awk. */"
65 n_headers =
split(header_name
, headers
, " ")
66 for (i =
1; i
<= n_headers
; i
++)
67 print "#include " quote headers
[i
] quote
68 print "#include " quote
"opts.h" quote
69 print "#include " quote
"intl.h" quote
71 print "#ifdef GCC_DRIVER"
72 print "int target_flags;"
74 print "#include " quote
"flags.h" quote
75 print "#include " quote
"target.h" quote
76 print "#endif /* GCC_DRIVER */"
80 for (i =
0; i
< n_opts
; i
++) {
81 if (flag_set_p
("Save", flags
[i
]))
84 name = var_name
(flags
[i
]);
88 if (flag_set_p
("VarExists", flags
[i
])) {
89 # Need it for the gcc driver.
96 init = opt_args
("Init", flags
[i
])
99 else if (name in var_seen
)
105 print "#ifdef GCC_DRIVER"
106 print "/* Set by -" opts
[i
] "."
107 print " " help
[i
] " */"
108 print var_type
(flags
[i
]) name init
";"
110 print "#endif /* GCC_DRIVER */"
117 print "/* Local state variables. */"
118 for (i =
0; i
< n_opts
; i
++) {
119 name = static_var
(opts
[i
], flags
[i
]);
121 print "static " var_type
(flags
[i
]) name
";"
125 print "const char * const lang_names[] =\n{"
126 for (i =
0; i
< n_langs
; i
++) {
127 macros
[i
] =
"CL_" langs
[i
]
128 gsub( "[^A-Za-z0-9_]", "X", macros
[i
] )
129 s =
substr(" ", length (macros
[i
]))
130 print " " quote langs
[i
] quote
","
134 print "const unsigned int cl_options_count = N_OPTS;\n"
135 print "const unsigned int cl_lang_count = " n_langs
";\n"
137 print "const struct cl_option cl_options[] =\n{"
140 for (i =
0; i
< n_opts
; i
++) {
141 back_chain
[i
] =
"N_OPTS";
142 indices
[opts
[i
]] = j
;
143 # Combine the flags of identical switches. Switches
144 # appear many times if they are handled by many front
146 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
147 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
149 back_chain
[i
] =
"N_OPTS";
150 indices
[opts
[i
]] = j
;
155 for (i =
0; i
< n_opts
; i
++) {
156 # Combine the flags of identical switches. Switches
157 # appear many times if they are handled by many front
159 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
160 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
164 len =
length (opts
[i
]);
165 enum =
"OPT_" opts
[i
]
166 if (opts
[i
] ==
"finline-limit=" || opts
[i
] ==
"Wlarger-than=")
168 gsub ("[^A-Za-z0-9]", "_", enum
)
170 # If this switch takes joined arguments, back-chain all
171 # subsequent switches to it for which it is a prefix. If
172 # a later switch S is a longer prefix of a switch T, T
173 # will be back-chained to S in a later iteration of this
174 # for() loop, which is what we want.
175 if (flag_set_p
("Joined.*", flags
[i
])) {
176 for (j = i
+ 1; j
< n_opts
; j
++) {
177 if (substr (opts
[j
], 1, len
) != opts
[i
])
179 back_chain
[j
] = enum
;
183 s =
substr(" ", length (opts
[i
]))
190 hlp = quote help
[i
] quote
;
192 neg = opt_args
("Negative", flags
[i
]);
196 if (flag_set_p
("RejectNegative", flags
[i
]))
199 if (opts
[i
] ~
"^[Wfm]")
200 idx = indices
[opts
[i
]];
205 # Split the printf after %u to work around an ia64-hp-hpux11.23
207 printf(" { %c-%s%c,\n %s,\n %s, %u,",
208 quote
, opts
[i
], quote
, hlp
, back_chain
[i
], len
)
209 printf(" %d,\n", idx
)
210 condition = opt_args
("Condition", flags
[i
])
211 cl_flags = switch_flags
(flags
[i
])
218 condition
, cl_flags
, cl_flags
)
220 printf(" %s,\n", cl_flags
)
221 printf(" %s, %s }%s\n", var_ref
(opts
[i
], flags
[i
]),
222 var_set
(flags
[i
]), comma
)
228 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
230 print "/* Save optimization variables into a structure. */"
232 print "cl_optimization_save (struct cl_optimization *ptr)";
239 var_opt_char
[0] =
"optimize";
240 var_opt_char
[1] =
"optimize_size";
241 var_opt_range
["optimize"] =
"0, 255";
242 var_opt_range
["optimize_size"] =
"0, 255";
244 # Sort by size to mimic how the structure is laid out to be friendlier to the
247 for (i =
0; i
< n_opts
; i
++) {
248 if (flag_set_p
("Optimization", flags
[i
])) {
249 name = var_name
(flags
[i
])
253 if(name in var_opt_seen
)
256 var_opt_seen
[name
]++;
257 otype = var_type_struct
(flags
[i
]);
258 if (otype ~
"^((un)?signed +)?int *$")
259 var_opt_int
[n_opt_int
++] = name
;
261 else if (otype ~
"^((un)?signed +)?short *$")
262 var_opt_short
[n_opt_short
++] = name
;
264 else if (otype ~
"^((un)?signed +)?char *$") {
265 var_opt_char
[n_opt_char
++] = name
;
266 if (otype ~
"^unsigned +char *$")
267 var_opt_range
[name
] =
"0, 255"
268 else if (otype ~
"^signed +char *$")
269 var_opt_range
[name
] =
"-128, 127"
272 var_opt_other
[n_opt_other
++] = name
;
276 for (i =
0; i
< n_opt_char
; i
++) {
277 name = var_opt_char
[i
];
278 if (var_opt_range
[name
] != "")
279 print " gcc_assert (IN_RANGE (" name
", " var_opt_range
[name
] "));";
283 for (i =
0; i
< n_opt_other
; i
++) {
284 print " ptr->" var_opt_other
[i
] " = " var_opt_other
[i
] ";";
287 for (i =
0; i
< n_opt_int
; i
++) {
288 print " ptr->" var_opt_int
[i
] " = " var_opt_int
[i
] ";";
291 for (i =
0; i
< n_opt_short
; i
++) {
292 print " ptr->" var_opt_short
[i
] " = " var_opt_short
[i
] ";";
295 for (i =
0; i
< n_opt_char
; i
++) {
296 print " ptr->" var_opt_char
[i
] " = " var_opt_char
[i
] ";";
302 print "/* Restore optimization options from a structure. */";
304 print "cl_optimization_restore (struct cl_optimization *ptr)";
307 for (i =
0; i
< n_opt_other
; i
++) {
308 print " " var_opt_other
[i
] " = ptr->" var_opt_other
[i
] ";";
311 for (i =
0; i
< n_opt_int
; i
++) {
312 print " " var_opt_int
[i
] " = ptr->" var_opt_int
[i
] ";";
315 for (i =
0; i
< n_opt_short
; i
++) {
316 print " " var_opt_short
[i
] " = ptr->" var_opt_short
[i
] ";";
319 for (i =
0; i
< n_opt_char
; i
++) {
320 print " " var_opt_char
[i
] " = ptr->" var_opt_char
[i
] ";";
326 print "/* Print optimization options from a structure. */";
328 print "cl_optimization_print (FILE *file,";
329 print " int indent_to,";
330 print " struct cl_optimization *ptr)";
333 print " fputs (\"\\n\", file);";
334 for (i =
0; i
< n_opt_other
; i
++) {
335 print " if (ptr->" var_opt_other
[i
] ")";
336 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
337 print " indent_to, \"\",";
338 print " \"" var_opt_other
[i
] "\",";
339 print " (unsigned long)ptr->" var_opt_other
[i
] ");";
343 for (i =
0; i
< n_opt_int
; i
++) {
344 print " if (ptr->" var_opt_int
[i
] ")";
345 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
346 print " indent_to, \"\",";
347 print " \"" var_opt_int
[i
] "\",";
348 print " ptr->" var_opt_int
[i
] ");";
352 for (i =
0; i
< n_opt_short
; i
++) {
353 print " if (ptr->" var_opt_short
[i
] ")";
354 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
355 print " indent_to, \"\",";
356 print " \"" var_opt_short
[i
] "\",";
357 print " ptr->" var_opt_short
[i
] ");";
361 for (i =
0; i
< n_opt_char
; i
++) {
362 print " if (ptr->" var_opt_char
[i
] ")";
363 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
364 print " indent_to, \"\",";
365 print " \"" var_opt_char
[i
] "\",";
366 print " ptr->" var_opt_char
[i
] ");";
373 print "/* Save selected option variables into a structure. */"
375 print "cl_target_option_save (struct cl_target_option *ptr)";
384 for (i =
0; i
< n_opts
; i
++) {
385 if (flag_set_p
("Save", flags
[i
])) {
386 name = var_name
(flags
[i
])
388 name =
"target_flags";
390 if(name in var_save_seen
)
393 var_save_seen
[name
]++;
394 otype = var_type_struct
(flags
[i
])
395 if (otype ~
"^((un)?signed +)?int *$")
396 var_target_int
[n_target_int
++] = name
;
398 else if (otype ~
"^((un)?signed +)?short *$")
399 var_target_short
[n_target_short
++] = name
;
401 else if (otype ~
"^((un)?signed +)?char *$") {
402 var_target_char
[n_target_char
++] = name
;
403 if (otype ~
"^unsigned +char *$")
404 var_target_range
[name
] =
"0, 255"
405 else if (otype ~
"^signed +char *$")
406 var_target_range
[name
] =
"-128, 127"
409 var_target_other
[n_target_other
++] = name
;
413 var_target_int
[n_target_int
++] =
"target_flags";
417 for (i =
0; i
< n_target_char
; i
++) {
418 name = var_target_char
[i
];
419 if (var_target_range
[name
] != "") {
421 print " gcc_assert (IN_RANGE (" name
", " var_target_range
[name
] "));";
428 print " if (targetm.target_option.save)";
429 print " targetm.target_option.save (ptr);";
432 for (i =
0; i
< n_target_other
; i
++) {
433 print " ptr->" var_target_other
[i
] " = " var_target_other
[i
] ";";
436 for (i =
0; i
< n_target_int
; i
++) {
437 print " ptr->" var_target_int
[i
] " = " var_target_int
[i
] ";";
440 for (i =
0; i
< n_target_short
; i
++) {
441 print " ptr->" var_target_short
[i
] " = " var_target_short
[i
] ";";
444 for (i =
0; i
< n_target_char
; i
++) {
445 print " ptr->" var_target_char
[i
] " = " var_target_char
[i
] ";";
451 print "/* Restore selected current options from a structure. */";
453 print "cl_target_option_restore (struct cl_target_option *ptr)";
456 for (i =
0; i
< n_target_other
; i
++) {
457 print " " var_target_other
[i
] " = ptr->" var_target_other
[i
] ";";
460 for (i =
0; i
< n_target_int
; i
++) {
461 print " " var_target_int
[i
] " = ptr->" var_target_int
[i
] ";";
464 for (i =
0; i
< n_target_short
; i
++) {
465 print " " var_target_short
[i
] " = ptr->" var_target_short
[i
] ";";
468 for (i =
0; i
< n_target_char
; i
++) {
469 print " " var_target_char
[i
] " = ptr->" var_target_char
[i
] ";";
472 # This must occur after the normal variables in case the code depends on those
475 print " if (targetm.target_option.restore)";
476 print " targetm.target_option.restore (ptr);";
481 print "/* Print optimization options from a structure. */";
483 print "cl_target_option_print (FILE *file,";
484 print " int indent,";
485 print " struct cl_target_option *ptr)";
488 print " fputs (\"\\n\", file);";
489 for (i =
0; i
< n_target_other
; i
++) {
490 print " if (ptr->" var_target_other
[i
] ")";
491 print " fprintf (file, \"%*s%s (0x%lx)\\n\",";
492 print " indent, \"\",";
493 print " \"" var_target_other
[i
] "\",";
494 print " (unsigned long)ptr->" var_target_other
[i
] ");";
498 for (i =
0; i
< n_target_int
; i
++) {
499 print " if (ptr->" var_target_int
[i
] ")";
500 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
501 print " indent, \"\",";
502 print " \"" var_target_int
[i
] "\",";
503 print " ptr->" var_target_int
[i
] ");";
507 for (i =
0; i
< n_target_short
; i
++) {
508 print " if (ptr->" var_target_short
[i
] ")";
509 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
510 print " indent, \"\",";
511 print " \"" var_target_short
[i
] "\",";
512 print " ptr->" var_target_short
[i
] ");";
516 for (i =
0; i
< n_target_char
; i
++) {
517 print " if (ptr->" var_target_char
[i
] ")";
518 print " fprintf (file, \"%*s%s (0x%x)\\n\",";
519 print " indent, \"\",";
520 print " \"" var_target_char
[i
] "\",";
521 print " ptr->" var_target_char
[i
] ");";
526 print " if (targetm.target_option.print)";
527 print " targetm.target_option.print (file, indent, ptr);";