1 # Copyright (C) 2003,2004,2005,2006,2007,2008, 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
25 # Usage: awk -f opt-functions.awk -f opth-gen.awk < inputfile > options.h
35 # Collect the text and flags of each option into an array
37 if ($1 == "Language") {
41 else if ($1 == "TargetSave") {
42 # Make sure the declarations are put in source order
43 target_save_decl[n_target_save] = $2
47 name = opt_args("Mask", $1)
55 extra_masks[n_extra_masks++] = name
60 # Dump out an enumeration into a .h file.
61 # Combine the flags of duplicate options.
63 print "/* This file is auto-generated by opth-gen.awk. */"
65 print "#ifndef OPTIONS_H"
66 print "#define OPTIONS_H"
68 print "extern int target_flags;"
69 print "extern int target_flags_explicit;"
74 for (i = 0; i < n_opts; i++) {
75 if (flag_set_p("Save", flags[i]))
78 name = var_name(flags[i]);
86 print "extern " var_type(flags[i]) name ";"
90 # All of the optimization switches gathered together so they can be saved and restored.
91 # This will allow attribute((cold)) to turn on space optimization.
93 # Change the type of normal switches from int to unsigned char to save space.
94 # Also, order the structure so that pointer fields occur first, then int
95 # fields, and then char fields to provide the best packing.
97 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
99 print "/* Structure to save/restore optimization and target specific options. */";
100 print "struct GTY(()) cl_optimization";
107 var_opt_char[0] = "unsigned char optimize";
108 var_opt_char[1] = "unsigned char optimize_size";
110 for (i = 0; i < n_opts; i++) {
111 if (flag_set_p("Optimization", flags[i])) {
112 name = var_name(flags[i])
116 if(name in var_opt_seen)
119 var_opt_seen[name]++;
120 otype = var_type_struct(flags[i]);
121 if (otype ~ "^((un)?signed +)?int *$")
122 var_opt_int[n_opt_int++] = otype name;
124 else if (otype ~ "^((un)?signed +)?short *$")
125 var_opt_short[n_opt_short++] = otype name;
127 else if (otype ~ "^((un)?signed +)?char *$")
128 var_opt_char[n_opt_char++] = otype name;
131 var_opt_other[n_opt_other++] = otype name;
135 for (i = 0; i < n_opt_other; i++) {
136 print " " var_opt_other[i] ";";
139 for (i = 0; i < n_opt_int; i++) {
140 print " " var_opt_int[i] ";";
143 for (i = 0; i < n_opt_short; i++) {
144 print " " var_opt_short[i] ";";
147 for (i = 0; i < n_opt_char; i++) {
148 print " " var_opt_char[i] ";";
154 # Target and optimization save/restore/print functions.
155 print "/* Structure to save/restore selected target specific options. */";
156 print "struct GTY(()) cl_target_option";
164 for (i = 0; i < n_target_save; i++) {
165 if (target_save_decl[i] ~ "^((un)?signed +)?int +[_a-zA-Z0-9]+$")
166 var_target_int[n_target_int++] = target_save_decl[i];
168 else if (target_save_decl[i] ~ "^((un)?signed +)?short +[_a-zA-Z0-9]+$")
169 var_target_short[n_target_short++] = target_save_decl[i];
171 else if (target_save_decl[i] ~ "^((un)?signed +)?char +[_a-zA-Z0-9]+$")
172 var_target_char[n_target_char++] = target_save_decl[i];
175 var_target_other[n_target_other++] = target_save_decl[i];
179 for (i = 0; i < n_opts; i++) {
180 if (flag_set_p("Save", flags[i])) {
181 name = var_name(flags[i])
183 name = "target_flags";
185 if(name in var_save_seen)
188 var_save_seen[name]++;
189 otype = var_type_struct(flags[i])
190 if (otype ~ "^((un)?signed +)?int *$")
191 var_target_int[n_target_int++] = otype name;
193 else if (otype ~ "^((un)?signed +)?short *$")
194 var_target_short[n_target_short++] = otype name;
196 else if (otype ~ "^((un)?signed +)?char *$")
197 var_target_char[n_target_char++] = otype name;
200 var_target_other[n_target_other++] = otype name;
204 var_target_int[n_target_int++] = "int target_flags";
207 for (i = 0; i < n_target_other; i++) {
208 print " " var_target_other[i] ";";
211 for (i = 0; i < n_target_int; i++) {
212 print " " var_target_int[i] ";";
215 for (i = 0; i < n_target_short; i++) {
216 print " " var_target_short[i] ";";
219 for (i = 0; i < n_target_char; i++) {
220 print " " var_target_char[i] ";";
226 print "/* Save optimization variables into a structure. */"
227 print "extern void cl_optimization_save (struct cl_optimization *);";
229 print "/* Restore optimization variables from a structure. */";
230 print "extern void cl_optimization_restore (struct cl_optimization *);";
232 print "/* Print optimization variables from a structure. */";
233 print "extern void cl_optimization_print (FILE *, int, struct cl_optimization *);";
235 print "/* Save selected option variables into a structure. */"
236 print "extern void cl_target_option_save (struct cl_target_option *);";
238 print "/* Restore selected option variables from a structure. */"
239 print "extern void cl_target_option_restore (struct cl_target_option *);";
241 print "/* Print target option variables from a structure. */";
242 print "extern void cl_target_option_print (FILE *, int, struct cl_target_option *);";
246 for (i = 0; i < n_opts; i++) {
247 name = opt_args("Mask", flags[i])
248 vname = var_name(flags[i])
251 mask = "OPTION_MASK_"
253 if (name != "" && !flag_set_p("MaskExists", flags[i]))
254 print "#define " mask name " (1 << " masknum[vname]++ ")"
256 for (i = 0; i < n_extra_masks; i++) {
257 print "#define MASK_" extra_masks[i] " (1 << " masknum[""]++ ")"
260 for (var in masknum) {
261 if (masknum[var] > 31) {
263 print "#error too many target masks"
265 print "#error too many masks for " var
270 for (i = 0; i < n_opts; i++) {
271 name = opt_args("Mask", flags[i])
272 vname = var_name(flags[i])
274 mask = "OPTION_MASK_"
276 vname = "target_flags"
280 if (name != "" && !flag_set_p("MaskExists", flags[i]))
281 print "#define " macro name \
282 " ((" vname " & " mask name ") != 0)"
284 for (i = 0; i < n_extra_masks; i++) {
285 print "#define TARGET_" extra_masks[i] \
286 " ((target_flags & MASK_" extra_masks[i] ") != 0)"
290 for (i = 0; i < n_opts; i++) {
291 opt = opt_args("InverseMask", flags[i])
293 vname = var_name(flags[i])
295 mask = "OPTION_MASK_"
297 vname = "target_flags"
301 print "#define " macro nth_arg(1, opt) \
302 " ((" vname " & " mask nth_arg(0, opt) ") == 0)"
307 for (i = 0; i < n_langs; i++) {
308 macros[i] = "CL_" langs[i]
309 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
310 s = substr(" ", length (macros[i]))
311 print "#define " macros[i] s " (1 << " i ")"
313 print "#define CL_LANG_ALL ((1 << " n_langs ") - 1)"
316 print "enum opt_code"
319 for (i = 0; i < n_opts; i++)
320 back_chain[i] = "N_OPTS";
323 for (i = 0; i < n_opts; i++) {
324 # Combine the flags of identical switches. Switches
325 # appear many times if they are handled by many front
327 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
328 flags[i + 1] = flags[i] " " flags[i + 1];
332 len = length (opts[i]);
333 enum = opt_enum(opts[i])
334 enum_string = enum " = " enum_value ","
336 # Aliases do not get enumeration names.
337 if ((flag_set_p("Alias.*", flags[i]) \
338 && !flag_set_p("SeparateAlias", flags[i])) \
339 || flag_set_p("Ignore", flags[i])) {
340 enum_string = "/* " enum_string " */"
343 # If this switch takes joined arguments, back-chain all
344 # subsequent switches to it for which it is a prefix. If
345 # a later switch S is a longer prefix of a switch T, T
346 # will be back-chained to S in a later iteration of this
347 # for() loop, which is what we want.
348 if (flag_set_p("Joined.*", flags[i])) {
349 for (j = i + 1; j < n_opts; j++) {
350 if (substr (opts[j], 1, len) != opts[i])
352 back_chain[j] = enum;
357 length (enum_string))
362 hlp = "N_(\"" help[i] "\")";
364 print " " enum_string s "/* -" opts[i] " */"
369 print " OPT_SPECIAL_unknown,"
370 print " OPT_SPECIAL_ignore,"
371 print " OPT_SPECIAL_program_name,"
372 print " OPT_SPECIAL_input_file"
375 print "#endif /* OPTIONS_H */"