gcc/
[official-gcc.git] / gcc / opth-gen.awk
blobcf2ef9683c842bcf11f354f387afa4fefb728683
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.
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.
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.
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/>.
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.
24 # This program uses functions from opt-functions.awk
25 # Usage: awk -f opt-functions.awk -f opth-gen.awk < inputfile > options.h
27 BEGIN {
28 n_opts = 0
29 n_langs = 0
30 n_target_save = 0
31 n_extra_masks = 0
32 quote = "\042"
33 comma = ","
34 FS=SUBSEP
37 # Collect the text and flags of each option into an array
39 if ($1 == "Language") {
40 langs[n_langs] = $2
41 n_langs++;
43 else if ($1 == "TargetSave") {
44 # Make sure the declarations are put in source order
45 target_save_decl[n_target_save] = $2
46 n_target_save++
48 else {
49 name = opt_args("Mask", $1)
50 if (name == "") {
51 opts[n_opts] = $1
52 flags[n_opts] = $2
53 help[n_opts] = $3
54 n_opts++;
56 else {
57 extra_masks[n_extra_masks++] = name
62 # Dump out an enumeration into a .h file.
63 # Combine the flags of duplicate options.
64 END {
65 print "/* This file is auto-generated by opth-gen.awk. */"
66 print ""
67 print "#ifndef OPTIONS_H"
68 print "#define OPTIONS_H"
69 print ""
70 print "extern int target_flags;"
71 print "extern int target_flags_explicit;"
72 print ""
74 have_save = 0;
76 for (i = 0; i < n_opts; i++) {
77 if (flag_set_p("Save", flags[i]))
78 have_save = 1;
80 name = var_name(flags[i]);
81 if (name == "")
82 continue;
84 if (name in var_seen)
85 continue;
87 var_seen[name] = 1;
88 print "extern " var_type(flags[i]) name ";"
90 print ""
92 # All of the optimization switches gathered together so they can be saved and restored.
93 # This will allow attribute((cold)) to turn on space optimization.
95 # Change the type of normal switches from int to unsigned char to save space.
96 # Also, order the structure so that pointer fields occur first, then int
97 # fields, and then char fields to provide the best packing.
99 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
100 print ""
101 print "/* Structure to save/restore optimization and target specific options. */";
102 print "struct GTY(()) cl_optimization";
103 print "{";
105 n_opt_char = 2;
106 n_opt_short = 0;
107 n_opt_int = 0;
108 n_opt_other = 0;
109 var_opt_char[0] = "unsigned char optimize";
110 var_opt_char[1] = "unsigned char optimize_size";
112 for (i = 0; i < n_opts; i++) {
113 if (flag_set_p("Optimization", flags[i])) {
114 name = var_name(flags[i])
115 if(name == "")
116 continue;
118 if(name in var_opt_seen)
119 continue;
121 var_opt_seen[name]++;
122 otype = var_type_struct(flags[i]);
123 if (otype ~ "^((un)?signed +)?int *$")
124 var_opt_int[n_opt_int++] = otype name;
126 else if (otype ~ "^((un)?signed +)?short *$")
127 var_opt_short[n_opt_short++] = otype name;
129 else if (otype ~ "^((un)?signed +)?char *$")
130 var_opt_char[n_opt_char++] = otype name;
132 else
133 var_opt_other[n_opt_other++] = otype name;
137 for (i = 0; i < n_opt_other; i++) {
138 print " " var_opt_other[i] ";";
141 for (i = 0; i < n_opt_int; i++) {
142 print " " var_opt_int[i] ";";
145 for (i = 0; i < n_opt_short; i++) {
146 print " " var_opt_short[i] ";";
149 for (i = 0; i < n_opt_char; i++) {
150 print " " var_opt_char[i] ";";
153 print "};";
154 print "";
156 # Target and optimization save/restore/print functions.
157 print "/* Structure to save/restore selected target specific options. */";
158 print "struct GTY(()) cl_target_option";
159 print "{";
161 n_target_char = 0;
162 n_target_short = 0;
163 n_target_int = 0;
164 n_target_other = 0;
166 for (i = 0; i < n_target_save; i++) {
167 if (target_save_decl[i] ~ "^((un)?signed +)?int +[_a-zA-Z0-9]+$")
168 var_target_int[n_target_int++] = target_save_decl[i];
170 else if (target_save_decl[i] ~ "^((un)?signed +)?short +[_a-zA-Z0-9]+$")
171 var_target_short[n_target_short++] = target_save_decl[i];
173 else if (target_save_decl[i] ~ "^((un)?signed +)?char +[_a-zA-Z0-9]+$")
174 var_target_char[n_target_char++] = target_save_decl[i];
176 else
177 var_target_other[n_target_other++] = target_save_decl[i];
180 if (have_save) {
181 for (i = 0; i < n_opts; i++) {
182 if (flag_set_p("Save", flags[i])) {
183 name = var_name(flags[i])
184 if(name == "")
185 name = "target_flags";
187 if(name in var_save_seen)
188 continue;
190 var_save_seen[name]++;
191 otype = var_type_struct(flags[i])
192 if (otype ~ "^((un)?signed +)?int *$")
193 var_target_int[n_target_int++] = otype name;
195 else if (otype ~ "^((un)?signed +)?short *$")
196 var_target_short[n_target_short++] = otype name;
198 else if (otype ~ "^((un)?signed +)?char *$")
199 var_target_char[n_target_char++] = otype name;
201 else
202 var_target_other[n_target_other++] = otype name;
205 } else {
206 var_target_int[n_target_int++] = "int target_flags";
209 for (i = 0; i < n_target_other; i++) {
210 print " " var_target_other[i] ";";
213 for (i = 0; i < n_target_int; i++) {
214 print " " var_target_int[i] ";";
217 for (i = 0; i < n_target_short; i++) {
218 print " " var_target_short[i] ";";
221 for (i = 0; i < n_target_char; i++) {
222 print " " var_target_char[i] ";";
225 print "};";
226 print "";
227 print "";
228 print "/* Save optimization variables into a structure. */"
229 print "extern void cl_optimization_save (struct cl_optimization *);";
230 print "";
231 print "/* Restore optimization variables from a structure. */";
232 print "extern void cl_optimization_restore (struct cl_optimization *);";
233 print "";
234 print "/* Print optimization variables from a structure. */";
235 print "extern void cl_optimization_print (FILE *, int, struct cl_optimization *);";
236 print "";
237 print "/* Save selected option variables into a structure. */"
238 print "extern void cl_target_option_save (struct cl_target_option *);";
239 print "";
240 print "/* Restore selected option variables from a structure. */"
241 print "extern void cl_target_option_restore (struct cl_target_option *);";
242 print "";
243 print "/* Print target option variables from a structure. */";
244 print "extern void cl_target_option_print (FILE *, int, struct cl_target_option *);";
245 print "#endif";
246 print "";
248 for (i = 0; i < n_opts; i++) {
249 name = opt_args("Mask", flags[i])
250 vname = var_name(flags[i])
251 mask = "MASK_"
252 if (vname != "") {
253 mask = "OPTION_MASK_"
255 if (name != "" && !flag_set_p("MaskExists", flags[i]))
256 print "#define " mask name " (1 << " masknum[vname]++ ")"
258 for (i = 0; i < n_extra_masks; i++) {
259 print "#define MASK_" extra_masks[i] " (1 << " masknum[""]++ ")"
262 for (var in masknum) {
263 if (masknum[var] > 31) {
264 if (var == "")
265 print "#error too many target masks"
266 else
267 print "#error too many masks for " var
270 print ""
272 for (i = 0; i < n_opts; i++) {
273 name = opt_args("Mask", flags[i])
274 vname = var_name(flags[i])
275 macro = "OPTION_"
276 mask = "OPTION_MASK_"
277 if (vname == "") {
278 vname = "target_flags"
279 macro = "TARGET_"
280 mask = "MASK_"
282 if (name != "" && !flag_set_p("MaskExists", flags[i]))
283 print "#define " macro name \
284 " ((" vname " & " mask name ") != 0)"
286 for (i = 0; i < n_extra_masks; i++) {
287 print "#define TARGET_" extra_masks[i] \
288 " ((target_flags & MASK_" extra_masks[i] ") != 0)"
290 print ""
292 for (i = 0; i < n_opts; i++) {
293 opt = opt_args("InverseMask", flags[i])
294 if (opt ~ ",") {
295 vname = var_name(flags[i])
296 macro = "OPTION_"
297 mask = "OPTION_MASK_"
298 if (vname == "") {
299 vname = "target_flags"
300 macro = "TARGET_"
301 mask = "MASK_"
303 print "#define " macro nth_arg(1, opt) \
304 " ((" vname " & " mask nth_arg(0, opt) ") == 0)"
307 print ""
309 for (i = 0; i < n_langs; i++) {
310 macros[i] = "CL_" langs[i]
311 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
312 s = substr(" ", length (macros[i]))
313 print "#define " macros[i] s " (1 << " i ")"
315 print "#define CL_LANG_ALL ((1 << " n_langs ") - 1)"
317 print ""
318 print "enum opt_code"
319 print "{"
321 for (i = 0; i < n_opts; i++)
322 back_chain[i] = "N_OPTS";
324 for (i = 0; i < n_opts; i++) {
325 # Combine the flags of identical switches. Switches
326 # appear many times if they are handled by many front
327 # ends, for example.
328 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
329 flags[i + 1] = flags[i] " " flags[i + 1];
330 i++;
333 len = length (opts[i]);
334 enum = opt_enum(opts[i])
336 # If this switch takes joined arguments, back-chain all
337 # subsequent switches to it for which it is a prefix. If
338 # a later switch S is a longer prefix of a switch T, T
339 # will be back-chained to S in a later iteration of this
340 # for() loop, which is what we want.
341 if (flag_set_p("Joined.*", flags[i])) {
342 for (j = i + 1; j < n_opts; j++) {
343 if (substr (opts[j], 1, len) != opts[i])
344 break;
345 back_chain[j] = enum;
349 s = substr(" ", length (enum))
350 if (i + 1 == n_opts)
351 comma = ""
353 if (help[i] == "")
354 hlp = "0"
355 else
356 hlp = "N_(\"" help[i] "\")";
358 print " " enum "," s "/* -" opts[i] " */"
361 print " N_OPTS"
362 print "};"
363 print ""
364 print "#endif /* OPTIONS_H */"