common.opt (debug_struct_ordinary, [...]): New Variable entries.
[gcc.git] / gcc / opth-gen.awk
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.
5 #
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.
10 #
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.
15 #
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/>.
19
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.
23 #
24 # This program uses functions from opt-functions.awk
25 # Usage: awk -f opt-functions.awk -f opth-gen.awk < inputfile > options.h
26
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
34 }
35
36 # Collect the text and flags of each option into an array
37 {
38 if ($1 == "Language") {
39 langs[n_langs] = $2
40 n_langs++;
41 }
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++
46 }
47 else if ($1 == "Variable") {
48 extra_vars[n_extra_vars] = $2
49 n_extra_vars++
50 }
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++;
58 }
59 else {
60 extra_masks[n_extra_masks++] = name
61 }
62 }
63 }
64
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 ""
75
76 have_save = 0;
77
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"
82
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 type_after = var
90 sub("^.*[ *]", "", name)
91 sub("\\[.*\\]$", "", name)
92 sub("\\[.*\\]$", "", type)
93 sub(" *" name "$", "", type)
94 sub("^.*" name, "", type_after)
95 var_seen[name] = 1
96 print "#ifdef GENERATOR_FILE"
97 print "extern " orig_var ";"
98 print "#else"
99 print " " type " x_" name type_after ";"
100 print "#define " name " global_options.x_" name
101 print "#endif"
102 }
103
104 for (i = 0; i < n_opts; i++) {
105 if (flag_set_p("Save", flags[i]))
106 have_save = 1;
107
108 name = var_name(flags[i]);
109 if (name == "")
110 continue;
111
112 if (name in var_seen)
113 continue;
114
115 var_seen[name] = 1;
116 print "#ifdef GENERATOR_FILE"
117 print "extern " var_type(flags[i]) name ";"
118 print "#else"
119 print " " var_type(flags[i]) "x_" name ";"
120 print "#define " name " global_options.x_" name
121 print "#endif"
122 }
123 for (i = 0; i < n_opts; i++) {
124 name = static_var(opts[i], flags[i]);
125 if (name != "") {
126 print "#ifndef GENERATOR_FILE"
127 print " " var_type(flags[i]) "x_" name ";"
128 print "#define x_" name " do_not_use"
129 print "#endif"
130 }
131 }
132 print "#ifndef GENERATOR_FILE"
133 print "};"
134 print "extern struct gcc_options global_options;"
135 print "extern const struct gcc_options global_options_init;"
136 print "extern struct gcc_options global_options_set;"
137 print "#define target_flags_explicit global_options_set.x_target_flags"
138 print "#endif"
139 print "#endif"
140 print ""
141
142 # All of the optimization switches gathered together so they can be saved and restored.
143 # This will allow attribute((cold)) to turn on space optimization.
144
145 # Change the type of normal switches from int to unsigned char to save space.
146 # Also, order the structure so that pointer fields occur first, then int
147 # fields, and then char fields to provide the best packing.
148
149 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
150 print ""
151 print "/* Structure to save/restore optimization and target specific options. */";
152 print "struct GTY(()) cl_optimization";
153 print "{";
154
155 n_opt_char = 2;
156 n_opt_short = 0;
157 n_opt_int = 0;
158 n_opt_other = 0;
159 var_opt_char[0] = "unsigned char x_optimize";
160 var_opt_char[1] = "unsigned char x_optimize_size";
161
162 for (i = 0; i < n_opts; i++) {
163 if (flag_set_p("Optimization", flags[i])) {
164 name = var_name(flags[i])
165 if(name == "")
166 continue;
167
168 if(name in var_opt_seen)
169 continue;
170
171 var_opt_seen[name]++;
172 otype = var_type_struct(flags[i]);
173 if (otype ~ "^((un)?signed +)?int *$")
174 var_opt_int[n_opt_int++] = otype "x_" name;
175
176 else if (otype ~ "^((un)?signed +)?short *$")
177 var_opt_short[n_opt_short++] = otype "x_" name;
178
179 else if (otype ~ "^((un)?signed +)?char *$")
180 var_opt_char[n_opt_char++] = otype "x_" name;
181
182 else
183 var_opt_other[n_opt_other++] = otype "x_" name;
184 }
185 }
186
187 for (i = 0; i < n_opt_other; i++) {
188 print " " var_opt_other[i] ";";
189 }
190
191 for (i = 0; i < n_opt_int; i++) {
192 print " " var_opt_int[i] ";";
193 }
194
195 for (i = 0; i < n_opt_short; i++) {
196 print " " var_opt_short[i] ";";
197 }
198
199 for (i = 0; i < n_opt_char; i++) {
200 print " " var_opt_char[i] ";";
201 }
202
203 print "};";
204 print "";
205
206 # Target and optimization save/restore/print functions.
207 print "/* Structure to save/restore selected target specific options. */";
208 print "struct GTY(()) cl_target_option";
209 print "{";
210
211 n_target_char = 0;
212 n_target_short = 0;
213 n_target_int = 0;
214 n_target_other = 0;
215
216 for (i = 0; i < n_target_save; i++) {
217 if (target_save_decl[i] ~ "^((un)?signed +)?int +[_" alnum "]+$")
218 var_target_int[n_target_int++] = target_save_decl[i];
219
220 else if (target_save_decl[i] ~ "^((un)?signed +)?short +[_" alnum "]+$")
221 var_target_short[n_target_short++] = target_save_decl[i];
222
223 else if (target_save_decl[i] ~ "^((un)?signed +)?char +[_ " alnum "]+$")
224 var_target_char[n_target_char++] = target_save_decl[i];
225
226 else
227 var_target_other[n_target_other++] = target_save_decl[i];
228 }
229
230 if (have_save) {
231 for (i = 0; i < n_opts; i++) {
232 if (flag_set_p("Save", flags[i])) {
233 name = var_name(flags[i])
234 if(name == "")
235 name = "target_flags";
236
237 if(name in var_save_seen)
238 continue;
239
240 var_save_seen[name]++;
241 otype = var_type_struct(flags[i])
242 if (otype ~ "^((un)?signed +)?int *$")
243 var_target_int[n_target_int++] = otype "x_" name;
244
245 else if (otype ~ "^((un)?signed +)?short *$")
246 var_target_short[n_target_short++] = otype "x_" name;
247
248 else if (otype ~ "^((un)?signed +)?char *$")
249 var_target_char[n_target_char++] = otype "x_" name;
250
251 else
252 var_target_other[n_target_other++] = otype "x_" name;
253 }
254 }
255 } else {
256 var_target_int[n_target_int++] = "int x_target_flags";
257 }
258
259 for (i = 0; i < n_target_other; i++) {
260 print " " var_target_other[i] ";";
261 }
262
263 for (i = 0; i < n_target_int; i++) {
264 print " " var_target_int[i] ";";
265 }
266
267 for (i = 0; i < n_target_short; i++) {
268 print " " var_target_short[i] ";";
269 }
270
271 for (i = 0; i < n_target_char; i++) {
272 print " " var_target_char[i] ";";
273 }
274
275 print "};";
276 print "";
277 print "";
278 print "/* Save optimization variables into a structure. */"
279 print "extern void cl_optimization_save (struct cl_optimization *, struct gcc_options *);";
280 print "";
281 print "/* Restore optimization variables from a structure. */";
282 print "extern void cl_optimization_restore (struct gcc_options *, struct cl_optimization *);";
283 print "";
284 print "/* Print optimization variables from a structure. */";
285 print "extern void cl_optimization_print (FILE *, int, struct cl_optimization *);";
286 print "";
287 print "/* Save selected option variables into a structure. */"
288 print "extern void cl_target_option_save (struct cl_target_option *, struct gcc_options *);";
289 print "";
290 print "/* Restore selected option variables from a structure. */"
291 print "extern void cl_target_option_restore (struct gcc_options *, struct cl_target_option *);";
292 print "";
293 print "/* Print target option variables from a structure. */";
294 print "extern void cl_target_option_print (FILE *, int, struct cl_target_option *);";
295 print "#endif";
296 print "";
297
298 for (i = 0; i < n_opts; i++) {
299 name = opt_args("Mask", flags[i])
300 vname = var_name(flags[i])
301 mask = "MASK_"
302 if (vname != "") {
303 mask = "OPTION_MASK_"
304 }
305 if (name != "" && !flag_set_p("MaskExists", flags[i]))
306 print "#define " mask name " (1 << " masknum[vname]++ ")"
307 }
308 for (i = 0; i < n_extra_masks; i++) {
309 print "#define MASK_" extra_masks[i] " (1 << " masknum[""]++ ")"
310 }
311
312 for (var in masknum) {
313 if (masknum[var] > 31) {
314 if (var == "")
315 print "#error too many target masks"
316 else
317 print "#error too many masks for " var
318 }
319 }
320 print ""
321
322 for (i = 0; i < n_opts; i++) {
323 name = opt_args("Mask", flags[i])
324 vname = var_name(flags[i])
325 macro = "OPTION_"
326 mask = "OPTION_MASK_"
327 if (vname == "") {
328 vname = "target_flags"
329 macro = "TARGET_"
330 mask = "MASK_"
331 }
332 if (name != "" && !flag_set_p("MaskExists", flags[i]))
333 print "#define " macro name \
334 " ((" vname " & " mask name ") != 0)"
335 }
336 for (i = 0; i < n_extra_masks; i++) {
337 print "#define TARGET_" extra_masks[i] \
338 " ((target_flags & MASK_" extra_masks[i] ") != 0)"
339 }
340 print ""
341
342 for (i = 0; i < n_opts; i++) {
343 opt = opt_args("InverseMask", flags[i])
344 if (opt ~ ",") {
345 vname = var_name(flags[i])
346 macro = "OPTION_"
347 mask = "OPTION_MASK_"
348 if (vname == "") {
349 vname = "target_flags"
350 macro = "TARGET_"
351 mask = "MASK_"
352 }
353 print "#define " macro nth_arg(1, opt) \
354 " ((" vname " & " mask nth_arg(0, opt) ") == 0)"
355 }
356 }
357 print ""
358
359 for (i = 0; i < n_langs; i++) {
360 macros[i] = "CL_" langs[i]
361 gsub( "[^" alnum "_]", "X", macros[i] )
362 s = substr(" ", length (macros[i]))
363 print "#define " macros[i] s " (1 << " i ")"
364 }
365 print "#define CL_LANG_ALL ((1 << " n_langs ") - 1)"
366
367 print ""
368 print "enum opt_code"
369 print "{"
370
371 for (i = 0; i < n_opts; i++)
372 back_chain[i] = "N_OPTS";
373
374 enum_value = 0
375 for (i = 0; i < n_opts; i++) {
376 # Combine the flags of identical switches. Switches
377 # appear many times if they are handled by many front
378 # ends, for example.
379 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
380 flags[i + 1] = flags[i] " " flags[i + 1];
381 i++;
382 }
383
384 len = length (opts[i]);
385 enum = opt_enum(opts[i])
386 enum_string = enum " = " enum_value ","
387
388 # Aliases do not get enumeration names.
389 if ((flag_set_p("Alias.*", flags[i]) \
390 && !flag_set_p("SeparateAlias", flags[i])) \
391 || flag_set_p("Ignore", flags[i])) {
392 enum_string = "/* " enum_string " */"
393 }
394
395 # If this switch takes joined arguments, back-chain all
396 # subsequent switches to it for which it is a prefix. If
397 # a later switch S is a longer prefix of a switch T, T
398 # will be back-chained to S in a later iteration of this
399 # for() loop, which is what we want.
400 if (flag_set_p("Joined.*", flags[i])) {
401 for (j = i + 1; j < n_opts; j++) {
402 if (substr (opts[j], 1, len) != opts[i])
403 break;
404 back_chain[j] = enum;
405 }
406 }
407
408 s = substr(" ",
409 length (enum_string))
410
411 if (help[i] == "")
412 hlp = "0"
413 else
414 hlp = "N_(\"" help[i] "\")";
415
416 print " " enum_string s "/* -" opts[i] " */"
417 enum_value++
418 }
419
420 print " N_OPTS,"
421 print " OPT_SPECIAL_unknown,"
422 print " OPT_SPECIAL_ignore,"
423 print " OPT_SPECIAL_program_name,"
424 print " OPT_SPECIAL_input_file"
425 print "};"
426 print ""
427 print "#endif /* OPTIONS_H */"
428 }