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