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
36 # Collect the text and flags of each option into an array
38 if ($1 == "Language") {
42 else if ($1 == "TargetSave") {
43 # Make sure the declarations are put in source order
44 target_save_decl[n_target_save] = $2
47 else if ($1 == "Variable") {
48 extra_vars[n_extra_vars] = $2
52 name = opt_args("Mask", $1)
60 extra_masks[n_extra_masks++] = name
65 # Dump out an enumeration into a .h file.
66 # Combine the flags of duplicate options.
68 print "/* This file is auto-generated by opth-gen.awk. */"
70 print "#ifndef OPTIONS_H"
71 print "#define OPTIONS_H"
73 print "#include \"flag-types.h\""
78 print "#if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS) && !defined(IN_RTS)"
79 print "#ifndef GENERATOR_FILE"
80 print "struct gcc_options\n{"
83 for (i = 0; i < n_extra_vars; i++) {
89 sub("^.*[ *]", "", name)
90 sub(" *" name "$", "", type)
92 print "#ifdef GENERATOR_FILE"
93 print "extern " orig_var ";"
95 print " " type " x_" name ";"
96 print "#define " name " global_options.x_" name
100 for (i = 0; i < n_opts; i++) {
101 if (flag_set_p("Save", flags[i]))
104 name = var_name(flags[i]);
108 if (name in var_seen)
112 print "#ifdef GENERATOR_FILE"
113 print "extern " var_type(flags[i]) name ";"
115 print " " var_type(flags[i]) "x_" name ";"
116 print "#define " name " global_options.x_" name
119 for (i = 0; i < n_opts; i++) {
120 name = static_var(opts[i], flags[i]);
122 print "#ifndef GENERATOR_FILE"
123 print " " var_type(flags[i]) "x_" name ";"
124 print "#define x_" name " do_not_use"
128 print "#ifndef GENERATOR_FILE"
130 print "extern struct gcc_options global_options;"
131 print "extern const struct gcc_options global_options_init;"
132 print "extern struct gcc_options global_options_set;"
133 print "#define target_flags_explicit global_options_set.x_target_flags"
138 # All of the optimization switches gathered together so they can be saved and restored.
139 # This will allow attribute((cold)) to turn on space optimization.
141 # Change the type of normal switches from int to unsigned char to save space.
142 # Also, order the structure so that pointer fields occur first, then int
143 # fields, and then char fields to provide the best packing.
145 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
147 print "/* Structure to save/restore optimization and target specific options. */";
148 print "struct GTY(()) cl_optimization";
155 var_opt_char[0] = "unsigned char x_optimize";
156 var_opt_char[1] = "unsigned char x_optimize_size";
158 for (i = 0; i < n_opts; i++) {
159 if (flag_set_p("Optimization", flags[i])) {
160 name = var_name(flags[i])
164 if(name in var_opt_seen)
167 var_opt_seen[name]++;
168 otype = var_type_struct(flags[i]);
169 if (otype ~ "^((un)?signed +)?int *$")
170 var_opt_int[n_opt_int++] = otype "x_" name;
172 else if (otype ~ "^((un)?signed +)?short *$")
173 var_opt_short[n_opt_short++] = otype "x_" name;
175 else if (otype ~ "^((un)?signed +)?char *$")
176 var_opt_char[n_opt_char++] = otype "x_" name;
179 var_opt_other[n_opt_other++] = otype "x_" name;
183 for (i = 0; i < n_opt_other; i++) {
184 print " " var_opt_other[i] ";";
187 for (i = 0; i < n_opt_int; i++) {
188 print " " var_opt_int[i] ";";
191 for (i = 0; i < n_opt_short; i++) {
192 print " " var_opt_short[i] ";";
195 for (i = 0; i < n_opt_char; i++) {
196 print " " var_opt_char[i] ";";
202 # Target and optimization save/restore/print functions.
203 print "/* Structure to save/restore selected target specific options. */";
204 print "struct GTY(()) cl_target_option";
212 for (i = 0; i < n_target_save; i++) {
213 if (target_save_decl[i] ~ "^((un)?signed +)?int +[_" alnum "]+$")
214 var_target_int[n_target_int++] = target_save_decl[i];
216 else if (target_save_decl[i] ~ "^((un)?signed +)?short +[_" alnum "]+$")
217 var_target_short[n_target_short++] = target_save_decl[i];
219 else if (target_save_decl[i] ~ "^((un)?signed +)?char +[_ " alnum "]+$")
220 var_target_char[n_target_char++] = target_save_decl[i];
223 var_target_other[n_target_other++] = target_save_decl[i];
227 for (i = 0; i < n_opts; i++) {
228 if (flag_set_p("Save", flags[i])) {
229 name = var_name(flags[i])
231 name = "target_flags";
233 if(name in var_save_seen)
236 var_save_seen[name]++;
237 otype = var_type_struct(flags[i])
238 if (otype ~ "^((un)?signed +)?int *$")
239 var_target_int[n_target_int++] = otype "x_" name;
241 else if (otype ~ "^((un)?signed +)?short *$")
242 var_target_short[n_target_short++] = otype "x_" name;
244 else if (otype ~ "^((un)?signed +)?char *$")
245 var_target_char[n_target_char++] = otype "x_" name;
248 var_target_other[n_target_other++] = otype "x_" name;
252 var_target_int[n_target_int++] = "int x_target_flags";
255 for (i = 0; i < n_target_other; i++) {
256 print " " var_target_other[i] ";";
259 for (i = 0; i < n_target_int; i++) {
260 print " " var_target_int[i] ";";
263 for (i = 0; i < n_target_short; i++) {
264 print " " var_target_short[i] ";";
267 for (i = 0; i < n_target_char; i++) {
268 print " " var_target_char[i] ";";
274 print "/* Save optimization variables into a structure. */"
275 print "extern void cl_optimization_save (struct cl_optimization *, struct gcc_options *);";
277 print "/* Restore optimization variables from a structure. */";
278 print "extern void cl_optimization_restore (struct gcc_options *, struct cl_optimization *);";
280 print "/* Print optimization variables from a structure. */";
281 print "extern void cl_optimization_print (FILE *, int, struct cl_optimization *);";
283 print "/* Save selected option variables into a structure. */"
284 print "extern void cl_target_option_save (struct cl_target_option *, struct gcc_options *);";
286 print "/* Restore selected option variables from a structure. */"
287 print "extern void cl_target_option_restore (struct gcc_options *, struct cl_target_option *);";
289 print "/* Print target option variables from a structure. */";
290 print "extern void cl_target_option_print (FILE *, int, struct cl_target_option *);";
294 for (i = 0; i < n_opts; i++) {
295 name = opt_args("Mask", flags[i])
296 vname = var_name(flags[i])
299 mask = "OPTION_MASK_"
301 if (name != "" && !flag_set_p("MaskExists", flags[i]))
302 print "#define " mask name " (1 << " masknum[vname]++ ")"
304 for (i = 0; i < n_extra_masks; i++) {
305 print "#define MASK_" extra_masks[i] " (1 << " masknum[""]++ ")"
308 for (var in masknum) {
309 if (masknum[var] > 31) {
311 print "#error too many target masks"
313 print "#error too many masks for " var
318 for (i = 0; i < n_opts; i++) {
319 name = opt_args("Mask", flags[i])
320 vname = var_name(flags[i])
322 mask = "OPTION_MASK_"
324 vname = "target_flags"
328 if (name != "" && !flag_set_p("MaskExists", flags[i]))
329 print "#define " macro name \
330 " ((" vname " & " mask name ") != 0)"
332 for (i = 0; i < n_extra_masks; i++) {
333 print "#define TARGET_" extra_masks[i] \
334 " ((target_flags & MASK_" extra_masks[i] ") != 0)"
338 for (i = 0; i < n_opts; i++) {
339 opt = opt_args("InverseMask", flags[i])
341 vname = var_name(flags[i])
343 mask = "OPTION_MASK_"
345 vname = "target_flags"
349 print "#define " macro nth_arg(1, opt) \
350 " ((" vname " & " mask nth_arg(0, opt) ") == 0)"
355 for (i = 0; i < n_langs; i++) {
356 macros[i] = "CL_" langs[i]
357 gsub( "[^" alnum "_]", "X", macros[i] )
358 s = substr(" ", length (macros[i]))
359 print "#define " macros[i] s " (1 << " i ")"
361 print "#define CL_LANG_ALL ((1 << " n_langs ") - 1)"
364 print "enum opt_code"
367 for (i = 0; i < n_opts; i++)
368 back_chain[i] = "N_OPTS";
371 for (i = 0; i < n_opts; i++) {
372 # Combine the flags of identical switches. Switches
373 # appear many times if they are handled by many front
375 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
376 flags[i + 1] = flags[i] " " flags[i + 1];
380 len = length (opts[i]);
381 enum = opt_enum(opts[i])
382 enum_string = enum " = " enum_value ","
384 # Aliases do not get enumeration names.
385 if ((flag_set_p("Alias.*", flags[i]) \
386 && !flag_set_p("SeparateAlias", flags[i])) \
387 || flag_set_p("Ignore", flags[i])) {
388 enum_string = "/* " enum_string " */"
391 # If this switch takes joined arguments, back-chain all
392 # subsequent switches to it for which it is a prefix. If
393 # a later switch S is a longer prefix of a switch T, T
394 # will be back-chained to S in a later iteration of this
395 # for() loop, which is what we want.
396 if (flag_set_p("Joined.*", flags[i])) {
397 for (j = i + 1; j < n_opts; j++) {
398 if (substr (opts[j], 1, len) != opts[i])
400 back_chain[j] = enum;
405 length (enum_string))
410 hlp = "N_(\"" help[i] "\")";
412 print " " enum_string s "/* -" opts[i] " */"
417 print " OPT_SPECIAL_unknown,"
418 print " OPT_SPECIAL_ignore,"
419 print " OPT_SPECIAL_program_name,"
420 print " OPT_SPECIAL_input_file"
423 print "#endif /* OPTIONS_H */"