options.texi (SeparateAlias): Document.
[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_masks = 0
32 FS=SUBSEP
33 }
34
35 # Collect the text and flags of each option into an array
36 {
37 if ($1 == "Language") {
38 langs[n_langs] = $2
39 n_langs++;
40 }
41 else if ($1 == "TargetSave") {
42 # Make sure the declarations are put in source order
43 target_save_decl[n_target_save] = $2
44 n_target_save++
45 }
46 else {
47 name = opt_args("Mask", $1)
48 if (name == "") {
49 opts[n_opts] = $1
50 flags[n_opts] = $2
51 help[n_opts] = $3
52 n_opts++;
53 }
54 else {
55 extra_masks[n_extra_masks++] = name
56 }
57 }
58 }
59
60 # Dump out an enumeration into a .h file.
61 # Combine the flags of duplicate options.
62 END {
63 print "/* This file is auto-generated by opth-gen.awk. */"
64 print ""
65 print "#ifndef OPTIONS_H"
66 print "#define OPTIONS_H"
67 print ""
68 print "extern int target_flags;"
69 print "extern int target_flags_explicit;"
70 print ""
71
72 have_save = 0;
73
74 for (i = 0; i < n_opts; i++) {
75 if (flag_set_p("Save", flags[i]))
76 have_save = 1;
77
78 name = var_name(flags[i]);
79 if (name == "")
80 continue;
81
82 if (name in var_seen)
83 continue;
84
85 var_seen[name] = 1;
86 print "extern " var_type(flags[i]) name ";"
87 }
88 print ""
89
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.
92
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.
96
97 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
98 print ""
99 print "/* Structure to save/restore optimization and target specific options. */";
100 print "struct GTY(()) cl_optimization";
101 print "{";
102
103 n_opt_char = 2;
104 n_opt_short = 0;
105 n_opt_int = 0;
106 n_opt_other = 0;
107 var_opt_char[0] = "unsigned char optimize";
108 var_opt_char[1] = "unsigned char optimize_size";
109
110 for (i = 0; i < n_opts; i++) {
111 if (flag_set_p("Optimization", flags[i])) {
112 name = var_name(flags[i])
113 if(name == "")
114 continue;
115
116 if(name in var_opt_seen)
117 continue;
118
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;
123
124 else if (otype ~ "^((un)?signed +)?short *$")
125 var_opt_short[n_opt_short++] = otype name;
126
127 else if (otype ~ "^((un)?signed +)?char *$")
128 var_opt_char[n_opt_char++] = otype name;
129
130 else
131 var_opt_other[n_opt_other++] = otype name;
132 }
133 }
134
135 for (i = 0; i < n_opt_other; i++) {
136 print " " var_opt_other[i] ";";
137 }
138
139 for (i = 0; i < n_opt_int; i++) {
140 print " " var_opt_int[i] ";";
141 }
142
143 for (i = 0; i < n_opt_short; i++) {
144 print " " var_opt_short[i] ";";
145 }
146
147 for (i = 0; i < n_opt_char; i++) {
148 print " " var_opt_char[i] ";";
149 }
150
151 print "};";
152 print "";
153
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";
157 print "{";
158
159 n_target_char = 0;
160 n_target_short = 0;
161 n_target_int = 0;
162 n_target_other = 0;
163
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];
167
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];
170
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];
173
174 else
175 var_target_other[n_target_other++] = target_save_decl[i];
176 }
177
178 if (have_save) {
179 for (i = 0; i < n_opts; i++) {
180 if (flag_set_p("Save", flags[i])) {
181 name = var_name(flags[i])
182 if(name == "")
183 name = "target_flags";
184
185 if(name in var_save_seen)
186 continue;
187
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;
192
193 else if (otype ~ "^((un)?signed +)?short *$")
194 var_target_short[n_target_short++] = otype name;
195
196 else if (otype ~ "^((un)?signed +)?char *$")
197 var_target_char[n_target_char++] = otype name;
198
199 else
200 var_target_other[n_target_other++] = otype name;
201 }
202 }
203 } else {
204 var_target_int[n_target_int++] = "int target_flags";
205 }
206
207 for (i = 0; i < n_target_other; i++) {
208 print " " var_target_other[i] ";";
209 }
210
211 for (i = 0; i < n_target_int; i++) {
212 print " " var_target_int[i] ";";
213 }
214
215 for (i = 0; i < n_target_short; i++) {
216 print " " var_target_short[i] ";";
217 }
218
219 for (i = 0; i < n_target_char; i++) {
220 print " " var_target_char[i] ";";
221 }
222
223 print "};";
224 print "";
225 print "";
226 print "/* Save optimization variables into a structure. */"
227 print "extern void cl_optimization_save (struct cl_optimization *);";
228 print "";
229 print "/* Restore optimization variables from a structure. */";
230 print "extern void cl_optimization_restore (struct cl_optimization *);";
231 print "";
232 print "/* Print optimization variables from a structure. */";
233 print "extern void cl_optimization_print (FILE *, int, struct cl_optimization *);";
234 print "";
235 print "/* Save selected option variables into a structure. */"
236 print "extern void cl_target_option_save (struct cl_target_option *);";
237 print "";
238 print "/* Restore selected option variables from a structure. */"
239 print "extern void cl_target_option_restore (struct cl_target_option *);";
240 print "";
241 print "/* Print target option variables from a structure. */";
242 print "extern void cl_target_option_print (FILE *, int, struct cl_target_option *);";
243 print "#endif";
244 print "";
245
246 for (i = 0; i < n_opts; i++) {
247 name = opt_args("Mask", flags[i])
248 vname = var_name(flags[i])
249 mask = "MASK_"
250 if (vname != "") {
251 mask = "OPTION_MASK_"
252 }
253 if (name != "" && !flag_set_p("MaskExists", flags[i]))
254 print "#define " mask name " (1 << " masknum[vname]++ ")"
255 }
256 for (i = 0; i < n_extra_masks; i++) {
257 print "#define MASK_" extra_masks[i] " (1 << " masknum[""]++ ")"
258 }
259
260 for (var in masknum) {
261 if (masknum[var] > 31) {
262 if (var == "")
263 print "#error too many target masks"
264 else
265 print "#error too many masks for " var
266 }
267 }
268 print ""
269
270 for (i = 0; i < n_opts; i++) {
271 name = opt_args("Mask", flags[i])
272 vname = var_name(flags[i])
273 macro = "OPTION_"
274 mask = "OPTION_MASK_"
275 if (vname == "") {
276 vname = "target_flags"
277 macro = "TARGET_"
278 mask = "MASK_"
279 }
280 if (name != "" && !flag_set_p("MaskExists", flags[i]))
281 print "#define " macro name \
282 " ((" vname " & " mask name ") != 0)"
283 }
284 for (i = 0; i < n_extra_masks; i++) {
285 print "#define TARGET_" extra_masks[i] \
286 " ((target_flags & MASK_" extra_masks[i] ") != 0)"
287 }
288 print ""
289
290 for (i = 0; i < n_opts; i++) {
291 opt = opt_args("InverseMask", flags[i])
292 if (opt ~ ",") {
293 vname = var_name(flags[i])
294 macro = "OPTION_"
295 mask = "OPTION_MASK_"
296 if (vname == "") {
297 vname = "target_flags"
298 macro = "TARGET_"
299 mask = "MASK_"
300 }
301 print "#define " macro nth_arg(1, opt) \
302 " ((" vname " & " mask nth_arg(0, opt) ") == 0)"
303 }
304 }
305 print ""
306
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 ")"
312 }
313 print "#define CL_LANG_ALL ((1 << " n_langs ") - 1)"
314
315 print ""
316 print "enum opt_code"
317 print "{"
318
319 for (i = 0; i < n_opts; i++)
320 back_chain[i] = "N_OPTS";
321
322 enum_value = 0
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
326 # ends, for example.
327 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
328 flags[i + 1] = flags[i] " " flags[i + 1];
329 i++;
330 }
331
332 len = length (opts[i]);
333 enum = opt_enum(opts[i])
334 enum_string = enum " = " enum_value ","
335
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 " */"
341 }
342
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])
351 break;
352 back_chain[j] = enum;
353 }
354 }
355
356 s = substr(" ",
357 length (enum_string))
358
359 if (help[i] == "")
360 hlp = "0"
361 else
362 hlp = "N_(\"" help[i] "\")";
363
364 print " " enum_string s "/* -" opts[i] " */"
365 enum_value++
366 }
367
368 print " N_OPTS,"
369 print " OPT_SPECIAL_unknown,"
370 print " OPT_SPECIAL_ignore,"
371 print " OPT_SPECIAL_program_name,"
372 print " OPT_SPECIAL_input_file"
373 print "};"
374 print ""
375 print "#endif /* OPTIONS_H */"
376 }