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