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