| 1 |
/* Subroutines used for parsing target attribute for RISC-V. |
| 2 |
Copyright (C) 2023-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GCC 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. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#define IN_TARGET_CODE 1 |
| 21 |
|
| 22 |
#define INCLUDE_STRING |
| 23 |
#include "config.h" |
| 24 |
#include "system.h" |
| 25 |
#include "coretypes.h" |
| 26 |
#include "target.h" |
| 27 |
#include "tree.h" |
| 28 |
#include "tm_p.h" |
| 29 |
#include "diagnostic.h" |
| 30 |
#include "opts.h" |
| 31 |
#include "riscv-subset.h" |
| 32 |
|
| 33 |
namespace { |
| 34 |
class riscv_target_attr_parser |
| 35 |
{ |
| 36 |
public: |
| 37 |
riscv_target_attr_parser (location_t loc) |
| 38 |
: m_found_arch_p (false) |
| 39 |
, m_found_tune_p (false) |
| 40 |
, m_found_cpu_p (false) |
| 41 |
, m_found_priority_p (false) |
| 42 |
, m_subset_list (nullptr) |
| 43 |
, m_loc (loc) |
| 44 |
, m_cpu_info (nullptr) |
| 45 |
, m_tune (nullptr) |
| 46 |
, m_priority (0) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
bool handle_arch (const char *); |
| 51 |
bool handle_cpu (const char *); |
| 52 |
bool handle_tune (const char *); |
| 53 |
bool handle_priority (const char *); |
| 54 |
|
| 55 |
void update_settings (struct gcc_options *opts) const; |
| 56 |
private: |
| 57 |
const char *m_raw_attr_str; |
| 58 |
bool parse_arch (const char *); |
| 59 |
|
| 60 |
bool m_found_arch_p; |
| 61 |
bool m_found_tune_p; |
| 62 |
bool m_found_cpu_p; |
| 63 |
bool m_found_priority_p; |
| 64 |
riscv_subset_list *m_subset_list; |
| 65 |
location_t m_loc; |
| 66 |
const riscv_cpu_info *m_cpu_info; |
| 67 |
const char *m_tune; |
| 68 |
int m_priority; |
| 69 |
}; |
| 70 |
} |
| 71 |
|
| 72 |
/* All the information needed to handle a target attribute. |
| 73 |
NAME is the name of the attribute. |
| 74 |
HANDLER is the function that takes the attribute string as an argument. */ |
| 75 |
|
| 76 |
struct riscv_attribute_info |
| 77 |
{ |
| 78 |
const char *name; |
| 79 |
bool (riscv_target_attr_parser::*handler) (const char *); |
| 80 |
}; |
| 81 |
|
| 82 |
/* The target attributes that we support. */ |
| 83 |
|
| 84 |
static const struct riscv_attribute_info riscv_target_attrs[] |
| 85 |
= {{"arch", &riscv_target_attr_parser::handle_arch}, |
| 86 |
{"cpu", &riscv_target_attr_parser::handle_cpu}, |
| 87 |
{"tune", &riscv_target_attr_parser::handle_tune}, |
| 88 |
{NULL, NULL}}; |
| 89 |
|
| 90 |
static const struct riscv_attribute_info riscv_target_version_attrs[] |
| 91 |
= {{"arch", &riscv_target_attr_parser::handle_arch}, |
| 92 |
{"priority", &riscv_target_attr_parser::handle_priority}, |
| 93 |
{NULL, NULL}}; |
| 94 |
|
| 95 |
bool |
| 96 |
riscv_target_attr_parser::parse_arch (const char *str) |
| 97 |
{ |
| 98 |
if (m_subset_list) |
| 99 |
delete m_subset_list; |
| 100 |
/* Check if it's setting full arch string. */ |
| 101 |
if (strncmp ("rv", str, strlen ("rv")) == 0) |
| 102 |
{ |
| 103 |
if (TARGET_64BIT && strncmp ("32", str + 2, strlen ("32")) == 0) |
| 104 |
{ |
| 105 |
error_at (m_loc, "unexpected arch for %<target()%> attribute: " |
| 106 |
"must start with rv64 but found %qs", str); |
| 107 |
goto fail; |
| 108 |
} |
| 109 |
|
| 110 |
if (!TARGET_64BIT && strncmp ("64", str + 2, strlen ("64")) == 0) |
| 111 |
{ |
| 112 |
error_at (m_loc, "unexpected arch for %<target()%> attribute: " |
| 113 |
"must start with rv32 but found %qs", str); |
| 114 |
goto fail; |
| 115 |
} |
| 116 |
|
| 117 |
m_subset_list = riscv_subset_list::parse (str, m_loc); |
| 118 |
|
| 119 |
if (m_subset_list == nullptr) |
| 120 |
goto fail; |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
/* Parsing the extension list like "+<ext>[,+<ext>]*". */ |
| 127 |
size_t len = strlen (str); |
| 128 |
std::unique_ptr<char[]> buf (new char[len+1]); |
| 129 |
char *str_to_check = buf.get (); |
| 130 |
strcpy (str_to_check, str); |
| 131 |
const char *token = strtok_r (str_to_check, ",", &str_to_check); |
| 132 |
const char *local_arch_str = global_options.x_riscv_arch_string; |
| 133 |
m_subset_list = local_arch_str |
| 134 |
? riscv_subset_list::parse (local_arch_str, m_loc) |
| 135 |
: riscv_cmdline_subset_list ()->clone (); |
| 136 |
m_subset_list->set_loc (m_loc); |
| 137 |
m_subset_list->set_allow_adding_dup (true); |
| 138 |
|
| 139 |
while (token) |
| 140 |
{ |
| 141 |
if (token[0] != '+') |
| 142 |
{ |
| 143 |
error_at ( |
| 144 |
m_loc, |
| 145 |
"unexpected arch for %<target()%> attribute: must start " |
| 146 |
"with + or rv"); |
| 147 |
goto fail; |
| 148 |
} |
| 149 |
|
| 150 |
const char *result = m_subset_list->parse_single_ext (token + 1); |
| 151 |
/* Check parse_single_ext has consume all string. */ |
| 152 |
if (*result != '\0') |
| 153 |
{ |
| 154 |
error_at ( |
| 155 |
m_loc, |
| 156 |
"unexpected arch for %<target()%> attribute: bad " |
| 157 |
"string found %qs", token); |
| 158 |
goto fail; |
| 159 |
} |
| 160 |
|
| 161 |
token = strtok_r (NULL, ",", &str_to_check); |
| 162 |
} |
| 163 |
|
| 164 |
m_subset_list->set_allow_adding_dup (false); |
| 165 |
m_subset_list->finalize (); |
| 166 |
return true; |
| 167 |
} |
| 168 |
fail: |
| 169 |
if (m_subset_list != nullptr) |
| 170 |
{ |
| 171 |
delete m_subset_list; |
| 172 |
m_subset_list = nullptr; |
| 173 |
} |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
/* Handle the ARCH_STR argument to the arch= target attribute. */ |
| 178 |
|
| 179 |
bool |
| 180 |
riscv_target_attr_parser::handle_arch (const char *str) |
| 181 |
{ |
| 182 |
if (m_found_arch_p) |
| 183 |
error_at (m_loc, "%<target()%> attribute: arch appears more than once"); |
| 184 |
m_found_arch_p = true; |
| 185 |
return parse_arch (str); |
| 186 |
} |
| 187 |
|
| 188 |
/* Handle the CPU_STR argument to the cpu= target attribute. */ |
| 189 |
|
| 190 |
bool |
| 191 |
riscv_target_attr_parser::handle_cpu (const char *str) |
| 192 |
{ |
| 193 |
if (m_found_cpu_p) |
| 194 |
error_at (m_loc, "%<target()%> attribute: cpu appears more than once"); |
| 195 |
|
| 196 |
m_found_cpu_p = true; |
| 197 |
const riscv_cpu_info *cpu_info = riscv_find_cpu (str); |
| 198 |
|
| 199 |
if (!cpu_info) |
| 200 |
{ |
| 201 |
error_at (m_loc, "%<target()%> attribute: unknown CPU %qs", str); |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
if (m_subset_list == nullptr) |
| 206 |
{ |
| 207 |
const char *arch_str = cpu_info->arch; |
| 208 |
m_subset_list = riscv_subset_list::parse (arch_str, m_loc); |
| 209 |
gcc_assert (m_subset_list); |
| 210 |
} |
| 211 |
|
| 212 |
m_cpu_info = cpu_info; |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
/* Handle the TUNE_STR argument to the tune= target attribute. */ |
| 217 |
|
| 218 |
bool |
| 219 |
riscv_target_attr_parser::handle_tune (const char *str) |
| 220 |
{ |
| 221 |
if (m_found_tune_p) |
| 222 |
error_at (m_loc, "%<target()%> attribute: tune appears more than once"); |
| 223 |
m_found_tune_p = true; |
| 224 |
const struct riscv_tune_info *tune = riscv_parse_tune (str, true); |
| 225 |
|
| 226 |
if (tune == nullptr) |
| 227 |
{ |
| 228 |
error_at (m_loc, "%<target()%> attribute: unknown TUNE %qs", str); |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
m_tune = tune->name; |
| 233 |
|
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
bool |
| 238 |
riscv_target_attr_parser::handle_priority (const char *str) |
| 239 |
{ |
| 240 |
if (m_found_priority_p) |
| 241 |
error_at (m_loc, "%<target()%> attribute: priority appears more than once"); |
| 242 |
m_found_priority_p = true; |
| 243 |
|
| 244 |
if (sscanf (str, "%d", &m_priority) != 1) |
| 245 |
{ |
| 246 |
error_at (m_loc, "%<target()%> attribute: invalid priority %qs", str); |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
return true; |
| 251 |
} |
| 252 |
|
| 253 |
void |
| 254 |
riscv_target_attr_parser::update_settings (struct gcc_options *opts) const |
| 255 |
{ |
| 256 |
if (m_subset_list) |
| 257 |
{ |
| 258 |
std::string local_arch = m_subset_list->to_string (true); |
| 259 |
const char* local_arch_str = local_arch.c_str (); |
| 260 |
struct cl_target_option *default_opts |
| 261 |
= TREE_TARGET_OPTION (target_option_default_node); |
| 262 |
if (opts->x_riscv_arch_string != default_opts->x_riscv_arch_string) |
| 263 |
free (CONST_CAST (void *, (const void *) opts->x_riscv_arch_string)); |
| 264 |
opts->x_riscv_arch_string = xstrdup (local_arch_str); |
| 265 |
|
| 266 |
riscv_set_arch_by_subset_list (m_subset_list, opts); |
| 267 |
} |
| 268 |
|
| 269 |
if (m_cpu_info) |
| 270 |
opts->x_riscv_cpu_string = m_cpu_info->name; |
| 271 |
|
| 272 |
if (m_tune) |
| 273 |
opts->x_riscv_tune_string = m_tune; |
| 274 |
else |
| 275 |
{ |
| 276 |
if (m_cpu_info) |
| 277 |
opts->x_riscv_tune_string = m_cpu_info->tune; |
| 278 |
} |
| 279 |
|
| 280 |
if (m_priority) |
| 281 |
opts->x_riscv_fmv_priority = m_priority; |
| 282 |
} |
| 283 |
|
| 284 |
/* Parse ARG_STR which contains the definition of one target attribute. |
| 285 |
Show appropriate errors if any or return true if the attribute is valid. */ |
| 286 |
|
| 287 |
static bool |
| 288 |
riscv_process_one_target_attr (char *arg_str, |
| 289 |
location_t loc, |
| 290 |
riscv_target_attr_parser &attr_parser, |
| 291 |
const struct riscv_attribute_info *attrs) |
| 292 |
{ |
| 293 |
size_t len = strlen (arg_str); |
| 294 |
|
| 295 |
if (len == 0) |
| 296 |
{ |
| 297 |
error_at (loc, "malformed %<target()%> attribute"); |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
std::unique_ptr<char[]> buf (new char[len+1]); |
| 302 |
char *str_to_check = buf.get(); |
| 303 |
strcpy (str_to_check, arg_str); |
| 304 |
|
| 305 |
char *arg = strchr (str_to_check, '='); |
| 306 |
|
| 307 |
if (!arg) |
| 308 |
{ |
| 309 |
error_at ( |
| 310 |
loc, |
| 311 |
"attribute %<target(\"%s\")%> does not accept an argument", |
| 312 |
str_to_check); |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
arg[0] = '\0'; |
| 317 |
++arg; |
| 318 |
for (const auto *attr = attrs; |
| 319 |
attr->name; |
| 320 |
++attr) |
| 321 |
{ |
| 322 |
/* If the names don't match up, or the user has given an argument |
| 323 |
to an attribute that doesn't accept one, or didn't give an argument |
| 324 |
to an attribute that expects one, fail to match. */ |
| 325 |
if (strncmp (str_to_check, attr->name, strlen (attr->name)) != 0) |
| 326 |
continue; |
| 327 |
|
| 328 |
return (&attr_parser->*attr->handler) (arg); |
| 329 |
} |
| 330 |
|
| 331 |
error_at (loc, "Got unknown attribute %<target(\"%s\")%>", str_to_check); |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
/* Count how many times the character C appears in |
| 336 |
NULL-terminated string STR. */ |
| 337 |
|
| 338 |
static unsigned int |
| 339 |
num_occurrences_in_str (char c, char *str) |
| 340 |
{ |
| 341 |
unsigned int res = 0; |
| 342 |
while (*str != '\0') |
| 343 |
{ |
| 344 |
if (*str == c) |
| 345 |
res++; |
| 346 |
|
| 347 |
str++; |
| 348 |
} |
| 349 |
|
| 350 |
return res; |
| 351 |
} |
| 352 |
|
| 353 |
/* Parse the string in ARGS that contains the target attribute information |
| 354 |
and update the global target options space. */ |
| 355 |
|
| 356 |
bool |
| 357 |
riscv_process_target_attr (const char *args, |
| 358 |
location_t loc, |
| 359 |
const struct riscv_attribute_info *attrs) |
| 360 |
{ |
| 361 |
size_t len = strlen (args); |
| 362 |
|
| 363 |
/* No need to emit warning or error on empty string here, generic code already |
| 364 |
handle this case. */ |
| 365 |
if (len == 0) |
| 366 |
{ |
| 367 |
return false; |
| 368 |
} |
| 369 |
|
| 370 |
std::unique_ptr<char[]> buf (new char[len+1]); |
| 371 |
char *str_to_check = buf.get (); |
| 372 |
strcpy (str_to_check, args); |
| 373 |
|
| 374 |
/* Used to catch empty spaces between semi-colons i.e. |
| 375 |
attribute ((target ("attr1;;attr2"))). */ |
| 376 |
unsigned int num_semicolons = num_occurrences_in_str (';', str_to_check); |
| 377 |
|
| 378 |
/* Handle multiple target attributes separated by ';'. */ |
| 379 |
char *token = strtok_r (str_to_check, ";", &str_to_check); |
| 380 |
|
| 381 |
riscv_target_attr_parser attr_parser (loc); |
| 382 |
unsigned int num_attrs = 0; |
| 383 |
while (token) |
| 384 |
{ |
| 385 |
num_attrs++; |
| 386 |
if (!riscv_process_one_target_attr (token, loc, attr_parser, attrs)) |
| 387 |
return false; |
| 388 |
|
| 389 |
token = strtok_r (NULL, ";", &str_to_check); |
| 390 |
} |
| 391 |
|
| 392 |
if (num_attrs != num_semicolons + 1) |
| 393 |
{ |
| 394 |
error_at (loc, "malformed %<target(\"%s\")%> attribute", |
| 395 |
args); |
| 396 |
return false; |
| 397 |
} |
| 398 |
|
| 399 |
/* Apply settings from target attribute. */ |
| 400 |
attr_parser.update_settings (&global_options); |
| 401 |
|
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
/* Parse the tree in ARGS that contains the target attribute information |
| 406 |
and update the global target options space. */ |
| 407 |
|
| 408 |
static bool |
| 409 |
riscv_process_target_attr (tree args, |
| 410 |
location_t loc, |
| 411 |
const struct riscv_attribute_info *attrs) |
| 412 |
{ |
| 413 |
if (TREE_CODE (args) == TREE_LIST) |
| 414 |
{ |
| 415 |
do |
| 416 |
{ |
| 417 |
tree head = TREE_VALUE (args); |
| 418 |
if (head) |
| 419 |
{ |
| 420 |
if (!riscv_process_target_attr (head, loc, attrs)) |
| 421 |
return false; |
| 422 |
} |
| 423 |
args = TREE_CHAIN (args); |
| 424 |
} while (args); |
| 425 |
|
| 426 |
return true; |
| 427 |
} |
| 428 |
|
| 429 |
if (TREE_CODE (args) != STRING_CST) |
| 430 |
{ |
| 431 |
error_at (loc, "attribute %<target%> argument not a string"); |
| 432 |
return false; |
| 433 |
} |
| 434 |
|
| 435 |
return riscv_process_target_attr (TREE_STRING_POINTER (args), loc, attrs); |
| 436 |
} |
| 437 |
|
| 438 |
/* Implement TARGET_OPTION_VALID_ATTRIBUTE_P. |
| 439 |
This is used to process attribute ((target ("..."))). |
| 440 |
Note, that riscv_set_current_function() has not been called before, |
| 441 |
so we need must not mess with the current global_options, which |
| 442 |
likely belong to another function. */ |
| 443 |
|
| 444 |
bool |
| 445 |
riscv_option_valid_attribute_p (tree fndecl, tree, tree args, int) |
| 446 |
{ |
| 447 |
struct cl_target_option cur_target; |
| 448 |
bool ret; |
| 449 |
tree new_target; |
| 450 |
tree existing_target = DECL_FUNCTION_SPECIFIC_TARGET (fndecl); |
| 451 |
location_t loc = DECL_SOURCE_LOCATION (fndecl); |
| 452 |
|
| 453 |
/* Save the current target options to restore at the end. */ |
| 454 |
cl_target_option_save (&cur_target, &global_options, &global_options_set); |
| 455 |
|
| 456 |
/* If fndecl already has some target attributes applied to it, unpack |
| 457 |
them so that we add this attribute on top of them, rather than |
| 458 |
overwriting them. */ |
| 459 |
if (existing_target) |
| 460 |
{ |
| 461 |
struct cl_target_option *existing_options |
| 462 |
= TREE_TARGET_OPTION (existing_target); |
| 463 |
|
| 464 |
if (existing_options) |
| 465 |
cl_target_option_restore (&global_options, &global_options_set, |
| 466 |
existing_options); |
| 467 |
} |
| 468 |
else |
| 469 |
cl_target_option_restore (&global_options, &global_options_set, |
| 470 |
TREE_TARGET_OPTION (target_option_default_node)); |
| 471 |
|
| 472 |
/* Now we can parse the attributes and set &global_options accordingly. */ |
| 473 |
ret = riscv_process_target_attr (args, loc, riscv_target_attrs); |
| 474 |
if (ret) |
| 475 |
{ |
| 476 |
riscv_override_options_internal (&global_options); |
| 477 |
new_target = build_target_option_node (&global_options, |
| 478 |
&global_options_set); |
| 479 |
DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target; |
| 480 |
} |
| 481 |
|
| 482 |
/* Restore current target options to original state. */ |
| 483 |
cl_target_option_restore (&global_options, &global_options_set, &cur_target); |
| 484 |
return ret; |
| 485 |
} |
| 486 |
|
| 487 |
/* Parse the tree in ARGS that contains the target_version attribute |
| 488 |
information and update the global target options space. */ |
| 489 |
|
| 490 |
bool |
| 491 |
riscv_process_target_version_attr (tree args, location_t loc) |
| 492 |
{ |
| 493 |
if (TREE_CODE (args) == TREE_LIST) |
| 494 |
{ |
| 495 |
if (TREE_CHAIN (args)) |
| 496 |
{ |
| 497 |
error ("attribute %<target_version%> has multiple values"); |
| 498 |
return false; |
| 499 |
} |
| 500 |
args = TREE_VALUE (args); |
| 501 |
} |
| 502 |
|
| 503 |
if (!args || TREE_CODE (args) != STRING_CST) |
| 504 |
{ |
| 505 |
error ("attribute %<target_version%> argument not a string"); |
| 506 |
return false; |
| 507 |
} |
| 508 |
|
| 509 |
const char *str = TREE_STRING_POINTER (args); |
| 510 |
if (strcmp (str, "default") == 0) |
| 511 |
return true; |
| 512 |
|
| 513 |
return riscv_process_target_attr (str, loc, riscv_target_version_attrs); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
/* Implement TARGET_OPTION_VALID_VERSION_ATTRIBUTE_P. This is used to |
| 518 |
process attribute ((target_version ("..."))). */ |
| 519 |
|
| 520 |
bool |
| 521 |
riscv_option_valid_version_attribute_p (tree fndecl, tree, tree args, int) |
| 522 |
{ |
| 523 |
struct cl_target_option cur_target; |
| 524 |
bool ret; |
| 525 |
tree new_target; |
| 526 |
tree existing_target = DECL_FUNCTION_SPECIFIC_TARGET (fndecl); |
| 527 |
location_t loc = DECL_SOURCE_LOCATION (fndecl); |
| 528 |
|
| 529 |
/* Save the current target options to restore at the end. */ |
| 530 |
cl_target_option_save (&cur_target, &global_options, &global_options_set); |
| 531 |
|
| 532 |
/* If fndecl already has some target attributes applied to it, unpack |
| 533 |
them so that we add this attribute on top of them, rather than |
| 534 |
overwriting them. */ |
| 535 |
if (existing_target) |
| 536 |
{ |
| 537 |
struct cl_target_option *existing_options |
| 538 |
= TREE_TARGET_OPTION (existing_target); |
| 539 |
|
| 540 |
if (existing_options) |
| 541 |
cl_target_option_restore (&global_options, &global_options_set, |
| 542 |
existing_options); |
| 543 |
} |
| 544 |
else |
| 545 |
cl_target_option_restore (&global_options, &global_options_set, |
| 546 |
TREE_TARGET_OPTION (target_option_current_node)); |
| 547 |
|
| 548 |
ret = riscv_process_target_version_attr (args, loc); |
| 549 |
|
| 550 |
/* Set up any additional state. */ |
| 551 |
if (ret) |
| 552 |
{ |
| 553 |
riscv_override_options_internal (&global_options); |
| 554 |
new_target = build_target_option_node (&global_options, |
| 555 |
&global_options_set); |
| 556 |
} |
| 557 |
else |
| 558 |
new_target = NULL; |
| 559 |
|
| 560 |
if (fndecl && ret) |
| 561 |
DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target; |
| 562 |
|
| 563 |
cl_target_option_restore (&global_options, &global_options_set, &cur_target); |
| 564 |
|
| 565 |
return ret; |
| 566 |
} |