2010-11-11 Jakub Jelinek <jakub@redhat.com>
[official-gcc.git] / gcc / opth-gen.awk
blob677225e1c1bf0f3bed076711dd2d9a24fa8d7a35
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
9 # later version.
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
22 # C header file.
24 # This program uses functions from opt-functions.awk
25 # Usage: awk -f opt-functions.awk -f opth-gen.awk < inputfile > options.h
27 BEGIN {
28 n_opts = 0
29 n_langs = 0
30 n_target_save = 0
31 n_extra_vars = 0
32 n_extra_masks = 0
33 FS=SUBSEP
36 # Collect the text and flags of each option into an array
38 if ($1 == "Language") {
39 langs[n_langs] = $2
40 n_langs++;
42 else if ($1 == "TargetSave") {
43 # Make sure the declarations are put in source order
44 target_save_decl[n_target_save] = $2
45 n_target_save++
47 else if ($1 == "Variable") {
48 extra_vars[n_extra_vars] = $2
49 n_extra_vars++
51 else {
52 name = opt_args("Mask", $1)
53 if (name == "") {
54 opts[n_opts] = $1
55 flags[n_opts] = $2
56 help[n_opts] = $3
57 n_opts++;
59 else {
60 extra_masks[n_extra_masks++] = name
65 # Dump out an enumeration into a .h file.
66 # Combine the flags of duplicate options.
67 END {
68 print "/* This file is auto-generated by opth-gen.awk. */"
69 print ""
70 print "#ifndef OPTIONS_H"
71 print "#define OPTIONS_H"
72 print ""
73 print "#include \"flag-types.h\""
74 print ""
76 have_save = 0;
78 print "#if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS) && !defined(IN_RTS)"
79 print "#ifndef GENERATOR_FILE"
80 print "struct gcc_options\n{"
81 print "#endif"
83 for (i = 0; i < n_extra_vars; i++) {
84 var = extra_vars[i]
85 sub(" *=.*", "", var)
86 orig_var = var
87 name = var
88 type = var
89 sub("^.*[ *]", "", name)
90 sub(" *" name "$", "", type)
91 var_seen[name] = 1
92 print "#ifdef GENERATOR_FILE"
93 print "extern " orig_var ";"
94 print "#else"
95 print " " type " x_" name ";"
96 print "#define " name " global_options.x_" name
97 print "#endif"
100 for (i = 0; i < n_opts; i++) {
101 if (flag_set_p("Save", flags[i]))
102 have_save = 1;
104 name = var_name(flags[i]);
105 if (name == "")
106 continue;
108 if (name in var_seen)
109 continue;
111 var_seen[name] = 1;
112 print "#ifdef GENERATOR_FILE"
113 print "extern " var_type(flags[i]) name ";"
114 print "#else"
115 print " " var_type(flags[i]) "x_" name ";"
116 print "#define " name " global_options.x_" name
117 print "#endif"
119 for (i = 0; i < n_opts; i++) {
120 name = static_var(opts[i], flags[i]);
121 if (name != "") {
122 print "#ifndef GENERATOR_FILE"
123 print " " var_type(flags[i]) "x_" name ";"
124 print "#define x_" name " do_not_use"
125 print "#endif"
128 print "#ifndef GENERATOR_FILE"
129 print "};"
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"
134 print "#endif"
135 print "#endif"
136 print ""
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)"
146 print ""
147 print "/* Structure to save/restore optimization and target specific options. */";
148 print "struct GTY(()) cl_optimization";
149 print "{";
151 n_opt_char = 2;
152 n_opt_short = 0;
153 n_opt_int = 0;
154 n_opt_other = 0;
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])
161 if(name == "")
162 continue;
164 if(name in var_opt_seen)
165 continue;
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;
178 else
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] ";";
199 print "};";
200 print "";
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";
205 print "{";
207 n_target_char = 0;
208 n_target_short = 0;
209 n_target_int = 0;
210 n_target_other = 0;
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];
222 else
223 var_target_other[n_target_other++] = target_save_decl[i];
226 if (have_save) {
227 for (i = 0; i < n_opts; i++) {
228 if (flag_set_p("Save", flags[i])) {
229 name = var_name(flags[i])
230 if(name == "")
231 name = "target_flags";
233 if(name in var_save_seen)
234 continue;
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;
247 else
248 var_target_other[n_target_other++] = otype "x_" name;
251 } else {
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] ";";
271 print "};";
272 print "";
273 print "";
274 print "/* Save optimization variables into a structure. */"
275 print "extern void cl_optimization_save (struct cl_optimization *, struct gcc_options *);";
276 print "";
277 print "/* Restore optimization variables from a structure. */";
278 print "extern void cl_optimization_restore (struct gcc_options *, struct cl_optimization *);";
279 print "";
280 print "/* Print optimization variables from a structure. */";
281 print "extern void cl_optimization_print (FILE *, int, struct cl_optimization *);";
282 print "";
283 print "/* Save selected option variables into a structure. */"
284 print "extern void cl_target_option_save (struct cl_target_option *, struct gcc_options *);";
285 print "";
286 print "/* Restore selected option variables from a structure. */"
287 print "extern void cl_target_option_restore (struct gcc_options *, struct cl_target_option *);";
288 print "";
289 print "/* Print target option variables from a structure. */";
290 print "extern void cl_target_option_print (FILE *, int, struct cl_target_option *);";
291 print "#endif";
292 print "";
294 for (i = 0; i < n_opts; i++) {
295 name = opt_args("Mask", flags[i])
296 vname = var_name(flags[i])
297 mask = "MASK_"
298 if (vname != "") {
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) {
310 if (var == "")
311 print "#error too many target masks"
312 else
313 print "#error too many masks for " var
316 print ""
318 for (i = 0; i < n_opts; i++) {
319 name = opt_args("Mask", flags[i])
320 vname = var_name(flags[i])
321 macro = "OPTION_"
322 mask = "OPTION_MASK_"
323 if (vname == "") {
324 vname = "target_flags"
325 macro = "TARGET_"
326 mask = "MASK_"
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)"
336 print ""
338 for (i = 0; i < n_opts; i++) {
339 opt = opt_args("InverseMask", flags[i])
340 if (opt ~ ",") {
341 vname = var_name(flags[i])
342 macro = "OPTION_"
343 mask = "OPTION_MASK_"
344 if (vname == "") {
345 vname = "target_flags"
346 macro = "TARGET_"
347 mask = "MASK_"
349 print "#define " macro nth_arg(1, opt) \
350 " ((" vname " & " mask nth_arg(0, opt) ") == 0)"
353 print ""
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)"
363 print ""
364 print "enum opt_code"
365 print "{"
367 for (i = 0; i < n_opts; i++)
368 back_chain[i] = "N_OPTS";
370 enum_value = 0
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
374 # ends, for example.
375 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
376 flags[i + 1] = flags[i] " " flags[i + 1];
377 i++;
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])
399 break;
400 back_chain[j] = enum;
404 s = substr(" ",
405 length (enum_string))
407 if (help[i] == "")
408 hlp = "0"
409 else
410 hlp = "N_(\"" help[i] "\")";
412 print " " enum_string s "/* -" opts[i] " */"
413 enum_value++
416 print " N_OPTS,"
417 print " OPT_SPECIAL_unknown,"
418 print " OPT_SPECIAL_ignore,"
419 print " OPT_SPECIAL_program_name,"
420 print " OPT_SPECIAL_input_file"
421 print "};"
422 print ""
423 print "#endif /* OPTIONS_H */"