1 # Copyright (C) 2003-2024 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 # If FLAGS includes a UrlSuffix flag, return the value it specifies.
197 # Return the empty string otherwise.
198 function url_suffix
(flags
)
200 return nth_arg
(0, opt_args
("UrlSuffix", flags
))
203 # If FLAGS includes a LangUrlSuffix_LANG flag, return the
204 # value it specifies.
205 # Return the empty string otherwise.
206 function lang_url_suffix
(flags
, lang
)
208 return nth_arg
(0, opt_args
("LangUrlSuffix_" lang
, flags
))
211 # Return the name of the variable if FLAGS has a HOST_WIDE_INT variable.
212 # Return the empty string otherwise.
213 function host_wide_int_var_name
(flags
)
215 split (flags
, array
, "[ \t]+")
216 if (array
[1] ==
"HOST_WIDE_INT")
222 # Return true if the option described by FLAGS has a globally-visible state.
223 function global_state_p
(flags
)
225 return (var_name
(flags
) != "" \
226 || opt_args
("Mask", flags
) != "" \
227 || opt_args
("InverseMask", flags
) != "")
230 # Return true if the option described by FLAGS must have some state
231 # associated with it.
232 function needs_state_p
(flags
)
234 return (flag_set_p
("Target", flags
) \
235 && !flag_set_p
("Alias.*", flags
) \
236 && !flag_set_p
("Ignore", flags
))
239 # If FLAGS describes an option that needs state without a public
240 # variable name, return the name of that field, minus the initial
241 # "x_", otherwise return "". NAME is the name of the option.
242 function static_var
(name
, flags
)
244 if (global_state_p
(flags
) || !needs_state_p
(flags
))
246 gsub ("[^" alnum
"]", "_", name
)
250 # Return the type of variable that should be associated with the given flags.
251 function var_type
(flags
)
253 if (flag_set_p
("Defer", flags
))
255 else if (flag_set_p
("Enum.*", flags
)) {
256 en = opt_args
("Enum", flags
);
257 return enum_type
[en
] " "
259 else if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
))
261 else if (flag_set_p
("Host_Wide_Int", flags
))
262 return "HOST_WIDE_INT "
263 else if (flag_set_p
("UInteger", flags
))
266 return "const char *"
269 # Return the type of variable that should be associated with the given flags
270 # for use within a structure. Simple variables are changed to signed char
271 # type instead of int to save space.
272 function var_type_struct
(flags
)
274 if (flag_set_p
("UInteger", flags
)) {
275 if (host_wide_int
[var_name
(flags
)] ==
"yes")
276 return "HOST_WIDE_INT ";
277 if (flag_set_p
("ByteSize", flags
))
278 return "HOST_WIDE_INT "
281 else if (flag_set_p
("Enum.*", flags
)) {
282 en = opt_args
("Enum", flags
);
283 return enum_type
[en
] " "
285 else if (!flag_set_p
("Joined.*", flags
) && !flag_set_p
("Separate", flags
)) {
286 if (flag_set_p
(".*Mask.*", flags
)) {
287 if (host_wide_int
[var_name
(flags
)] ==
"yes")
288 return "HOST_WIDE_INT "
290 return "/* - */ int "
293 return "signed char "
296 return "const char *"
299 # Given that an option has flags FLAGS, return an initializer for the
300 # "var_enum", "var_type" and "var_value" fields of its cl_options[] entry.
301 function var_set
(flags
)
303 if (flag_set_p
("Defer", flags
))
304 return "0, CLVC_DEFER, 0"
305 s = nth_arg
(1, opt_args
("Var", flags
))
307 return "0, CLVC_EQUAL, " s
308 s = opt_args
("Mask", flags
);
310 vn = var_name
(flags
);
312 return "0, CLVC_BIT_SET, OPTION_MASK_" s
314 return "0, CLVC_BIT_SET, MASK_" s
316 s = nth_arg
(0, opt_args
("InverseMask", flags
));
318 vn = var_name
(flags
);
320 return "0, CLVC_BIT_CLEAR, OPTION_MASK_" s
322 return "0, CLVC_BIT_CLEAR, MASK_" s
324 if (flag_set_p
("Enum.*", flags
)) {
325 en = opt_args
("Enum", flags
);
326 if (flag_set_p
("EnumSet", flags
))
327 return enum_index
[en
] ", CLVC_ENUM, CLEV_SET"
328 else if (flag_set_p
("EnumBitSet", flags
))
329 return enum_index
[en
] ", CLVC_ENUM, CLEV_BITSET"
331 return enum_index
[en
] ", CLVC_ENUM, CLEV_NORMAL"
333 if (var_type
(flags
) ==
"const char *")
334 return "0, CLVC_STRING, 0"
335 if (flag_set_p
("ByteSize", flags
))
336 return "0, CLVC_SIZE, 0"
337 return "0, CLVC_INTEGER, 0"
340 # Given that an option called NAME has flags FLAGS, return an initializer
341 # for the "flag_var" field of its cl_options[] entry.
342 function var_ref
(name
, flags
)
344 name = var_name
(flags
) static_var
(name
, flags
)
346 return "offsetof (struct gcc_options, x_" name
")"
347 if (opt_args
("Mask", flags
) != "")
348 return "offsetof (struct gcc_options, x_target_flags)"
349 if (opt_args
("InverseMask", flags
) != "")
350 return "offsetof (struct gcc_options, x_target_flags)"
351 return "(unsigned short) -1"
354 # Given the option called NAME return a sanitized version of its name.
355 function opt_sanitized_name
(name
)
357 gsub ("[^" alnum
"]", "_", name
)
361 # Given the option called NAME return the appropriate enum for it.
362 function opt_enum
(name
)
364 return "OPT_" opt_sanitized_name
(name
)
367 # Given the language called NAME return a sanitized version of its name.
368 function lang_sanitized_name
(name
)
370 gsub( "[^" alnum
"_]", "X", name
)
374 # Search for a valid var_name among all OPTS equal to option NAME.
375 # If not found, return "".
376 function search_var_name
(name
, opt_numbers
, opts
, flags
, n_opts
)
378 opt_var_name = var_name
(flags
[opt_numbers
[name
]]);
379 if (opt_var_name
!= "") {
382 for (k =
0; k
< n_opts
; k
++) {
383 if (opts
[k
] == name
&& var_name
(flags
[k
]) != "") {
384 return var_name
(flags
[k
]);
390 function integer_range_info
(range_option
, init
, option
, uinteger_used
)
392 if (range_option
!= "") {
394 start = nth_arg
(0, range_option
) + 0;
395 end = nth_arg
(1, range_option
) + 0;
396 if (init
!= "" && init
!= "-1" && (ival
< start
|| ival
> end))
397 print "#error initial value " init
" of '" option
"' must be in range [" start
"," end "]"
398 if (uinteger_used
&& start
< 0)
399 print "#error '" option
"': negative IntegerRange (" start
", " end ") cannot be combined with UInteger"
400 return start
", " end
406 # Find the index of VAR in VAR_ARRY which as length N_VAR_ARRY. If
407 # VAR is not found, return N_VAR_ARRY. That means the var is a new
409 function find_index
(var
, var_arry
, n_var_arry
)
411 for (var_index =
0; var_index
< n_var_arry
; var_index
++)
413 if (var_arry
[var_index
] == var
)