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
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
24 # This program uses functions from opt-functions.awk
25 # Usage: awk -f opt-functions.awk -f opth-gen.awk < inputfile > options.h
36 # Collect the text and flags of each option into an array
38 if ($
1 ==
"Language") {
42 else if ($
1 ==
"TargetSave") {
43 # Make sure the declarations are put in source order
44 target_save_decl
[n_target_save
] = $
2
47 else if ($
1 ==
"Variable") {
48 extra_vars
[n_extra_vars
] = $
2
52 name = opt_args
("Mask", $
1)
60 extra_masks
[n_extra_masks
++] = name
65 # Dump out an enumeration into a .h file.
66 # Combine the flags of duplicate options.
68 print "/* This file is auto-generated by opth-gen.awk. */"
70 print "#ifndef OPTIONS_H"
71 print "#define OPTIONS_H"
76 print "#if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS) && !defined(IN_RTS)"
77 print "#ifndef GENERATOR_FILE"
78 print "struct gcc_options\n{"
81 for (i =
0; i
< n_extra_vars
; i
++) {
87 sub("^.*[ *]", "", name
)
88 sub(" *" name
"$", "", type
)
90 print "#ifdef GENERATOR_FILE"
91 print "extern " orig_var
";"
93 print " " type
" x_" name
";"
94 print "#define " name
" global_options.x_" name
98 for (i =
0; i
< n_opts
; i
++) {
99 if (flag_set_p
("Save", flags
[i
]))
102 name = var_name
(flags
[i
]);
106 if (name in var_seen
)
110 print "#ifdef GENERATOR_FILE"
111 print "extern " var_type
(flags
[i
]) name
";"
113 print " " var_type
(flags
[i
]) "x_" name
";"
114 print "#define " name
" global_options.x_" name
117 for (i =
0; i
< n_opts
; i
++) {
118 name = static_var
(opts
[i
], flags
[i
]);
120 print "#ifndef GENERATOR_FILE"
121 print " " var_type
(flags
[i
]) "x_" name
";"
122 print "#define x_" name
" do_not_use"
126 print "#ifndef GENERATOR_FILE"
128 print "extern struct gcc_options global_options;"
129 print "extern const struct gcc_options global_options_init;"
130 print "extern struct gcc_options global_options_set;"
131 print "#define target_flags_explicit global_options_set.x_target_flags"
136 # All of the optimization switches gathered together so they can be saved and restored.
137 # This will allow attribute((cold)) to turn on space optimization.
139 # Change the type of normal switches from int to unsigned char to save space.
140 # Also, order the structure so that pointer fields occur first, then int
141 # fields, and then char fields to provide the best packing.
143 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
145 print "/* Structure to save/restore optimization and target specific options. */";
146 print "struct GTY(()) cl_optimization";
153 var_opt_char
[0] =
"unsigned char x_optimize";
154 var_opt_char
[1] =
"unsigned char x_optimize_size";
156 for (i =
0; i
< n_opts
; i
++) {
157 if (flag_set_p
("Optimization", flags
[i
])) {
158 name = var_name
(flags
[i
])
162 if(name in var_opt_seen
)
165 var_opt_seen
[name
]++;
166 otype = var_type_struct
(flags
[i
]);
167 if (otype ~
"^((un)?signed +)?int *$")
168 var_opt_int
[n_opt_int
++] = otype
"x_" name
;
170 else if (otype ~
"^((un)?signed +)?short *$")
171 var_opt_short
[n_opt_short
++] = otype
"x_" name
;
173 else if (otype ~
"^((un)?signed +)?char *$")
174 var_opt_char
[n_opt_char
++] = otype
"x_" name
;
177 var_opt_other
[n_opt_other
++] = otype
"x_" name
;
181 for (i =
0; i
< n_opt_other
; i
++) {
182 print " " var_opt_other
[i
] ";";
185 for (i =
0; i
< n_opt_int
; i
++) {
186 print " " var_opt_int
[i
] ";";
189 for (i =
0; i
< n_opt_short
; i
++) {
190 print " " var_opt_short
[i
] ";";
193 for (i =
0; i
< n_opt_char
; i
++) {
194 print " " var_opt_char
[i
] ";";
200 # Target and optimization save/restore/print functions.
201 print "/* Structure to save/restore selected target specific options. */";
202 print "struct GTY(()) cl_target_option";
210 for (i =
0; i
< n_target_save
; i
++) {
211 if (target_save_decl
[i
] ~
"^((un)?signed +)?int +[_" alnum
"]+$")
212 var_target_int
[n_target_int
++] = target_save_decl
[i
];
214 else if (target_save_decl
[i
] ~
"^((un)?signed +)?short +[_" alnum
"]+$")
215 var_target_short
[n_target_short
++] = target_save_decl
[i
];
217 else if (target_save_decl
[i
] ~
"^((un)?signed +)?char +[_ " alnum
"]+$")
218 var_target_char
[n_target_char
++] = target_save_decl
[i
];
221 var_target_other
[n_target_other
++] = target_save_decl
[i
];
225 for (i =
0; i
< n_opts
; i
++) {
226 if (flag_set_p
("Save", flags
[i
])) {
227 name = var_name
(flags
[i
])
229 name =
"target_flags";
231 if(name in var_save_seen
)
234 var_save_seen
[name
]++;
235 otype = var_type_struct
(flags
[i
])
236 if (otype ~
"^((un)?signed +)?int *$")
237 var_target_int
[n_target_int
++] = otype
"x_" name
;
239 else if (otype ~
"^((un)?signed +)?short *$")
240 var_target_short
[n_target_short
++] = otype
"x_" name
;
242 else if (otype ~
"^((un)?signed +)?char *$")
243 var_target_char
[n_target_char
++] = otype
"x_" name
;
246 var_target_other
[n_target_other
++] = otype
"x_" name
;
250 var_target_int
[n_target_int
++] =
"int x_target_flags";
253 for (i =
0; i
< n_target_other
; i
++) {
254 print " " var_target_other
[i
] ";";
257 for (i =
0; i
< n_target_int
; i
++) {
258 print " " var_target_int
[i
] ";";
261 for (i =
0; i
< n_target_short
; i
++) {
262 print " " var_target_short
[i
] ";";
265 for (i =
0; i
< n_target_char
; i
++) {
266 print " " var_target_char
[i
] ";";
272 print "/* Save optimization variables into a structure. */"
273 print "extern void cl_optimization_save (struct cl_optimization *, struct gcc_options *);";
275 print "/* Restore optimization variables from a structure. */";
276 print "extern void cl_optimization_restore (struct gcc_options *, struct cl_optimization *);";
278 print "/* Print optimization variables from a structure. */";
279 print "extern void cl_optimization_print (FILE *, int, struct cl_optimization *);";
281 print "/* Save selected option variables into a structure. */"
282 print "extern void cl_target_option_save (struct cl_target_option *, struct gcc_options *);";
284 print "/* Restore selected option variables from a structure. */"
285 print "extern void cl_target_option_restore (struct gcc_options *, struct cl_target_option *);";
287 print "/* Print target option variables from a structure. */";
288 print "extern void cl_target_option_print (FILE *, int, struct cl_target_option *);";
292 for (i =
0; i
< n_opts
; i
++) {
293 name = opt_args
("Mask", flags
[i
])
294 vname = var_name
(flags
[i
])
297 mask =
"OPTION_MASK_"
299 if (name
!= "" && !flag_set_p
("MaskExists", flags
[i
]))
300 print "#define " mask name
" (1 << " masknum
[vname
]++ ")"
302 for (i =
0; i
< n_extra_masks
; i
++) {
303 print "#define MASK_" extra_masks
[i
] " (1 << " masknum
[""]++ ")"
306 for (var in masknum
) {
307 if (masknum
[var
] > 31) {
309 print "#error too many target masks"
311 print "#error too many masks for " var
316 for (i =
0; i
< n_opts
; i
++) {
317 name = opt_args
("Mask", flags
[i
])
318 vname = var_name
(flags
[i
])
320 mask =
"OPTION_MASK_"
322 vname =
"target_flags"
326 if (name
!= "" && !flag_set_p
("MaskExists", flags
[i
]))
327 print "#define " macro name \
328 " ((" vname
" & " mask name
") != 0)"
330 for (i =
0; i
< n_extra_masks
; i
++) {
331 print "#define TARGET_" extra_masks
[i
] \
332 " ((target_flags & MASK_" extra_masks
[i
] ") != 0)"
336 for (i =
0; i
< n_opts
; i
++) {
337 opt = opt_args
("InverseMask", flags
[i
])
339 vname = var_name
(flags
[i
])
341 mask =
"OPTION_MASK_"
343 vname =
"target_flags"
347 print "#define " macro nth_arg
(1, opt
) \
348 " ((" vname
" & " mask nth_arg
(0, opt
) ") == 0)"
353 for (i =
0; i
< n_langs
; i
++) {
354 macros
[i
] =
"CL_" langs
[i
]
355 gsub( "[^" alnum
"_]", "X", macros
[i
] )
356 s =
substr(" ", length (macros
[i
]))
357 print "#define " macros
[i
] s
" (1 << " i
")"
359 print "#define CL_LANG_ALL ((1 << " n_langs
") - 1)"
362 print "enum opt_code"
365 for (i =
0; i
< n_opts
; i
++)
366 back_chain
[i
] =
"N_OPTS";
369 for (i =
0; i
< n_opts
; i
++) {
370 # Combine the flags of identical switches. Switches
371 # appear many times if they are handled by many front
373 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
374 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
378 len =
length (opts
[i
]);
379 enum = opt_enum
(opts
[i
])
380 enum_string = enum
" = " enum_value
","
382 # Aliases do not get enumeration names.
383 if ((flag_set_p
("Alias.*", flags
[i
]) \
384 && !flag_set_p
("SeparateAlias", flags
[i
])) \
385 || flag_set_p
("Ignore", flags
[i
])) {
386 enum_string =
"/* " enum_string
" */"
389 # If this switch takes joined arguments, back-chain all
390 # subsequent switches to it for which it is a prefix. If
391 # a later switch S is a longer prefix of a switch T, T
392 # will be back-chained to S in a later iteration of this
393 # for() loop, which is what we want.
394 if (flag_set_p
("Joined.*", flags
[i
])) {
395 for (j = i
+ 1; j
< n_opts
; j
++) {
396 if (substr (opts
[j
], 1, len
) != opts
[i
])
398 back_chain
[j
] = enum
;
403 length (enum_string
))
408 hlp =
"N_(\"" help
[i
] "\")";
410 print " " enum_string s
"/* -" opts
[i
] " */"
415 print " OPT_SPECIAL_unknown,"
416 print " OPT_SPECIAL_ignore,"
417 print " OPT_SPECIAL_program_name,"
418 print " OPT_SPECIAL_input_file"
421 print "#endif /* OPTIONS_H */"