1 # Copyright (C) 2003-2023 Free Software Foundation, Inc.
2 # Contributed by Kelley Cook, June 2004.
3 # Original code from Neil Booth, May 2003.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; see the file COPYING3. If not see
17 # <http://www.gnu.org/licenses/>.
19 # Some common subroutines for use by opt[ch]-gen.awk.
21 # Define some helpful character classes, for portability.
23 lower =
"abcdefghijklmnopqrstuvwxyz"
24 upper =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26 alnum = lower
"" upper
"" digit
29 # Return nonzero if FLAGS contains a flag matching REGEX.
30 function flag_set_p
(regex
, flags
)
32 # Ignore the arguments of flags with arguments.
33 gsub ("\\([^)]+\\)", "", flags
);
34 return (" " flags
" ") ~
(" " regex
" ")
37 # Return STRING if FLAGS contains a flag matching regexp REGEX,
38 # otherwise return the empty string.
39 function test_flag
(regex
, flags
, string
)
41 if (flag_set_p
(regex
, flags
))
46 # Return a field initializer, with trailing comma, for a field that is
47 # 1 if FLAGS contains a flag matching REGEX and 0 otherwise.
48 function flag_init
(regex
, flags
)
50 if (flag_set_p
(regex
, flags
))
51 return "1 /* " regex
" */, "
56 # If FLAGS contains a "NAME(...argument...)" flag, return the value
57 # of the argument. Return the empty string otherwise.
58 function opt_args
(name
, flags
)
61 if (flags !~
" " name
"\\(")
63 sub(".* " name
"\\(", "", flags
)
66 sub ("^[{]", "", flags
)
67 sub ("}\\).*", "", flags
)
70 sub("\\).*", "", flags
)
75 # If FLAGS contains a "NAME(...argument...)" flag, return the value
76 # of the argument. Print error message otherwise.
77 function opt_args_non_empty
(name
, flags
, description
)
79 args = opt_args
(name
, flags
)
81 print "#error Empty option argument '" name
"' during parsing of: " flags
85 # Return the number of comma-separated element of S.
91 sub("[^,]*, *", "", s
)
96 # Return the Nth comma-separated element of S. Return the empty string
97 # if S does not contain N elements.
98 function nth_arg
(n
, s
)
103 sub("[^,]*, *", "", s
)
109 # Return a bitmask of CL_* values for option flags FLAGS.
110 function switch_flags
(flags
)
113 for (j =
0; j
< n_langs
; j
++) {
115 gsub ( "\\+", "\\+", regex
)
116 result = result test_flag
(regex
, flags
, " | " macros
[j
])
119 test_flag
("Common", flags
, " | CL_COMMON") \
120 test_flag
("Target", flags
, " | CL_TARGET") \
121 test_flag
("PchIgnore", flags
, " | CL_PCH_IGNORE") \
122 test_flag
("Driver", flags
, " | CL_DRIVER") \
123 test_flag
("Joined", flags
, " | CL_JOINED") \
124 test_flag
("JoinedOrMissing", flags
, " | CL_JOINED") \
125 test_flag
("Separate", flags
, " | CL_SEPARATE") \
126 test_flag
("Undocumented", flags
, " | CL_UNDOCUMENTED") \
127 test_flag
("NoDWARFRecord", flags
, " | CL_NO_DWARF_RECORD") \
128 test_flag
("Warning", flags
, " | CL_WARNING") \
129 test_flag
("(Optimization|PerFunction)", flags
, " | CL_OPTIMIZATION") \
130 test_flag
("Param", flags
, " | CL_PARAMS")
131 sub( "^0 \\| ", "", result
)
135 # Return bit-field initializers for option flags FLAGS.
136 function switch_bit_fields
(flags
)
139 vn = var_name
(flags
);
140 if (host_wide_int
[vn
] ==
"yes")
141 hwi =
"Host_Wide_Int"
142 else if (flag_set_p
("Host_Wide_Int", flags
)) {
143 hwi =
"Host_Wide_Int"
144 uinteger_flag = flag_init
("UInteger", flags
)
149 sep_args = opt_args
("Args", flags
)
154 result = result sep_args
", "
156 if (uinteger_flag ==
"")
157 uinteger_flag = flag_init
("UInteger", flags
)
159 hwi_flag = flag_init
("Host_Wide_Int", hwi
)
160 byte_size_flag = flag_init
("ByteSize", flags
)
162 if (substr(byte_size_flag
, 1, 1) != "0" \
163 && substr(uinteger_flag
, 1, 1) ==
"0" \
164 && substr(hwi_flag
, 1, 1) ==
"0")
165 print "#error only UInteger amd Host_Wide_Int options can specify a ByteSize suffix"
167 # The following flags need to be in the same order as
168 # the corresponding members of struct cl_option defined
171 flag_init
("SeparateAlias", flags
) \
172 flag_init
("NegativeAlias", flags
) \
173 flag_init
("NoDriverArg", flags
) \
174 flag_init
("RejectDriver", flags
) \
175 flag_init
("RejectNegative", flags
) \
176 flag_init
("JoinedOrMissing", flags
) \
179 flag_init
("ToLower", flags
) \
182 if (flag_set_p
("Report", flags
))
183 print "#error Report option property is dropped"
185 sub(", $", "", result
)
189 # If FLAGS includes a Var flag, return the name of the variable it specifies.
190 # Return the empty string otherwise.
191 function var_name
(flags
)
193 return nth_arg
(0, opt_args
("Var", flags
))
196 # Return the name of the variable if FLAGS has a HOST_WIDE_INT variable.
197 # Return the empty string otherwise.
198 function host_wide_int_var_name
(flags
)
200 split (flags
, array
, "[ \t]+")
201 if (array
[1] ==
"HOST_WIDE_INT")
207 # Return true if the option described by FLAGS has a globally-visible state.
208 function global_state_p
(flags
)
210 return (var_name
(flags
) != "" \
211 || opt_args
("Mask", flags
) != "" \
212 || opt_args
("InverseMask", flags
) != "")
215 # Return true if the option described by FLAGS must have some state
216 # associated with it.
217 function needs_state_p
(flags
)
219 return (flag_set_p
("Target", flags
) \
220 && !flag_set_p
("Alias.*", flags
) \
221 && !flag_set_p
("Ignore", flags
))
224 # If FLAGS describes an option that needs state without a public
225 # variable name, return the name of that field, minus the initial
226 # "x_", otherwise return "". NAME is the name of the option.
227 function static_var
(name
, flags
)
229 if (global_state_p
(flags
) || !needs_state_p
(flags
))
231 gsub ("[^" alnum
"]", "_", name
)
235 # Return the type of variable that should be associated with the given flags.
236 function var_type
(flags
)
238 if (flag_set_p
("Defer", flags
))
240 else if (flag_set_p
("Enum.*", flags
)) {
241 en = opt_args
("Enum", flags
);
242 return enum_type
[en
] " "
244 else if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
))
246 else if (flag_set_p
("Host_Wide_Int", flags
))
247 return "HOST_WIDE_INT "
248 else if (flag_set_p
("UInteger", flags
))
251 return "const char *"
254 # Return the type of variable that should be associated with the given flags
255 # for use within a structure. Simple variables are changed to signed char
256 # type instead of int to save space.
257 function var_type_struct
(flags
)
259 if (flag_set_p
("UInteger", flags
)) {
260 if (host_wide_int
[var_name
(flags
)] ==
"yes")
261 return "HOST_WIDE_INT ";
262 if (flag_set_p
("ByteSize", flags
))
263 return "HOST_WIDE_INT "
266 else if (flag_set_p
("Enum.*", flags
)) {
267 en = opt_args
("Enum", flags
);
268 return enum_type
[en
] " "
270 else if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
)) {
271 if (flag_set_p
(".*Mask.*", flags
)) {
272 if (host_wide_int
[var_name
(flags
)] ==
"yes")
273 return "HOST_WIDE_INT "
275 return "/* - */ int "
278 return "signed char "
281 return "const char *"
284 # Given that an option has flags FLAGS, return an initializer for the
285 # "var_enum", "var_type" and "var_value" fields of its cl_options[] entry.
286 function var_set
(flags
)
288 if (flag_set_p
("Defer", flags
))
289 return "0, CLVC_DEFER, 0"
290 s = nth_arg
(1, opt_args
("Var", flags
))
292 return "0, CLVC_EQUAL, " s
293 s = opt_args
("Mask", flags
);
295 vn = var_name
(flags
);
297 return "0, CLVC_BIT_SET, OPTION_MASK_" s
299 return "0, CLVC_BIT_SET, MASK_" s
301 s = nth_arg
(0, opt_args
("InverseMask", flags
));
303 vn = var_name
(flags
);
305 return "0, CLVC_BIT_CLEAR, OPTION_MASK_" s
307 return "0, CLVC_BIT_CLEAR, MASK_" s
309 if (flag_set_p
("Enum.*", flags
)) {
310 en = opt_args
("Enum", flags
);
311 if (flag_set_p
("EnumSet", flags
))
312 return enum_index
[en
] ", CLVC_ENUM, CLEV_SET"
313 else if (flag_set_p
("EnumBitSet", flags
))
314 return enum_index
[en
] ", CLVC_ENUM, CLEV_BITSET"
316 return enum_index
[en
] ", CLVC_ENUM, CLEV_NORMAL"
318 if (var_type
(flags
) ==
"const char *")
319 return "0, CLVC_STRING, 0"
320 if (flag_set_p
("ByteSize", flags
))
321 return "0, CLVC_SIZE, 0"
322 return "0, CLVC_INTEGER, 0"
325 # Given that an option called NAME has flags FLAGS, return an initializer
326 # for the "flag_var" field of its cl_options[] entry.
327 function var_ref
(name
, flags
)
329 name = var_name
(flags
) static_var
(name
, flags
)
331 return "offsetof (struct gcc_options, x_" name
")"
332 if (opt_args
("Mask", flags
) != "")
333 return "offsetof (struct gcc_options, x_target_flags)"
334 if (opt_args
("InverseMask", flags
) != "")
335 return "offsetof (struct gcc_options, x_target_flags)"
336 return "(unsigned short) -1"
339 # Given the option called NAME return a sanitized version of its name.
340 function opt_sanitized_name
(name
)
342 gsub ("[^" alnum
"]", "_", name
)
346 # Given the option called NAME return the appropriate enum for it.
347 function opt_enum
(name
)
349 return "OPT_" opt_sanitized_name
(name
)
352 # Given the language called NAME return a sanitized version of its name.
353 function lang_sanitized_name
(name
)
355 gsub( "[^" alnum
"_]", "X", name
)
359 # Search for a valid var_name among all OPTS equal to option NAME.
360 # If not found, return "".
361 function search_var_name
(name
, opt_numbers
, opts
, flags
, n_opts
)
363 opt_var_name = var_name
(flags
[opt_numbers
[name
]]);
364 if (opt_var_name
!= "") {
367 for (k =
0; k
< n_opts
; k
++) {
368 if (opts
[k
] == name
&& var_name
(flags
[k
]) != "") {
369 return var_name
(flags
[k
]);
375 function integer_range_info
(range_option
, init
, option
, uinteger_used
)
377 if (range_option
!= "") {
379 start = nth_arg
(0, range_option
) + 0;
380 end = nth_arg
(1, range_option
) + 0;
381 if (init
!= "" && init
!= "-1" && (ival
< start
|| ival
> end))
382 print "#error initial value " init
" of '" option
"' must be in range [" start
"," end "]"
383 if (uinteger_used
&& start
< 0)
384 print "#error '" option
"': negative IntegerRange (" start
", " end ") cannot be combined with UInteger"
385 return start
", " end
391 # Find the index of VAR in VAR_ARRY which as length N_VAR_ARRY. If
392 # VAR is not found, return N_VAR_ARRY. That means the var is a new
394 function find_index
(var
, var_arry
, n_var_arry
)
396 for (var_index =
0; var_index
< n_var_arry
; var_index
++)
398 if (var_arry
[var_index
] == var
)