| 1 |
# Copyright (C) 2003-2025 Free Software Foundation, Inc. |
| 2 |
# Contributed by Kelley Cook, June 2004. |
| 3 |
# Original code from Neil Booth, May 2003. |
| 4 |
# |
| 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 |
| 8 |
# later version. |
| 9 |
# |
| 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. |
| 14 |
# |
| 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/>. |
| 18 |
|
| 19 |
# Some common subroutines for use by opt[ch]-gen.awk. |
| 20 |
|
| 21 |
# Define some helpful character classes, for portability. |
| 22 |
BEGIN { |
| 23 |
lower = "abcdefghijklmnopqrstuvwxyz" |
| 24 |
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 25 |
digit = "0123456789" |
| 26 |
alnum = lower "" upper "" digit |
| 27 |
} |
| 28 |
|
| 29 |
# Return nonzero if FLAGS contains a flag matching REGEX. |
| 30 |
function flag_set_p(regex, flags) |
| 31 |
{ |
| 32 |
# Ignore the arguments of flags with arguments. |
| 33 |
gsub ("\\([^)]+\\)", "", flags); |
| 34 |
return (" " flags " ") ~ (" " regex " ") |
| 35 |
} |
| 36 |
|
| 37 |
# Return STRING if FLAGS contains a flag matching regexp REGEX, |
| 38 |
# otherwise return the empty string. |
| 39 |
function test_flag(regex, flags, string) |
| 40 |
{ |
| 41 |
if (flag_set_p(regex, flags)) |
| 42 |
return string |
| 43 |
return "" |
| 44 |
} |
| 45 |
|
| 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) |
| 49 |
{ |
| 50 |
if (flag_set_p(regex, flags)) |
| 51 |
return "1 /* " regex " */, " |
| 52 |
else |
| 53 |
return "0, " |
| 54 |
} |
| 55 |
|
| 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) |
| 59 |
{ |
| 60 |
flags = " " flags |
| 61 |
if (flags !~ " " name "\\(") |
| 62 |
return "" |
| 63 |
sub(".* " name "\\(", "", flags) |
| 64 |
if (flags ~ "^[{]") |
| 65 |
{ |
| 66 |
sub ("^[{]", "", flags) |
| 67 |
sub ("}\\).*", "", flags) |
| 68 |
} |
| 69 |
else |
| 70 |
sub("\\).*", "", flags) |
| 71 |
|
| 72 |
return flags |
| 73 |
} |
| 74 |
|
| 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) |
| 78 |
{ |
| 79 |
args = opt_args(name, flags) |
| 80 |
if (args == "") |
| 81 |
print "#error Empty option argument '" name "' during parsing of: " flags |
| 82 |
return args |
| 83 |
} |
| 84 |
|
| 85 |
# Return the number of comma-separated element of S. |
| 86 |
function n_args(s) |
| 87 |
{ |
| 88 |
n = 1 |
| 89 |
while (s ~ ",") { |
| 90 |
n++ |
| 91 |
sub("[^,]*, *", "", s) |
| 92 |
} |
| 93 |
return n |
| 94 |
} |
| 95 |
|
| 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) |
| 99 |
{ |
| 100 |
while (n-- > 0) { |
| 101 |
if (s !~ ",") |
| 102 |
return "" |
| 103 |
sub("[^,]*, *", "", s) |
| 104 |
} |
| 105 |
sub(",.*", "", s) |
| 106 |
return s |
| 107 |
} |
| 108 |
|
| 109 |
# Return a bitmask of CL_* values for option flags FLAGS. |
| 110 |
function switch_flags (flags) |
| 111 |
{ |
| 112 |
result = "0" |
| 113 |
for (j = 0; j < n_langs; j++) { |
| 114 |
regex = langs[j] |
| 115 |
gsub ( "\\+", "\\+", regex ) |
| 116 |
result = result test_flag(regex, flags, " | " macros[j]) |
| 117 |
} |
| 118 |
result = result \ |
| 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 ) |
| 132 |
return result |
| 133 |
} |
| 134 |
|
| 135 |
# Return bit-field initializers for option flags FLAGS. |
| 136 |
function switch_bit_fields (flags) |
| 137 |
{ |
| 138 |
uinteger_flag = "" |
| 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) |
| 145 |
} |
| 146 |
else |
| 147 |
hwi = "" |
| 148 |
result = "" |
| 149 |
sep_args = opt_args("Args", flags) |
| 150 |
if (sep_args == "") |
| 151 |
sep_args = 0 |
| 152 |
else |
| 153 |
sep_args-- |
| 154 |
result = result sep_args ", " |
| 155 |
|
| 156 |
if (uinteger_flag == "") |
| 157 |
uinteger_flag = flag_init("UInteger", flags) |
| 158 |
|
| 159 |
hwi_flag = flag_init("Host_Wide_Int", hwi) |
| 160 |
byte_size_flag = flag_init("ByteSize", flags) |
| 161 |
|
| 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" |
| 166 |
|
| 167 |
# The following flags need to be in the same order as |
| 168 |
# the corresponding members of struct cl_option defined |
| 169 |
# in gcc/opts.h. |
| 170 |
result = result \ |
| 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) \ |
| 177 |
uinteger_flag \ |
| 178 |
hwi_flag \ |
| 179 |
flag_init("ToLower", flags) \ |
| 180 |
byte_size_flag |
| 181 |
|
| 182 |
if (flag_set_p("Report", flags)) |
| 183 |
print "#error Report option property is dropped" |
| 184 |
|
| 185 |
sub(", $", "", result) |
| 186 |
return result |
| 187 |
} |
| 188 |
|
| 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) |
| 192 |
{ |
| 193 |
return nth_arg(0, opt_args("Var", flags)) |
| 194 |
} |
| 195 |
|
| 196 |
# If FLAGS includes a UrlSuffix flag, return the value it specifies. |
| 197 |
# Return the empty string otherwise. |
| 198 |
function url_suffix(flags) |
| 199 |
{ |
| 200 |
return nth_arg(0, opt_args("UrlSuffix", flags)) |
| 201 |
} |
| 202 |
|
| 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) |
| 207 |
{ |
| 208 |
return nth_arg(0, opt_args("LangUrlSuffix_" lang, flags)) |
| 209 |
} |
| 210 |
|
| 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) |
| 214 |
{ |
| 215 |
split (flags, array, "[ \t]+") |
| 216 |
if (array[1] == "HOST_WIDE_INT") |
| 217 |
return array[2] |
| 218 |
else |
| 219 |
return "" |
| 220 |
} |
| 221 |
|
| 222 |
# Return true if the option described by FLAGS has a globally-visible state. |
| 223 |
function global_state_p(flags) |
| 224 |
{ |
| 225 |
return (var_name(flags) != "" \ |
| 226 |
|| opt_args("Mask", flags) != "" \ |
| 227 |
|| opt_args("InverseMask", flags) != "") |
| 228 |
} |
| 229 |
|
| 230 |
# Return true if the option described by FLAGS must have some state |
| 231 |
# associated with it. |
| 232 |
function needs_state_p(flags) |
| 233 |
{ |
| 234 |
return (flag_set_p("Target", flags) \ |
| 235 |
&& !flag_set_p("Alias.*", flags) \ |
| 236 |
&& !flag_set_p("Ignore", flags)) |
| 237 |
} |
| 238 |
|
| 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) |
| 243 |
{ |
| 244 |
if (global_state_p(flags) || !needs_state_p(flags)) |
| 245 |
return "" |
| 246 |
gsub ("[^" alnum "]", "_", name) |
| 247 |
return "VAR_" name |
| 248 |
} |
| 249 |
|
| 250 |
# Return the type of variable that should be associated with the given flags. |
| 251 |
function var_type(flags) |
| 252 |
{ |
| 253 |
if (flag_set_p("Defer", flags)) |
| 254 |
return "void *" |
| 255 |
else if (flag_set_p("Enum.*", flags)) { |
| 256 |
en = opt_args("Enum", flags); |
| 257 |
return enum_type[en] " " |
| 258 |
} |
| 259 |
else if (!flag_set_p("Joined.*", flags) && !flag_set_p("Separate", flags)) |
| 260 |
return "int " |
| 261 |
else if (flag_set_p("Host_Wide_Int", flags)) |
| 262 |
return "HOST_WIDE_INT " |
| 263 |
else if (flag_set_p("UInteger", flags)) |
| 264 |
return "int " |
| 265 |
else |
| 266 |
return "const char *" |
| 267 |
} |
| 268 |
|
| 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) |
| 273 |
{ |
| 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 " |
| 279 |
return "int " |
| 280 |
} |
| 281 |
else if (flag_set_p("Enum.*", flags)) { |
| 282 |
en = opt_args("Enum", flags); |
| 283 |
return enum_type[en] " " |
| 284 |
} |
| 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 " |
| 289 |
else |
| 290 |
return "/* - */ int " |
| 291 |
} |
| 292 |
else |
| 293 |
return "signed char " |
| 294 |
} |
| 295 |
else |
| 296 |
return "const char *" |
| 297 |
} |
| 298 |
|
| 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) |
| 302 |
{ |
| 303 |
if (flag_set_p("Defer", flags)) |
| 304 |
return "0, CLVC_DEFER, 0" |
| 305 |
s = nth_arg(1, opt_args("Var", flags)) |
| 306 |
if (s != "") |
| 307 |
return "0, CLVC_EQUAL, " s |
| 308 |
s = opt_args("Mask", flags); |
| 309 |
if (s != "") { |
| 310 |
vn = var_name(flags); |
| 311 |
if (vn) |
| 312 |
return "0, CLVC_BIT_SET, OPTION_MASK_" s |
| 313 |
else |
| 314 |
return "0, CLVC_BIT_SET, MASK_" s |
| 315 |
} |
| 316 |
s = nth_arg(0, opt_args("InverseMask", flags)); |
| 317 |
if (s != "") { |
| 318 |
vn = var_name(flags); |
| 319 |
if (vn) |
| 320 |
return "0, CLVC_BIT_CLEAR, OPTION_MASK_" s |
| 321 |
else |
| 322 |
return "0, CLVC_BIT_CLEAR, MASK_" s |
| 323 |
} |
| 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" |
| 330 |
else |
| 331 |
return enum_index[en] ", CLVC_ENUM, CLEV_NORMAL" |
| 332 |
} |
| 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" |
| 338 |
} |
| 339 |
|
| 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) |
| 343 |
{ |
| 344 |
name = var_name(flags) static_var(name, flags) |
| 345 |
if (name != "") |
| 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" |
| 352 |
} |
| 353 |
|
| 354 |
# Given the option called NAME return a sanitized version of its name. |
| 355 |
function opt_sanitized_name(name) |
| 356 |
{ |
| 357 |
gsub ("[^" alnum "]", "_", name) |
| 358 |
return name |
| 359 |
} |
| 360 |
|
| 361 |
# Given the option called NAME return the appropriate enum for it. |
| 362 |
function opt_enum(name) |
| 363 |
{ |
| 364 |
return "OPT_" opt_sanitized_name(name) |
| 365 |
} |
| 366 |
|
| 367 |
# Given the language called NAME return a sanitized version of its name. |
| 368 |
function lang_sanitized_name(name) |
| 369 |
{ |
| 370 |
gsub( "[^" alnum "_]", "X", name ) |
| 371 |
return name |
| 372 |
} |
| 373 |
|
| 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) |
| 377 |
{ |
| 378 |
opt_var_name = var_name(flags[opt_numbers[name]]); |
| 379 |
if (opt_var_name != "") { |
| 380 |
return opt_var_name; |
| 381 |
} |
| 382 |
for (k = 0; k < n_opts; k++) { |
| 383 |
if (opts[k] == name && var_name(flags[k]) != "") { |
| 384 |
return var_name(flags[k]); |
| 385 |
} |
| 386 |
} |
| 387 |
return "" |
| 388 |
} |
| 389 |
|
| 390 |
function integer_range_info(range_option, init, option, uinteger_used) |
| 391 |
{ |
| 392 |
if (range_option != "") { |
| 393 |
ival = init + 0; |
| 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 |
| 401 |
} |
| 402 |
else |
| 403 |
return "-1, -1" |
| 404 |
} |
| 405 |
|
| 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 |
| 408 |
# defination. |
| 409 |
function find_index(var, var_arry, n_var_arry) |
| 410 |
{ |
| 411 |
for (var_index = 0; var_index < n_var_arry; var_index++) |
| 412 |
{ |
| 413 |
if (var_arry[var_index] == var) |
| 414 |
break |
| 415 |
} |
| 416 |
return var_index |
| 417 |
} |