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
32 n_extra_target_vars =
0
34 n_extra_c_includes =
0
35 n_extra_h_includes =
0
41 # Collect the text and flags of each option into an array
43 if ($
1 ==
"Language") {
47 else if ($
1 ==
"TargetSave") {
48 # Make sure the declarations are put in source order
49 target_save_decl
[n_target_save
] = $
2
52 else if ($
1 ==
"Variable") {
53 extra_vars
[n_extra_vars
] = $
2
56 else if ($
1 ==
"TargetVariable") {
57 # Combination of TargetSave and Variable
58 extra_vars
[n_extra_vars
] = $
2
66 sub("^.*[ *]", "", name
)
67 sub(" *" name
"$", "", type
)
68 target_save_decl
[n_target_save
] = type
" x_" name
71 extra_target_vars
[n_extra_target_vars
] = name
74 else if ($
1 ==
"HeaderInclude") {
75 extra_h_includes
[n_extra_h_includes
++] = $
2;
77 else if ($
1 ==
"SourceInclude") {
78 extra_c_includes
[n_extra_c_includes
++] = $
2;
80 else if ($
1 ==
"Enum") {
82 name = opt_args
("Name", props
)
83 type = opt_args
("Type", props
)
84 unknown_error = opt_args
("UnknownError", props
)
85 enum_names
[n_enums
] = name
86 enum_type
[name
] = type
87 enum_index
[name
] = n_enums
88 enum_unknown_error
[name
] = unknown_error
92 else if ($
1 ==
"EnumValue") {
94 enum_name = opt_args
("Enum", props
)
95 string = opt_args
("String", props
)
96 value = opt_args
("Value", props
)
98 val_flags = val_flags \
99 test_flag
("Canonical", props
, "| CL_ENUM_CANONICAL") \
100 test_flag
("DriverOnly", props
, "| CL_ENUM_DRIVER_ONLY")
101 enum_data
[enum_name
] = enum_data
[enum_name
] \
102 " { " quote string quote
", " value
", " val_flags \
106 name = opt_args
("Mask", $
1)
114 extra_masks
[n_extra_masks
++] = name
119 # Dump out an enumeration into a .h file.
120 # Combine the flags of duplicate options.
122 print "/* This file is auto-generated by opth-gen.awk. */"
124 print "#ifndef OPTIONS_H"
125 print "#define OPTIONS_H"
127 print "#include \"flag-types.h\""
130 if (n_extra_h_includes
> 0) {
131 for (i =
0; i
< n_extra_h_includes
; i
++) {
132 print "#include " quote extra_h_includes
[i
] quote
137 print "#if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS) && !defined(IN_RTS)"
138 print "#ifndef GENERATOR_FILE"
139 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
140 print "struct GTY(()) gcc_options"
142 print "struct gcc_options"
147 for (i =
0; i
< n_extra_vars
; i
++) {
149 sub(" *=.*", "", var
)
154 sub("^.*[ *]", "", name
)
155 sub("\\[.*\\]$", "", name
)
156 sub("\\[.*\\]$", "", type
)
157 sub(" *" name
"$", "", type
)
158 sub("^.*" name
, "", type_after
)
160 print "#ifdef GENERATOR_FILE"
161 print "extern " orig_var
";"
163 print " " type
" x_" name type_after
";"
164 print "#define " name
" global_options.x_" name
168 for (i =
0; i
< n_opts
; i
++) {
169 if (flag_set_p
("Save", flags
[i
]))
172 name = var_name
(flags
[i
]);
176 if (name in var_seen
)
180 print "#ifdef GENERATOR_FILE"
181 print "extern " var_type
(flags
[i
]) name
";"
183 print " " var_type
(flags
[i
]) "x_" name
";"
184 print "#define " name
" global_options.x_" name
187 for (i =
0; i
< n_opts
; i
++) {
188 name = static_var
(opts
[i
], flags
[i
]);
190 print "#ifndef GENERATOR_FILE"
191 print " " var_type
(flags
[i
]) "x_" name
";"
192 print "#define x_" name
" do_not_use"
196 print "#ifndef GENERATOR_FILE"
198 print "extern struct gcc_options global_options;"
199 print "extern const struct gcc_options global_options_init;"
200 print "extern struct gcc_options global_options_set;"
201 print "#define target_flags_explicit global_options_set.x_target_flags"
206 # All of the optimization switches gathered together so they can be saved and restored.
207 # This will allow attribute((cold)) to turn on space optimization.
209 # Change the type of normal switches from int to unsigned char to save space.
210 # Also, order the structure so that pointer fields occur first, then int
211 # fields, and then char fields to provide the best packing.
213 print "#if !defined(GCC_DRIVER) && !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS)"
215 print "/* Structure to save/restore optimization and target specific options. */";
216 print "struct GTY(()) cl_optimization";
224 var_opt_char
[0] =
"unsigned char x_optimize";
225 var_opt_char
[1] =
"unsigned char x_optimize_size";
226 var_opt_enum
[0] =
"enum fp_contract_mode x_flag_fp_contract_mode";
228 for (i =
0; i
< n_opts
; i
++) {
229 if (flag_set_p
("Optimization", flags
[i
])) {
230 name = var_name
(flags
[i
])
234 if(name in var_opt_seen
)
237 var_opt_seen
[name
]++;
238 otype = var_type_struct
(flags
[i
]);
239 if (otype ~
"^((un)?signed +)?int *$")
240 var_opt_int
[n_opt_int
++] = otype
"x_" name
;
242 else if (otype ~
"^((un)?signed +)?short *$")
243 var_opt_short
[n_opt_short
++] = otype
"x_" name
;
245 else if (otype ~
"^((un)?signed +)?char *$")
246 var_opt_char
[n_opt_char
++] = otype
"x_" name
;
248 else if (otype ~
("^enum +[_" alnum
"]+ *$"))
249 var_opt_enum
[n_opt_enum
++] = otype
"x_" name
;
252 var_opt_other
[n_opt_other
++] = otype
"x_" name
;
256 for (i =
0; i
< n_opt_other
; i
++) {
257 print " " var_opt_other
[i
] ";";
260 for (i =
0; i
< n_opt_int
; i
++) {
261 print " " var_opt_int
[i
] ";";
264 for (i =
0; i
< n_opt_enum
; i
++) {
265 print " " var_opt_enum
[i
] ";";
268 for (i =
0; i
< n_opt_short
; i
++) {
269 print " " var_opt_short
[i
] ";";
272 for (i =
0; i
< n_opt_char
; i
++) {
273 print " " var_opt_char
[i
] ";";
279 # Target and optimization save/restore/print functions.
280 print "/* Structure to save/restore selected target specific options. */";
281 print "struct GTY(()) cl_target_option";
290 for (i =
0; i
< n_target_save
; i
++) {
291 if (target_save_decl
[i
] ~
"^((un)?signed +)?int +[_" alnum
"]+$")
292 var_target_int
[n_target_int
++] = target_save_decl
[i
];
294 else if (target_save_decl
[i
] ~
"^((un)?signed +)?short +[_" alnum
"]+$")
295 var_target_short
[n_target_short
++] = target_save_decl
[i
];
297 else if (target_save_decl
[i
] ~
"^((un)?signed +)?char +[_ " alnum
"]+$")
298 var_target_char
[n_target_char
++] = target_save_decl
[i
];
300 else if (target_save_decl
[i
] ~
("^enum +[_" alnum
"]+ +[_" alnum
"]+$")) {
301 var_target_enum
[n_target_enum
++] = target_save_decl
[i
];
304 var_target_other
[n_target_other
++] = target_save_decl
[i
];
308 for (i =
0; i
< n_opts
; i
++) {
309 if (flag_set_p
("Save", flags
[i
])) {
310 name = var_name
(flags
[i
])
312 name =
"target_flags";
314 if(name in var_save_seen
)
317 var_save_seen
[name
]++;
318 otype = var_type_struct
(flags
[i
])
319 if (otype ~
"^((un)?signed +)?int *$")
320 var_target_int
[n_target_int
++] = otype
"x_" name
;
322 else if (otype ~
"^((un)?signed +)?short *$")
323 var_target_short
[n_target_short
++] = otype
"x_" name
;
325 else if (otype ~
"^((un)?signed +)?char *$")
326 var_target_char
[n_target_char
++] = otype
"x_" name
;
328 else if (otype ~
("^enum +[_" alnum
"]+ +[_" alnum
"]+"))
329 var_target_enum
[n_target_enum
++] = otype
"x_" name
;
332 var_target_other
[n_target_other
++] = otype
"x_" name
;
336 var_target_int
[n_target_int
++] =
"int x_target_flags";
339 for (i =
0; i
< n_target_other
; i
++) {
340 print " " var_target_other
[i
] ";";
343 for (i =
0; i
< n_target_enum
; i
++) {
344 print " " var_target_enum
[i
] ";";
347 for (i =
0; i
< n_target_int
; i
++) {
348 print " " var_target_int
[i
] ";";
351 for (i =
0; i
< n_target_short
; i
++) {
352 print " " var_target_short
[i
] ";";
355 for (i =
0; i
< n_target_char
; i
++) {
356 print " " var_target_char
[i
] ";";
362 print "/* Save optimization variables into a structure. */"
363 print "extern void cl_optimization_save (struct cl_optimization *, struct gcc_options *);";
365 print "/* Restore optimization variables from a structure. */";
366 print "extern void cl_optimization_restore (struct gcc_options *, struct cl_optimization *);";
368 print "/* Print optimization variables from a structure. */";
369 print "extern void cl_optimization_print (FILE *, int, struct cl_optimization *);";
371 print "/* Save selected option variables into a structure. */"
372 print "extern void cl_target_option_save (struct cl_target_option *, struct gcc_options *);";
374 print "/* Restore selected option variables from a structure. */"
375 print "extern void cl_target_option_restore (struct gcc_options *, struct cl_target_option *);";
377 print "/* Print target option variables from a structure. */";
378 print "extern void cl_target_option_print (FILE *, int, struct cl_target_option *);";
382 for (i =
0; i
< n_opts
; i
++) {
383 name = opt_args
("Mask", flags
[i
])
384 vname = var_name
(flags
[i
])
387 mask =
"OPTION_MASK_"
389 if (name
!= "" && !flag_set_p
("MaskExists", flags
[i
]))
390 print "#define " mask name
" (1 << " masknum
[vname
]++ ")"
392 for (i =
0; i
< n_extra_masks
; i
++) {
393 print "#define MASK_" extra_masks
[i
] " (1 << " masknum
[""]++ ")"
396 for (var in masknum
) {
397 if (masknum
[var
] > 31) {
399 print "#error too many target masks"
401 print "#error too many masks for " var
406 for (i =
0; i
< n_opts
; i
++) {
407 name = opt_args
("Mask", flags
[i
])
408 vname = var_name
(flags
[i
])
410 mask =
"OPTION_MASK_"
412 vname =
"target_flags"
416 if (name
!= "" && !flag_set_p
("MaskExists", flags
[i
]))
417 print "#define " macro name \
418 " ((" vname
" & " mask name
") != 0)"
420 for (i =
0; i
< n_extra_masks
; i
++) {
421 print "#define TARGET_" extra_masks
[i
] \
422 " ((target_flags & MASK_" extra_masks
[i
] ") != 0)"
426 for (i =
0; i
< n_opts
; i
++) {
427 opt = opt_args
("InverseMask", flags
[i
])
429 vname = var_name
(flags
[i
])
431 mask =
"OPTION_MASK_"
433 vname =
"target_flags"
437 print "#define " macro nth_arg
(1, opt
) \
438 " ((" vname
" & " mask nth_arg
(0, opt
) ") == 0)"
443 for (i =
0; i
< n_langs
; i
++) {
444 macros
[i
] =
"CL_" langs
[i
]
445 gsub( "[^" alnum
"_]", "X", macros
[i
] )
446 s =
substr(" ", length (macros
[i
]))
447 print "#define " macros
[i
] s
" (1 << " i
")"
449 print "#define CL_LANG_ALL ((1 << " n_langs
") - 1)"
452 print "enum opt_code"
455 for (i =
0; i
< n_opts
; i
++)
456 back_chain
[i
] =
"N_OPTS";
459 for (i =
0; i
< n_opts
; i
++) {
460 # Combine the flags of identical switches. Switches
461 # appear many times if they are handled by many front
463 while( i
+ 1 != n_opts
&& opts
[i
] == opts
[i
+ 1] ) {
464 flags
[i
+ 1] = flags
[i
] " " flags
[i
+ 1];
468 len =
length (opts
[i
]);
469 enum = opt_enum
(opts
[i
])
470 enum_string = enum
" = " enum_value
","
472 # Aliases do not get enumeration names.
473 if ((flag_set_p
("Alias.*", flags
[i
]) \
474 && !flag_set_p
("SeparateAlias", flags
[i
])) \
475 || flag_set_p
("Ignore", flags
[i
])) {
476 enum_string =
"/* " enum_string
" */"
479 # If this switch takes joined arguments, back-chain all
480 # subsequent switches to it for which it is a prefix. If
481 # a later switch S is a longer prefix of a switch T, T
482 # will be back-chained to S in a later iteration of this
483 # for() loop, which is what we want.
484 if (flag_set_p
("Joined.*", flags
[i
])) {
485 for (j = i
+ 1; j
< n_opts
; j
++) {
486 if (substr (opts
[j
], 1, len
) != opts
[i
])
488 back_chain
[j
] = enum
;
493 length (enum_string
))
498 hlp =
"N_(\"" help
[i
] "\")";
500 print " " enum_string s
"/* -" opts
[i
] " */"
505 print " OPT_SPECIAL_unknown,"
506 print " OPT_SPECIAL_ignore,"
507 print " OPT_SPECIAL_program_name,"
508 print " OPT_SPECIAL_input_file"
511 print "#endif /* OPTIONS_H */"