Debian Codegraph @

gcc/common/config/riscv/riscv-common.cc

raw
1 /* Common hooks for RISC-V.
2 Copyright (C) 2016-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 #include <sstream>
21 #include <vector>
22 #include <queue>
23
24 #define INCLUDE_STRING
25 #define INCLUDE_SET
26 #define INCLUDE_MAP
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "common/common-target.h"
32 #include "common/common-target-def.h"
33 #include "opts.h"
34 #include "flags.h"
35 #include "diagnostic-core.h"
36 #include "config/riscv/riscv-protos.h"
37 #include "config/riscv/riscv-subset.h"
38
39 #ifdef TARGET_BIG_ENDIAN_DEFAULT
40 #undef TARGET_DEFAULT_TARGET_FLAGS
41 #define TARGET_DEFAULT_TARGET_FLAGS (MASK_BIG_ENDIAN)
42 #endif
43
44 typedef bool (*riscv_implied_predicator_t) (const riscv_subset_list *);
45
46 /* Type for implied ISA info. */
47 struct riscv_implied_info_t
48 {
49 constexpr riscv_implied_info_t (const char *ext, const char *implied_ext,
50 riscv_implied_predicator_t predicator
51 = nullptr)
52 : ext (ext), implied_ext (implied_ext), predicator (predicator){};
53
54 bool match (const riscv_subset_list *subset_list, const char *ext_name) const
55 {
56 if (strcmp (ext_name, ext) != 0)
57 return false;
58
59 if (predicator && !predicator (subset_list))
60 return false;
61
62 return true;
63 }
64
65 bool match (const riscv_subset_list *subset_list,
66 const riscv_subset_t *subset) const
67 {
68 return match (subset_list, subset->name.c_str());
69 }
70
71 const char *ext;
72 const char *implied_ext;
73 riscv_implied_predicator_t predicator;
74 };
75
76 /* Implied ISA info, must end with NULL sentinel. */
77 static const riscv_implied_info_t riscv_implied_info[] =
78 {
79 {"m", "zmmul"},
80
81 {"d", "f"},
82 {"f", "zicsr"},
83 {"d", "zicsr"},
84
85 {"a", "zaamo"},
86 {"a", "zalrsc"},
87
88 {"c", "zca"},
89 {"c", "zcf",
90 [] (const riscv_subset_list *subset_list) -> bool
91 {
92 return subset_list->xlen () == 32 && subset_list->lookup ("f");
93 }},
94 {"c", "zcd",
95 [] (const riscv_subset_list *subset_list) -> bool
96 {
97 return subset_list->lookup ("d");
98 }},
99
100 {"zabha", "zaamo"},
101 {"zacas", "zaamo"},
102 {"zawrs", "zalrsc"},
103
104 {"zcmop", "zca"},
105
106 {"b", "zba"},
107 {"b", "zbb"},
108 {"b", "zbs"},
109
110 {"zdinx", "zfinx"},
111 {"zfinx", "zicsr"},
112 {"zdinx", "zicsr"},
113
114 {"zicfiss", "zicsr"},
115 {"zicfiss", "zimop"},
116 {"zicfilp", "zicsr"},
117
118 {"zk", "zkn"},
119 {"zk", "zkr"},
120 {"zk", "zkt"},
121 {"zkn", "zbkb"},
122 {"zkn", "zbkc"},
123 {"zkn", "zbkx"},
124 {"zkn", "zkne"},
125 {"zkn", "zknd"},
126 {"zkn", "zknh"},
127 {"zks", "zbkb"},
128 {"zks", "zbkc"},
129 {"zks", "zbkx"},
130 {"zks", "zksed"},
131 {"zks", "zksh"},
132
133 {"v", "zvl128b"},
134 {"v", "zve64d"},
135
136 {"zve32f", "f"},
137 {"zve64f", "f"},
138 {"zve64d", "d"},
139
140 {"zve32x", "zvl32b"},
141 {"zve32f", "zve32x"},
142 {"zve32f", "zvl32b"},
143
144 {"zve64x", "zve32x"},
145 {"zve64x", "zvl64b"},
146 {"zve64f", "zve32f"},
147 {"zve64f", "zve64x"},
148 {"zve64f", "zvl64b"},
149 {"zve64d", "zve64f"},
150 {"zve64d", "zvl64b"},
151
152 {"zvl64b", "zvl32b"},
153 {"zvl128b", "zvl64b"},
154 {"zvl256b", "zvl128b"},
155 {"zvl512b", "zvl256b"},
156 {"zvl1024b", "zvl512b"},
157 {"zvl2048b", "zvl1024b"},
158 {"zvl4096b", "zvl2048b"},
159 {"zvl8192b", "zvl4096b"},
160 {"zvl16384b", "zvl8192b"},
161 {"zvl32768b", "zvl16384b"},
162 {"zvl65536b", "zvl32768b"},
163
164 {"zvkn", "zvkned"},
165 {"zvkn", "zvknhb"},
166 {"zvkn", "zvkb"},
167 {"zvkn", "zvkt"},
168 {"zvknc", "zvkn"},
169 {"zvknc", "zvbc"},
170 {"zvkng", "zvkn"},
171 {"zvkng", "zvkg"},
172 {"zvks", "zvksed"},
173 {"zvks", "zvksh"},
174 {"zvks", "zvkb"},
175 {"zvks", "zvkt"},
176 {"zvksc", "zvks"},
177 {"zvksc", "zvbc"},
178 {"zvksg", "zvks"},
179 {"zvksg", "zvkg"},
180 {"zvbb", "zvkb"},
181 {"zvbc", "zve64x"},
182 {"zvkb", "zve32x"},
183 {"zvkg", "zve32x"},
184 {"zvkned", "zve32x"},
185 {"zvknha", "zve32x"},
186 {"zvknhb", "zve64x"},
187 {"zvksed", "zve32x"},
188 {"zvksh", "zve32x"},
189
190 {"zfbfmin", "zfhmin"},
191 {"zfh", "zfhmin"},
192 {"zfhmin", "f"},
193
194 {"zfa", "f"},
195
196 {"zvfbfmin", "zve32f"},
197 {"zvfbfwma", "zvfbfmin"},
198 {"zvfbfwma", "zfbfmin"},
199 {"zvfhmin", "zve32f"},
200 {"zvfh", "zve32f"},
201 {"zvfh", "zfhmin"},
202
203 {"zhinx", "zhinxmin"},
204 {"zhinxmin", "zfinx"},
205
206 {"zce", "zca"},
207 {"zce", "zcb"},
208 {"zce", "zcmp"},
209 {"zce", "zcmt"},
210 {"zcf", "zca"},
211 {"zcd", "zca"},
212 {"zcb", "zca"},
213 {"zcmp", "zca"},
214 {"zcmt", "zca"},
215 {"zcmt", "zicsr"},
216 {"zce", "zcf",
217 [] (const riscv_subset_list *subset_list) -> bool
218 {
219 return subset_list->xlen () == 32 && subset_list->lookup ("f");
220 }},
221
222 {"smaia", "ssaia"},
223 {"smstateen", "ssstateen"},
224 {"smepmp", "zicsr"},
225 {"ssaia", "zicsr"},
226 {"sscofpmf", "zicsr"},
227 {"ssstateen", "zicsr"},
228 {"sstc", "zicsr"},
229
230 {"xsfvcp", "zve32x"},
231
232 {NULL, NULL}
233 };
234
235 /* This structure holds version information for specific ISA version. */
236
237 struct riscv_ext_version
238 {
239 const char *name;
240 enum riscv_isa_spec_class isa_spec_class;
241 int major_version;
242 int minor_version;
243 };
244
245 /* All standard extensions defined in all supported ISA spec. */
246 static const struct riscv_ext_version riscv_ext_version_table[] =
247 {
248 /* name, ISA spec, major version, minor_version. */
249 {"e", ISA_SPEC_CLASS_20191213, 2, 0},
250 {"e", ISA_SPEC_CLASS_20190608, 2, 0},
251 {"e", ISA_SPEC_CLASS_2P2, 2, 0},
252
253 {"i", ISA_SPEC_CLASS_20191213, 2, 1},
254 {"i", ISA_SPEC_CLASS_20190608, 2, 1},
255 {"i", ISA_SPEC_CLASS_2P2, 2, 0},
256
257 {"m", ISA_SPEC_CLASS_20191213, 2, 0},
258 {"m", ISA_SPEC_CLASS_20190608, 2, 0},
259 {"m", ISA_SPEC_CLASS_2P2, 2, 0},
260
261 {"a", ISA_SPEC_CLASS_20191213, 2, 1},
262 {"a", ISA_SPEC_CLASS_20190608, 2, 0},
263 {"a", ISA_SPEC_CLASS_2P2, 2, 0},
264
265 {"f", ISA_SPEC_CLASS_20191213, 2, 2},
266 {"f", ISA_SPEC_CLASS_20190608, 2, 2},
267 {"f", ISA_SPEC_CLASS_2P2, 2, 0},
268
269 {"d", ISA_SPEC_CLASS_20191213, 2, 2},
270 {"d", ISA_SPEC_CLASS_20190608, 2, 2},
271 {"d", ISA_SPEC_CLASS_2P2, 2, 0},
272
273 {"c", ISA_SPEC_CLASS_20191213, 2, 0},
274 {"c", ISA_SPEC_CLASS_20190608, 2, 0},
275 {"c", ISA_SPEC_CLASS_2P2, 2, 0},
276
277 {"b", ISA_SPEC_CLASS_NONE, 1, 0},
278
279 {"h", ISA_SPEC_CLASS_NONE, 1, 0},
280
281 {"v", ISA_SPEC_CLASS_NONE, 1, 0},
282
283 {"zicsr", ISA_SPEC_CLASS_20191213, 2, 0},
284 {"zicsr", ISA_SPEC_CLASS_20190608, 2, 0},
285
286 {"zifencei", ISA_SPEC_CLASS_20191213, 2, 0},
287 {"zifencei", ISA_SPEC_CLASS_20190608, 2, 0},
288
289 {"zicond", ISA_SPEC_CLASS_NONE, 1, 0},
290
291 {"za64rs", ISA_SPEC_CLASS_NONE, 1, 0},
292 {"za128rs", ISA_SPEC_CLASS_NONE, 1, 0},
293 {"zawrs", ISA_SPEC_CLASS_NONE, 1, 0},
294 {"zaamo", ISA_SPEC_CLASS_NONE, 1, 0},
295 {"zalrsc", ISA_SPEC_CLASS_NONE, 1, 0},
296 {"zabha", ISA_SPEC_CLASS_NONE, 1, 0},
297 {"zacas", ISA_SPEC_CLASS_NONE, 1, 0},
298
299 {"zba", ISA_SPEC_CLASS_NONE, 1, 0},
300 {"zbb", ISA_SPEC_CLASS_NONE, 1, 0},
301 {"zbc", ISA_SPEC_CLASS_NONE, 1, 0},
302 {"zbs", ISA_SPEC_CLASS_NONE, 1, 0},
303
304 {"zfinx", ISA_SPEC_CLASS_NONE, 1, 0},
305 {"zdinx", ISA_SPEC_CLASS_NONE, 1, 0},
306 {"zhinx", ISA_SPEC_CLASS_NONE, 1, 0},
307 {"zhinxmin", ISA_SPEC_CLASS_NONE, 1, 0},
308
309 {"zbkb", ISA_SPEC_CLASS_NONE, 1, 0},
310 {"zbkc", ISA_SPEC_CLASS_NONE, 1, 0},
311 {"zbkx", ISA_SPEC_CLASS_NONE, 1, 0},
312 {"zkne", ISA_SPEC_CLASS_NONE, 1, 0},
313 {"zknd", ISA_SPEC_CLASS_NONE, 1, 0},
314 {"zknh", ISA_SPEC_CLASS_NONE, 1, 0},
315 {"zkr", ISA_SPEC_CLASS_NONE, 1, 0},
316 {"zksed", ISA_SPEC_CLASS_NONE, 1, 0},
317 {"zksh", ISA_SPEC_CLASS_NONE, 1, 0},
318 {"zkt", ISA_SPEC_CLASS_NONE, 1, 0},
319
320 {"zihintntl", ISA_SPEC_CLASS_NONE, 1, 0},
321 {"zihintpause", ISA_SPEC_CLASS_NONE, 2, 0},
322
323 {"zicboz",ISA_SPEC_CLASS_NONE, 1, 0},
324 {"zicbom",ISA_SPEC_CLASS_NONE, 1, 0},
325 {"zicbop",ISA_SPEC_CLASS_NONE, 1, 0},
326 {"zic64b", ISA_SPEC_CLASS_NONE, 1, 0},
327 {"ziccamoa", ISA_SPEC_CLASS_NONE, 1, 0},
328 {"ziccif", ISA_SPEC_CLASS_NONE, 1, 0},
329 {"zicclsm", ISA_SPEC_CLASS_NONE, 1, 0},
330 {"ziccrse", ISA_SPEC_CLASS_NONE, 1, 0},
331
332 {"zicfiss", ISA_SPEC_CLASS_NONE, 1, 0},
333 {"zicfilp", ISA_SPEC_CLASS_NONE, 1, 0},
334
335 {"zimop", ISA_SPEC_CLASS_NONE, 1, 0},
336 {"zcmop", ISA_SPEC_CLASS_NONE, 1, 0},
337
338 {"zicntr", ISA_SPEC_CLASS_NONE, 2, 0},
339 {"zihpm", ISA_SPEC_CLASS_NONE, 2, 0},
340
341 {"zk", ISA_SPEC_CLASS_NONE, 1, 0},
342 {"zkn", ISA_SPEC_CLASS_NONE, 1, 0},
343 {"zks", ISA_SPEC_CLASS_NONE, 1, 0},
344
345 {"ztso", ISA_SPEC_CLASS_NONE, 1, 0},
346
347 {"zve32x", ISA_SPEC_CLASS_NONE, 1, 0},
348 {"zve32f", ISA_SPEC_CLASS_NONE, 1, 0},
349 {"zve64x", ISA_SPEC_CLASS_NONE, 1, 0},
350 {"zve64f", ISA_SPEC_CLASS_NONE, 1, 0},
351 {"zve64d", ISA_SPEC_CLASS_NONE, 1, 0},
352
353 {"zvbb", ISA_SPEC_CLASS_NONE, 1, 0},
354 {"zvbc", ISA_SPEC_CLASS_NONE, 1, 0},
355 {"zvkb", ISA_SPEC_CLASS_NONE, 1, 0},
356 {"zvkg", ISA_SPEC_CLASS_NONE, 1, 0},
357 {"zvkned", ISA_SPEC_CLASS_NONE, 1, 0},
358 {"zvknha", ISA_SPEC_CLASS_NONE, 1, 0},
359 {"zvknhb", ISA_SPEC_CLASS_NONE, 1, 0},
360 {"zvksed", ISA_SPEC_CLASS_NONE, 1, 0},
361 {"zvksh", ISA_SPEC_CLASS_NONE, 1, 0},
362 {"zvkn", ISA_SPEC_CLASS_NONE, 1, 0},
363 {"zvknc", ISA_SPEC_CLASS_NONE, 1, 0},
364 {"zvkng", ISA_SPEC_CLASS_NONE, 1, 0},
365 {"zvks", ISA_SPEC_CLASS_NONE, 1, 0},
366 {"zvksc", ISA_SPEC_CLASS_NONE, 1, 0},
367 {"zvksg", ISA_SPEC_CLASS_NONE, 1, 0},
368 {"zvkt", ISA_SPEC_CLASS_NONE, 1, 0},
369
370 {"zvl32b", ISA_SPEC_CLASS_NONE, 1, 0},
371 {"zvl64b", ISA_SPEC_CLASS_NONE, 1, 0},
372 {"zvl128b", ISA_SPEC_CLASS_NONE, 1, 0},
373 {"zvl256b", ISA_SPEC_CLASS_NONE, 1, 0},
374 {"zvl512b", ISA_SPEC_CLASS_NONE, 1, 0},
375 {"zvl1024b", ISA_SPEC_CLASS_NONE, 1, 0},
376 {"zvl2048b", ISA_SPEC_CLASS_NONE, 1, 0},
377 {"zvl4096b", ISA_SPEC_CLASS_NONE, 1, 0},
378 {"zvl8192b", ISA_SPEC_CLASS_NONE, 1, 0},
379 {"zvl16384b", ISA_SPEC_CLASS_NONE, 1, 0},
380 {"zvl32768b", ISA_SPEC_CLASS_NONE, 1, 0},
381 {"zvl65536b", ISA_SPEC_CLASS_NONE, 1, 0},
382
383 {"zfbfmin", ISA_SPEC_CLASS_NONE, 1, 0},
384 {"zfh", ISA_SPEC_CLASS_NONE, 1, 0},
385 {"zfhmin", ISA_SPEC_CLASS_NONE, 1, 0},
386 {"zvfbfmin", ISA_SPEC_CLASS_NONE, 1, 0},
387 {"zvfbfwma", ISA_SPEC_CLASS_NONE, 1, 0},
388 {"zvfhmin", ISA_SPEC_CLASS_NONE, 1, 0},
389 {"zvfh", ISA_SPEC_CLASS_NONE, 1, 0},
390
391 {"zfa", ISA_SPEC_CLASS_NONE, 1, 0},
392
393 {"zmmul", ISA_SPEC_CLASS_NONE, 1, 0},
394
395 {"zca", ISA_SPEC_CLASS_NONE, 1, 0},
396 {"zcb", ISA_SPEC_CLASS_NONE, 1, 0},
397 {"zce", ISA_SPEC_CLASS_NONE, 1, 0},
398 {"zcf", ISA_SPEC_CLASS_NONE, 1, 0},
399 {"zcd", ISA_SPEC_CLASS_NONE, 1, 0},
400 {"zcmp", ISA_SPEC_CLASS_NONE, 1, 0},
401 {"zcmt", ISA_SPEC_CLASS_NONE, 1, 0},
402
403 {"smaia", ISA_SPEC_CLASS_NONE, 1, 0},
404 {"smepmp", ISA_SPEC_CLASS_NONE, 1, 0},
405 {"smstateen", ISA_SPEC_CLASS_NONE, 1, 0},
406
407 {"ssaia", ISA_SPEC_CLASS_NONE, 1, 0},
408 {"sscofpmf", ISA_SPEC_CLASS_NONE, 1, 0},
409 {"ssstateen", ISA_SPEC_CLASS_NONE, 1, 0},
410 {"sstc", ISA_SPEC_CLASS_NONE, 1, 0},
411
412 {"svinval", ISA_SPEC_CLASS_NONE, 1, 0},
413 {"svnapot", ISA_SPEC_CLASS_NONE, 1, 0},
414 {"svpbmt", ISA_SPEC_CLASS_NONE, 1, 0},
415 {"svvptc", ISA_SPEC_CLASS_NONE, 1, 0},
416
417 {"xcvmac", ISA_SPEC_CLASS_NONE, 1, 0},
418 {"xcvalu", ISA_SPEC_CLASS_NONE, 1, 0},
419 {"xcvelw", ISA_SPEC_CLASS_NONE, 1, 0},
420 {"xcvsimd", ISA_SPEC_CLASS_NONE, 1, 0},
421 {"xcvbi", ISA_SPEC_CLASS_NONE, 1, 0},
422
423 {"xtheadba", ISA_SPEC_CLASS_NONE, 1, 0},
424 {"xtheadbb", ISA_SPEC_CLASS_NONE, 1, 0},
425 {"xtheadbs", ISA_SPEC_CLASS_NONE, 1, 0},
426 {"xtheadcmo", ISA_SPEC_CLASS_NONE, 1, 0},
427 {"xtheadcondmov", ISA_SPEC_CLASS_NONE, 1, 0},
428 {"xtheadfmemidx", ISA_SPEC_CLASS_NONE, 1, 0},
429 {"xtheadfmv", ISA_SPEC_CLASS_NONE, 1, 0},
430 {"xtheadint", ISA_SPEC_CLASS_NONE, 1, 0},
431 {"xtheadmac", ISA_SPEC_CLASS_NONE, 1, 0},
432 {"xtheadmemidx", ISA_SPEC_CLASS_NONE, 1, 0},
433 {"xtheadmempair", ISA_SPEC_CLASS_NONE, 1, 0},
434 {"xtheadsync", ISA_SPEC_CLASS_NONE, 1, 0},
435 {"xtheadvector", ISA_SPEC_CLASS_NONE, 1, 0},
436
437 {"xventanacondops", ISA_SPEC_CLASS_NONE, 1, 0},
438
439 {"xsfvcp", ISA_SPEC_CLASS_NONE, 1, 0},
440 {"xsfcease", ISA_SPEC_CLASS_NONE, 1, 0},
441 {"xsfvqmaccqoq", ISA_SPEC_CLASS_NONE, 1, 0},
442 {"xsfvqmaccdod", ISA_SPEC_CLASS_NONE, 1, 0},
443 {"xsfvfnrclipxfqf", ISA_SPEC_CLASS_NONE, 1, 0},
444
445 /* Terminate the list. */
446 {NULL, ISA_SPEC_CLASS_NONE, 0, 0}
447 };
448
449 /* Combine extensions defined in this table */
450 static const struct riscv_ext_version riscv_combine_info[] =
451 {
452 {"a", ISA_SPEC_CLASS_20191213, 2, 1},
453 {"b", ISA_SPEC_CLASS_NONE, 1, 0},
454 {"zk", ISA_SPEC_CLASS_NONE, 1, 0},
455 {"zkn", ISA_SPEC_CLASS_NONE, 1, 0},
456 {"zks", ISA_SPEC_CLASS_NONE, 1, 0},
457 {"zvkn", ISA_SPEC_CLASS_NONE, 1, 0},
458 {"zvknc", ISA_SPEC_CLASS_NONE, 1, 0},
459 {"zvkng", ISA_SPEC_CLASS_NONE, 1, 0},
460 {"zvks", ISA_SPEC_CLASS_NONE, 1, 0},
461 {"zvksc", ISA_SPEC_CLASS_NONE, 1, 0},
462 {"zvksg", ISA_SPEC_CLASS_NONE, 1, 0},
463 /* Terminate the list. */
464 {NULL, ISA_SPEC_CLASS_NONE, 0, 0}
465 };
466
467 static const riscv_cpu_info riscv_cpu_tables[] =
468 {
469 #define RISCV_CORE(CORE_NAME, ARCH, TUNE) \
470 {CORE_NAME, ARCH, TUNE},
471 #include "../../../config/riscv/riscv-cores.def"
472 {NULL, NULL, NULL}
473 };
474
475 static const char *riscv_tunes[] =
476 {
477 #define RISCV_TUNE(TUNE_NAME, PIPELINE_MODEL, TUNE_INFO) \
478 TUNE_NAME,
479 #include "../../../config/riscv/riscv-cores.def"
480 NULL
481 };
482
483 static const char *riscv_supported_std_ext (void);
484
485 bool riscv_subset_list::parse_failed = false;
486
487 static riscv_subset_list *cmdline_subset_list = NULL;
488
489 const riscv_subset_list *riscv_cmdline_subset_list ()
490 {
491 return cmdline_subset_list;
492 }
493
494 /* struct for recording multi-lib info. */
495 struct riscv_multi_lib_info_t {
496 std::string path;
497 std::string arch_str;
498 std::string abi_str;
499 std::vector<std::string> conds;
500 riscv_subset_list *subset_list;
501
502 static bool parse (struct riscv_multi_lib_info_t *,
503 const std::string &,
504 const std::vector<std::string> &);
505 };
506
507 /* Flag for checking if there is no suitable multi-lib found. */
508 static bool riscv_no_matched_multi_lib;
509
510 /* Used for record value of -march and -mabi. */
511 static std::string riscv_current_arch_str;
512 static std::string riscv_current_abi_str;
513
514 riscv_subset_t::riscv_subset_t ()
515 : name (), major_version (0), minor_version (0), next (NULL),
516 explicit_version_p (false), implied_p (false)
517 {
518 }
519
520 riscv_subset_list::riscv_subset_list (const char *arch, location_t loc)
521 : m_arch (arch), m_loc (loc), m_head (NULL), m_tail (NULL), m_xlen (0),
522 m_subset_num (0)
523 {
524 }
525
526 riscv_subset_list::~riscv_subset_list ()
527 {
528 if (!m_head)
529 return;
530
531 riscv_subset_t *item = this->m_head;
532 while (item != NULL)
533 {
534 riscv_subset_t *next = item->next;
535 delete item;
536 item = next;
537 }
538 }
539
540 /* Compute the match score of two arch string, return 0 if incompatible. */
541 int
542 riscv_subset_list::match_score (riscv_subset_list *list) const
543 {
544 riscv_subset_t *s;
545 int score = 0;
546 bool has_a_ext, list_has_a_ext;
547
548 /* Impossible to match if XLEN is different. */
549 if (list->m_xlen != this->m_xlen)
550 return 0;
551
552 /* There is different code gen in libstdc++ and libatomic between w/ A-ext
553 and w/o A-ext, and it not work if using soft and hard atomic mechanism
554 at same time, so they are incompatible. */
555 has_a_ext = this->lookup ("a") != NULL;
556 list_has_a_ext = list->lookup ("a") != NULL;
557
558 if (has_a_ext != list_has_a_ext)
559 return 0;
560
561
562 /* list must be subset of current this list, otherwise it not safe to
563 link.
564 TODO: We might give different weight for each extension, but the rule could
565 be complicated.
566 TODO: We might consider the version of each extension. */
567 for (s = list->m_head; s != NULL; s = s->next)
568 if (this->lookup (s->name.c_str ()) != NULL)
569 score++;
570 else
571 return 0;
572
573 return score;
574 }
575
576 /* Get the rank for single-letter subsets, lower value meaning higher
577 priority. */
578
579 static int
580 single_letter_subset_rank (char ext)
581 {
582 int rank;
583
584 switch (ext)
585 {
586 case 'i':
587 return 0;
588 case 'e':
589 return 1;
590 default:
591 break;
592 }
593
594 const char *all_ext = riscv_supported_std_ext ();
595 const char *ext_pos = strchr (all_ext, ext);
596 if (ext_pos == NULL)
597 /* If got an unknown extension letter, then give it an alphabetical
598 order, but after all known standard extension. */
599 rank = strlen (all_ext) + ext - 'a';
600 else
601 rank = (int)(ext_pos - all_ext) + 2 /* e and i has higher rank. */;
602
603 return rank;
604 }
605
606 /* Get the rank for multi-letter subsets, lower value meaning higher
607 priority. */
608
609 static int
610 multi_letter_subset_rank (const std::string &subset)
611 {
612 gcc_assert (subset.length () >= 2);
613 int high_order = -1;
614 int low_order = 0;
615 /* The order between multi-char extensions: s -> z -> x. */
616 char multiletter_class = subset[0];
617 switch (multiletter_class)
618 {
619 case 'z':
620 high_order = 0;
621 break;
622 case 's':
623 high_order = 1;
624 break;
625 case 'x':
626 high_order = 2;
627 break;
628 default:
629 gcc_unreachable ();
630 return -1;
631 }
632
633 if (multiletter_class == 'z')
634 /* Order for z extension on spec: If multiple "Z" extensions are named, they
635 should be ordered first by category, then alphabetically within a
636 category - for example, "Zicsr_Zifencei_Zam". */
637 low_order = single_letter_subset_rank (subset[1]);
638 else
639 low_order = 0;
640
641 return (high_order << 8) + low_order;
642 }
643
644 /* subset compare
645
646 Returns an integral value indicating the relationship between the subsets:
647 Return value indicates
648 -1 B has higher order than A.
649 0 A and B are same subset.
650 1 A has higher order than B.
651
652 */
653
654 static int
655 subset_cmp (const std::string &a, const std::string &b)
656 {
657 if (a == b)
658 return 0;
659
660 size_t a_len = a.length ();
661 size_t b_len = b.length ();
662
663 /* Single-letter extension always get higher order than
664 multi-letter extension. */
665 if (a_len == 1 && b_len != 1)
666 return 1;
667
668 if (a_len != 1 && b_len == 1)
669 return -1;
670
671 if (a_len == 1 && b_len == 1)
672 {
673 int rank_a = single_letter_subset_rank (a[0]);
674 int rank_b = single_letter_subset_rank (b[0]);
675
676 if (rank_a < rank_b)
677 return 1;
678 else
679 return -1;
680 }
681 else
682 {
683 int rank_a = multi_letter_subset_rank(a);
684 int rank_b = multi_letter_subset_rank(b);
685
686 /* Using alphabetical/lexicographical order if they have same rank. */
687 if (rank_a == rank_b)
688 /* The return value of strcmp has opposite meaning. */
689 return -strcmp (a.c_str (), b.c_str ());
690 else
691 return (rank_a < rank_b) ? 1 : -1;
692 }
693 }
694
695 /* Return true if EXT is a standard extension. */
696
697 static bool
698 standard_extensions_p (const char *ext)
699 {
700 const riscv_ext_version *ext_ver;
701 for (ext_ver = &riscv_ext_version_table[0]; ext_ver->name != NULL; ++ext_ver)
702 if (strcmp (ext, ext_ver->name) == 0)
703 return true;
704 return false;
705 }
706
707 /* Add new subset to list. */
708
709 void
710 riscv_subset_list::add (const char *subset, int major_version,
711 int minor_version, bool explicit_version_p,
712 bool implied_p)
713 {
714 riscv_subset_t *ext = lookup (subset);
715
716 if (ext)
717 {
718 if (ext->implied_p)
719 {
720 /* We won't add impiled `ext` if it already in list. */
721 gcc_assert (!implied_p);
722 ext->implied_p = implied_p;
723 ext->major_version = major_version;
724 ext->minor_version = minor_version;
725 }
726 else
727 {
728 /* The extension is already in the list. */
729 if (!m_allow_adding_dup
730 || ext->major_version != major_version
731 || ext->minor_version != minor_version)
732 error_at (
733 m_loc,
734 "%<-march=%s%>: extension %qs appear more than one time",
735 m_arch,
736 subset);
737 }
738 return;
739 }
740 else if (strlen (subset) == 1 && !standard_extensions_p (subset))
741 {
742 error_at (m_loc,
743 "%<-march=%s%>: extension %qs is unsupported standard single "
744 "letter extension",
745 m_arch, subset);
746 return;
747 }
748 else if (subset[0] == 'z' && !standard_extensions_p (subset))
749 {
750 error_at (m_loc,
751 "%<-march=%s%>: extension %qs starts with 'z' but is "
752 "unsupported standard extension",
753 m_arch, subset);
754 return;
755 }
756 else if (subset[0] == 's' && !standard_extensions_p (subset))
757 {
758 error_at (m_loc,
759 "%<-march=%s%>: extension %qs starts with 's' but is "
760 "unsupported standard supervisor extension",
761 m_arch, subset);
762 return;
763 }
764 else if (subset[0] == 'x' && !standard_extensions_p (subset))
765 {
766 error_at (m_loc,
767 "%<-march=%s%>: extension %qs starts with 'x' but is "
768 "unsupported non-standard extension",
769 m_arch, subset);
770 return;
771 }
772
773 m_subset_num++;
774 riscv_subset_t *s = new riscv_subset_t ();
775 riscv_subset_t *itr;
776
777 if (m_head == NULL)
778 m_head = s;
779
780 s->name = subset;
781 s->major_version = major_version;
782 s->minor_version = minor_version;
783 s->explicit_version_p = explicit_version_p;
784 s->implied_p = implied_p;
785 s->next = NULL;
786
787 if (m_tail == NULL)
788 {
789 m_tail = s;
790 return;
791 }
792
793 /* e, i or g should be first subext, never come here. */
794 gcc_assert (subset[0] != 'e'
795 && subset[0] != 'i'
796 && subset[0] != 'g');
797
798 if (m_tail == m_head)
799 {
800 gcc_assert (m_head->next == NULL);
801 m_head->next = s;
802 m_tail = s;
803 return;
804 }
805
806 gcc_assert (m_head->next != NULL);
807
808 /* Subset list must in canonical order, but implied subset won't
809 add in canonical order. */
810 for (itr = m_head; itr->next != NULL; itr = itr->next)
811 {
812 riscv_subset_t *next = itr->next;
813 int cmp = subset_cmp (s->name, next->name);
814 gcc_assert (cmp != 0);
815
816 if (cmp > 0)
817 {
818 s->next = next;
819 itr->next = s;
820 return;
821 }
822 }
823
824 /* Insert at tail of the list. */
825 itr->next = s;
826 m_tail = s;
827 }
828
829 static void
830 get_default_version (const char *ext,
831 unsigned int *major_version,
832 unsigned int *minor_version)
833 {
834 const riscv_ext_version *ext_ver;
835 for (ext_ver = &riscv_ext_version_table[0];
836 ext_ver->name != NULL;
837 ++ext_ver)
838 if (strcmp (ext, ext_ver->name) == 0)
839 {
840 if ((ext_ver->isa_spec_class == riscv_isa_spec) ||
841 (ext_ver->isa_spec_class == ISA_SPEC_CLASS_NONE))
842 {
843 *major_version = ext_ver->major_version;
844 *minor_version = ext_ver->minor_version;
845 return;
846 }
847 }
848
849 /* Not found version info. */
850 *major_version = 0;
851 *minor_version = 0;
852 }
853
854 /* Add new subset to list, but using default version from ISA spec version. */
855
856 void
857 riscv_subset_list::add (const char *subset, bool implied_p)
858 {
859 unsigned int major_version = 0, minor_version = 0;
860
861 get_default_version (subset, &major_version, &minor_version);
862
863 add (subset, major_version, minor_version, false, implied_p);
864 }
865
866 /* Convert subset info to string with explicit version info,
867 VERSION_P to determine append version info or not. */
868
869 std::string
870 riscv_subset_list::to_string (bool version_p) const
871 {
872 std::ostringstream oss;
873 oss << "rv" << m_xlen;
874
875 bool first = true;
876 riscv_subset_t *subset;
877
878 bool skip_zifencei = false;
879 bool skip_zaamo_zalrsc = false;
880 bool skip_zicsr = false;
881 bool skip_b = false;
882 bool i2p0 = false;
883
884 /* For RISC-V ISA version 2.2 or earlier version, zicsr and zifencei is
885 included in the base ISA. */
886 if (riscv_isa_spec == ISA_SPEC_CLASS_2P2)
887 {
888 skip_zifencei = true;
889 skip_zicsr = true;
890 }
891
892 for (subset = m_head; subset != NULL; subset = subset->next)
893 if (subset->name == "i")
894 {
895 i2p0 = subset->major_version == 2 && subset->minor_version == 0;
896 break;
897 }
898
899 #ifndef HAVE_AS_MISA_SPEC
900 /* Skip since older binutils doesn't recognize zicsr. */
901 skip_zicsr = true;
902 #endif
903 #ifndef HAVE_AS_MARCH_ZIFENCEI
904 /* Skip since older binutils doesn't recognize zifencei, we made
905 a mistake in that binutils 2.35 supports zicsr but not zifencei. */
906 skip_zifencei = true;
907 #endif
908 #ifndef HAVE_AS_MARCH_ZAAMO_ZALRSC
909 /* Skip since binutils 2.42 and earlier don't recognize zaamo/zalrsc.
910 Expanding 'a' to zaamo/zalrsc would otherwise break compilations
911 for users with an older version of binutils. */
912 skip_zaamo_zalrsc = true;
913 #endif
914 #ifndef HAVE_AS_MARCH_B
915 /* Skip since binutils 2.42 and earlier don't recognize b. */
916 skip_b = true;
917 #endif
918
919 for (subset = m_head; subset != NULL; subset = subset->next)
920 {
921 if (((subset->implied_p && skip_zifencei) || i2p0) &&
922 subset->name == "zifencei")
923 continue;
924
925 if (((subset->implied_p && skip_zicsr) || i2p0) &&
926 subset->name == "zicsr")
927 continue;
928
929 if (skip_zaamo_zalrsc && subset->name == "zaamo")
930 continue;
931
932 if (skip_zaamo_zalrsc && subset->name == "zalrsc")
933 continue;
934
935 if (skip_b && subset->name == "b")
936 continue;
937
938 /* For !version_p, we only separate extension with underline for
939 multi-letter extension. */
940 if (!first &&
941 (version_p
942 || subset->explicit_version_p
943 || subset->name.length() > 1))
944 oss << '_';
945 first = false;
946
947 oss << subset->name;
948
949 /* Let binutils decide the extension version if we don't know. */
950 if ((version_p || subset->explicit_version_p) &&
951 (subset->major_version != 0 || subset->minor_version != 0))
952 oss << subset->major_version
953 << 'p'
954 << subset->minor_version;
955 }
956
957 return oss.str ();
958 }
959
960 /* Find subset in list with version checking, return NULL if not found.
961 major/minor version checking can be ignored if major_version/minor_version
962 is RISCV_DONT_CARE_VERSION. */
963
964 riscv_subset_t *
965 riscv_subset_list::lookup (const char *subset, int major_version,
966 int minor_version) const
967 {
968 riscv_subset_t *s;
969
970 for (s = m_head; s != NULL; s = s->next)
971 if (strcasecmp (s->name.c_str (), subset) == 0)
972 {
973 if ((major_version != RISCV_DONT_CARE_VERSION)
974 && (s->major_version != major_version))
975 return NULL;
976
977 if ((minor_version != RISCV_DONT_CARE_VERSION)
978 && (s->minor_version != minor_version))
979 return NULL;
980
981 return s;
982 }
983
984 return s;
985 }
986
987 /* Return string which contains all supported standard extensions in
988 canonical order. */
989
990 static const char *
991 riscv_supported_std_ext (void)
992 {
993 return "mafdqlcbkjtpvnh";
994 }
995
996 /* Parsing subset version.
997
998 Return Value:
999 Points to the end of version
1000
1001 Arguments:
1002 `ext`: This extension.
1003 `p`: Current parsing position.
1004 `major_version`: Parsing result of major version, using
1005 default_major_version if version is not present in arch string.
1006 `minor_version`: Parsing result of minor version, set to 0 if version is
1007 not present in arch string, but set to `default_minor_version` if
1008 `major_version` using default_major_version.
1009 `std_ext_p`: True if parsing std extension.
1010 `explicit_version_p`: True if this subset is not using default version. */
1011
1012 const char *
1013 riscv_subset_list::parsing_subset_version (const char *ext,
1014 const char *p,
1015 unsigned *major_version,
1016 unsigned *minor_version,
1017 bool std_ext_p,
1018 bool *explicit_version_p)
1019 {
1020 bool major_p = true;
1021 unsigned version = 0;
1022 unsigned major = 0;
1023 unsigned minor = 0;
1024 *explicit_version_p = false;
1025
1026 /* If we got `p`, that means we are still parsing standard extension. */
1027 gcc_assert (std_ext_p || *p != 'p');
1028
1029 if (*p != 'p') {
1030 for (; *p; ++p)
1031 {
1032 if (*p == 'p')
1033 {
1034 if (!ISDIGIT (*(p+1)))
1035 {
1036 error_at (m_loc, "%<-march=%s%>: expect number "
1037 "after %<%dp%>", m_arch, version);
1038 return NULL;
1039 }
1040 if (!major_p)
1041 {
1042 error_at (m_loc, "%<-march=%s%>: for %<%s%dp%dp?%>, version "
1043 "number with more than 2 level is not supported",
1044 m_arch, ext, major, version);
1045 return NULL;
1046 }
1047 major = version;
1048 major_p = false;
1049 version = 0;
1050 }
1051 else if (ISDIGIT (*p))
1052 version = (version * 10) + (*p - '0');
1053 else
1054 break;
1055 }
1056 }
1057
1058 if (major_p)
1059 major = version;
1060 else
1061 minor = version;
1062
1063 if (major == 0 && minor == 0)
1064 get_default_version (ext, major_version, minor_version);
1065 else
1066 {
1067 *explicit_version_p = true;
1068 *major_version = major;
1069 *minor_version = minor;
1070 }
1071 return p;
1072 }
1073
1074 /* Parsing function for base extensions, rv[32|64][i|e|g]
1075
1076 Return Value:
1077 Points to the end of extensions, return NULL if any error.
1078
1079 Arguments:
1080 `p`: Current parsing position. */
1081 const char *
1082 riscv_subset_list::parse_base_ext (const char *p)
1083 {
1084 unsigned major_version = 0;
1085 unsigned minor_version = 0;
1086 bool explicit_version_p = false;
1087
1088 if (startswith (p, "rv32"))
1089 {
1090 m_xlen = 32;
1091 p += 4;
1092 }
1093 else if (startswith (p, "rv64"))
1094 {
1095 m_xlen = 64;
1096 p += 4;
1097 }
1098 else
1099 {
1100 error_at (m_loc, "%<-march=%s%>: ISA string must begin with rv32 or rv64",
1101 m_arch);
1102 return NULL;
1103 }
1104
1105 /* First letter must start with i, e or g. */
1106 switch (*p)
1107 {
1108 case 'i':
1109 p++;
1110 p = parsing_subset_version ("i", p, &major_version, &minor_version,
1111 /* std_ext_p= */ true, &explicit_version_p);
1112 add ("i", major_version, minor_version, explicit_version_p, false);
1113 break;
1114
1115 case 'e':
1116 p++;
1117 p = parsing_subset_version ("e", p, &major_version, &minor_version,
1118 /* std_ext_p= */ true, &explicit_version_p);
1119
1120 add ("e", major_version, minor_version, explicit_version_p, false);
1121
1122 if (m_xlen > 64)
1123 {
1124 error_at (m_loc, "%<-march=%s%>: rv%de is not a valid base ISA",
1125 m_arch, m_xlen);
1126 return NULL;
1127 }
1128 break;
1129
1130 case 'g':
1131 p++;
1132 p = parsing_subset_version ("g", p, &major_version, &minor_version,
1133 /* std_ext_p= */ true, &explicit_version_p);
1134 if (major_version != 0 || minor_version != 0)
1135 {
1136 warning_at (m_loc, 0, "version of %<g%> will be omitted, please "
1137 "specify version for individual extension");
1138 }
1139
1140 /* We have special rule for G, we disallow rv32gm2p but allow rv32g_zicsr
1141 here, basically we treating G expand to imafd and implied zicsr and
1142 zifencei. */
1143
1144 add ("i", false);
1145 add ("m", false);
1146 add ("a", false);
1147 add ("f", false);
1148 add ("d", false);
1149 add ("zicsr", true);
1150 add ("zifencei", true);
1151
1152 break;
1153
1154 default:
1155 error_at (m_loc, "%<-march=%s%>: first ISA subset must be %<e%>, "
1156 "%<i%> or %<g%>", m_arch);
1157 return NULL;
1158 }
1159 return p;
1160 }
1161
1162 /* Parsing function for one standard extensions.
1163
1164 Return Value:
1165 Points to the end of extensions.
1166
1167 Arguments:
1168 `p`: Current parsing position.
1169 `exact_single_p`: True if input string is exactly an extension and end
1170 with '\0'. */
1171
1172 const char *
1173 riscv_subset_list::parse_single_std_ext (const char *p, bool exact_single_p)
1174 {
1175 if (*p == 'x' || *p == 's' || *p == 'z')
1176 {
1177 error_at (m_loc,
1178 "%<-march=%s%>: Not single-letter extension. "
1179 "%<%c%>",
1180 m_arch, *p);
1181 return nullptr;
1182 }
1183
1184 if (exact_single_p && strlen (p) > 1)
1185 {
1186 return nullptr;
1187 }
1188
1189 unsigned major_version = 0;
1190 unsigned minor_version = 0;
1191 bool explicit_version_p = false;
1192 char subset[2] = {0, 0};
1193
1194 subset[0] = *p;
1195
1196 p++;
1197
1198 p = parsing_subset_version (subset, p, &major_version, &minor_version,
1199 /* std_ext_p= */ true, &explicit_version_p);
1200
1201 add (subset, major_version, minor_version, explicit_version_p, false);
1202 return p;
1203 }
1204
1205 /* Check any implied extensions for EXT. */
1206 void
1207 riscv_subset_list::handle_implied_ext (const char *ext)
1208 {
1209 const riscv_implied_info_t *implied_info;
1210 for (implied_info = &riscv_implied_info[0];
1211 implied_info->ext;
1212 ++implied_info)
1213 {
1214 if (!implied_info->match (this, ext))
1215 continue;
1216
1217 /* Skip if implied extension already present. */
1218 if (lookup (implied_info->implied_ext))
1219 continue;
1220
1221 /* Version of implied extension will get from current ISA spec
1222 version. */
1223 add (implied_info->implied_ext, true);
1224
1225 /* Recursively add implied extension by implied_info->implied_ext. */
1226 handle_implied_ext (implied_info->implied_ext);
1227 }
1228
1229 /* For RISC-V ISA version 2.2 or earlier version, zicsr and zifence is
1230 included in the base ISA. */
1231 if (riscv_isa_spec == ISA_SPEC_CLASS_2P2)
1232 {
1233 if (lookup ("zicsr") == NULL)
1234 add ("zicsr", true);
1235
1236 if (lookup ("zifencei") == NULL)
1237 add ("zifencei", true);
1238 }
1239 }
1240
1241 /* Check that all implied extensions are included. */
1242 bool
1243 riscv_subset_list::check_implied_ext ()
1244 {
1245 riscv_subset_t *itr;
1246 for (itr = m_head; itr != NULL; itr = itr->next)
1247 {
1248 const riscv_implied_info_t *implied_info;
1249 for (implied_info = &riscv_implied_info[0]; implied_info->ext;
1250 ++implied_info)
1251 {
1252 if (!implied_info->match (this, itr))
1253 continue;
1254
1255 if (!lookup (implied_info->implied_ext))
1256 return false;
1257 }
1258 }
1259 return true;
1260 }
1261
1262 /* Check any combine extensions for EXT. */
1263 void
1264 riscv_subset_list::handle_combine_ext ()
1265 {
1266 const riscv_ext_version *combine_info;
1267 const riscv_implied_info_t *implied_info;
1268 bool is_combined = false;
1269
1270 for (combine_info = &riscv_combine_info[0]; combine_info->name;
1271 ++combine_info)
1272 {
1273 /* Skip if combine extensions are present */
1274 if (lookup (combine_info->name))
1275 continue;
1276
1277 /* Find all extensions of the combine extension */
1278 for (implied_info = &riscv_implied_info[0]; implied_info->ext;
1279 ++implied_info)
1280 {
1281 if (!implied_info->match (this, combine_info->name))
1282 continue;
1283
1284 if (lookup (implied_info->implied_ext))
1285 is_combined = true;
1286 else
1287 {
1288 is_combined = false;
1289 break;
1290 }
1291 }
1292
1293 /* Add combine extensions */
1294 if (is_combined)
1295 {
1296 if (lookup (combine_info->name) == NULL)
1297 {
1298 add (combine_info->name, combine_info->major_version,
1299 combine_info->minor_version, false, true);
1300 }
1301 }
1302 }
1303 }
1304
1305 void
1306 riscv_subset_list::check_conflict_ext ()
1307 {
1308 if (lookup ("zcf") && m_xlen == 64)
1309 error_at (m_loc, "%<-march=%s%>: zcf extension supports in rv32 only",
1310 m_arch);
1311
1312 if (lookup ("zfinx") && lookup ("f"))
1313 error_at (m_loc,
1314 "%<-march=%s%>: z*inx conflicts with floating-point "
1315 "extensions",
1316 m_arch);
1317
1318 /* 'H' hypervisor extension requires base ISA with 32 registers. */
1319 if (lookup ("e") && lookup ("h"))
1320 error_at (m_loc, "%<-march=%s%>: h extension requires i extension", m_arch);
1321
1322 if (lookup ("zcd"))
1323 {
1324 if (lookup ("zcmt"))
1325 error_at (m_loc, "%<-march=%s%>: zcd conflicts with zcmt", m_arch);
1326 if (lookup ("zcmp"))
1327 error_at (m_loc, "%<-march=%s%>: zcd conflicts with zcmp", m_arch);
1328 }
1329
1330 if ((lookup ("v") || lookup ("zve32x")
1331 || lookup ("zve64x") || lookup ("zve32f")
1332 || lookup ("zve64f") || lookup ("zve64d")
1333 || lookup ("zvl32b") || lookup ("zvl64b")
1334 || lookup ("zvl128b") || lookup ("zvfh"))
1335 && lookup ("xtheadvector"))
1336 error_at (m_loc, "%<-march=%s%>: xtheadvector conflicts with vector "
1337 "extension or its sub-extensions", m_arch);
1338 }
1339
1340 /* Parsing function for multi-letter extensions.
1341
1342 Return Value:
1343 Points to the end of extensions.
1344
1345 Arguments:
1346 `p`: Current parsing position.
1347 `ext_type`: What kind of extensions, 's', 'z' or 'x'.
1348 `ext_type_str`: Full name for kind of extension.
1349 `exact_single_p`: True if input string is exactly an extension and end
1350 with '\0'. */
1351
1352
1353 const char *
1354 riscv_subset_list::parse_single_multiletter_ext (const char *p,
1355 const char *ext_type,
1356 const char *ext_type_str,
1357 bool exact_single_p)
1358 {
1359 unsigned major_version = 0;
1360 unsigned minor_version = 0;
1361 size_t ext_type_len = strlen (ext_type);
1362
1363 if (strncmp (p, ext_type, ext_type_len) != 0)
1364 return NULL;
1365
1366 char *subset = xstrdup (p);
1367 const char *end_of_version;
1368 bool explicit_version_p = false;
1369 char *q = subset;
1370 char *ext;
1371 char backup;
1372 size_t len = strlen (p);
1373 size_t end_of_version_pos, i;
1374 bool found_any_number = false;
1375 bool found_minor_version = false;
1376
1377 if (!exact_single_p)
1378 {
1379 /* Extension may not ended with '\0', may come with another extension
1380 which concat by '_' */
1381 /* Parse until end of this extension including version number. */
1382 while (*++q != '\0' && *q != '_')
1383 ;
1384
1385 len = q - subset;
1386 }
1387
1388 end_of_version_pos = len;
1389 /* Find the begin of version string. */
1390 for (i = len -1; i > 0; --i)
1391 {
1392 if (ISDIGIT (subset[i]))
1393 {
1394 found_any_number = true;
1395 continue;
1396 }
1397 /* Might be version seperator, but need to check one more char,
1398 we only allow <major>p<minor>, so we could stop parsing if found
1399 any more `p`. */
1400 if (subset[i] == 'p' &&
1401 !found_minor_version &&
1402 found_any_number && ISDIGIT (subset[i-1]))
1403 {
1404 found_minor_version = true;
1405 continue;
1406 }
1407
1408 end_of_version_pos = i + 1;
1409 break;
1410 }
1411
1412 backup = subset[end_of_version_pos];
1413 subset[end_of_version_pos] = '\0';
1414 ext = xstrdup (subset);
1415 subset[end_of_version_pos] = backup;
1416
1417 end_of_version
1418 = parsing_subset_version (ext, subset + end_of_version_pos, &major_version,
1419 &minor_version, /* std_ext_p= */ false,
1420 &explicit_version_p);
1421 free (ext);
1422
1423 if (end_of_version == NULL)
1424 {
1425 free (subset);
1426 return NULL;
1427 }
1428
1429 subset[end_of_version_pos] = '\0';
1430
1431 if (strlen (subset) == 1)
1432 {
1433 error_at (m_loc, "%<-march=%s%>: name of %s must be more than 1 letter",
1434 m_arch, ext_type_str);
1435 free (subset);
1436 return NULL;
1437 }
1438
1439 add (subset, major_version, minor_version, explicit_version_p, false);
1440 p += end_of_version - subset;
1441 free (subset);
1442
1443 if (*p != '\0' && *p != '_')
1444 {
1445 error_at (m_loc, "%<-march=%s%>: %s must separate with %<_%>",
1446 m_arch, ext_type_str);
1447 return NULL;
1448 }
1449
1450 return p;
1451
1452 }
1453
1454 /* Parsing function for a single-letter or multi-letter extensions.
1455
1456 Return Value:
1457 Points to the end of extensions.
1458
1459 Arguments:
1460 `p`: Current parsing position.
1461 `exact_single_p`: True if input string is exactly an extension and end
1462 with '\0'. */
1463
1464 const char *
1465 riscv_subset_list::parse_single_ext (const char *p, bool exact_single_p)
1466 {
1467 switch (p[0])
1468 {
1469 case 'x':
1470 return parse_single_multiletter_ext (p, "x", "non-standard extension",
1471 exact_single_p);
1472 case 'z':
1473 return parse_single_multiletter_ext (p, "z", "sub-extension",
1474 exact_single_p);
1475 case 's':
1476 return parse_single_multiletter_ext (p, "s", "supervisor extension",
1477 exact_single_p);
1478 default:
1479 return parse_single_std_ext (p, exact_single_p);
1480 }
1481 }
1482
1483 /* Parsing arch string to subset list, return NULL if parsing failed. */
1484
1485 riscv_subset_list *
1486 riscv_subset_list::parse (const char *arch, location_t loc)
1487 {
1488 if (riscv_subset_list::parse_failed)
1489 return NULL;
1490
1491 riscv_subset_list *subset_list = new riscv_subset_list (arch, loc);
1492 const char *p = arch;
1493 p = subset_list->parse_base_ext (p);
1494 if (p == NULL)
1495 goto fail;
1496
1497 while (p && *p)
1498 {
1499 switch (*p)
1500 {
1501 case '_':
1502 ++p;
1503 continue;
1504 case 'e':
1505 case 'i':
1506 case 'g':
1507 error_at (loc, "%<-march=%s%>: i, e or g must be the first extension",
1508 arch);
1509 goto fail;
1510 default:
1511 p = subset_list->parse_single_ext (p, /*exact_single_p=*/ false);
1512 }
1513 }
1514
1515 if (p == NULL)
1516 goto fail;
1517
1518 subset_list->finalize ();
1519
1520 return subset_list;
1521
1522 fail:
1523 delete subset_list;
1524 riscv_subset_list::parse_failed = true;
1525 return NULL;
1526 }
1527
1528 /* Clone whole subset list. */
1529
1530 riscv_subset_list *
1531 riscv_subset_list::clone () const
1532 {
1533 riscv_subset_list *new_list = new riscv_subset_list (m_arch, m_loc);
1534 for (riscv_subset_t *itr = m_head; itr != NULL; itr = itr->next)
1535 new_list->add (itr->name.c_str (), itr->major_version, itr->minor_version,
1536 itr->explicit_version_p, true);
1537
1538 new_list->m_xlen = m_xlen;
1539 return new_list;
1540 }
1541
1542 void
1543 riscv_subset_list::set_loc (location_t loc)
1544 {
1545 m_loc = loc;
1546 }
1547
1548 /* Make sure the implied or combined extension is included after add
1549 a new std extension to subset list or likewise. For exmaple as below,
1550
1551 void __attribute__((target("arch=+v"))) func () with -march=rv64gc.
1552
1553 The implied zvl128b and zve64d of the std v should be included. */
1554 void
1555 riscv_subset_list::finalize ()
1556 {
1557 riscv_subset_t *subset;
1558 unsigned pre_subset_num;
1559
1560 do
1561 {
1562 pre_subset_num = m_subset_num;
1563 for (subset = m_head; subset != NULL; subset = subset->next)
1564 handle_implied_ext (subset->name.c_str ());
1565 }
1566 while (pre_subset_num != m_subset_num);
1567
1568 gcc_assert (check_implied_ext ());
1569
1570 handle_combine_ext ();
1571 check_conflict_ext ();
1572 }
1573
1574 /* Return the current arch string. */
1575
1576 std::string
1577 riscv_arch_str (bool version_p)
1578 {
1579 if (cmdline_subset_list)
1580 return cmdline_subset_list->to_string (version_p);
1581 else
1582 return std::string();
1583 }
1584
1585 /* Type for pointer to member of gcc_options and cl_target_option. */
1586 typedef int (gcc_options::*opt_var_ref_t);
1587 typedef int (cl_target_option::*cl_opt_var_ref_t);
1588
1589 /* Types for recording extension to internal flag. */
1590 struct riscv_ext_flag_table_t {
1591 const char *ext;
1592 opt_var_ref_t var_ref;
1593 cl_opt_var_ref_t cl_var_ref;
1594 int mask;
1595 };
1596
1597 #define RISCV_EXT_FLAG_ENTRY(NAME, VAR, MASK) \
1598 {NAME, &gcc_options::VAR, &cl_target_option::VAR, MASK}
1599
1600 /* Mapping table between extension to internal flag. */
1601 static const riscv_ext_flag_table_t riscv_ext_flag_table[] =
1602 {
1603 RISCV_EXT_FLAG_ENTRY ("e", x_target_flags, MASK_RVE),
1604 RISCV_EXT_FLAG_ENTRY ("m", x_target_flags, MASK_MUL),
1605 RISCV_EXT_FLAG_ENTRY ("a", x_target_flags, MASK_ATOMIC),
1606 RISCV_EXT_FLAG_ENTRY ("f", x_target_flags, MASK_HARD_FLOAT),
1607 RISCV_EXT_FLAG_ENTRY ("d", x_target_flags, MASK_DOUBLE_FLOAT),
1608 RISCV_EXT_FLAG_ENTRY ("c", x_target_flags, MASK_RVC),
1609 RISCV_EXT_FLAG_ENTRY ("v", x_target_flags, MASK_FULL_V),
1610 RISCV_EXT_FLAG_ENTRY ("v", x_target_flags, MASK_VECTOR),
1611
1612 RISCV_EXT_FLAG_ENTRY ("zicsr", x_riscv_zi_subext, MASK_ZICSR),
1613 RISCV_EXT_FLAG_ENTRY ("zifencei", x_riscv_zi_subext, MASK_ZIFENCEI),
1614 RISCV_EXT_FLAG_ENTRY ("zicond", x_riscv_zi_subext, MASK_ZICOND),
1615
1616 RISCV_EXT_FLAG_ENTRY ("za64rs", x_riscv_za_subext, MASK_ZA64RS),
1617 RISCV_EXT_FLAG_ENTRY ("za128rs", x_riscv_za_subext, MASK_ZA128RS),
1618 RISCV_EXT_FLAG_ENTRY ("zawrs", x_riscv_za_subext, MASK_ZAWRS),
1619 RISCV_EXT_FLAG_ENTRY ("zaamo", x_riscv_za_subext, MASK_ZAAMO),
1620 RISCV_EXT_FLAG_ENTRY ("zalrsc", x_riscv_za_subext, MASK_ZALRSC),
1621 RISCV_EXT_FLAG_ENTRY ("zabha", x_riscv_za_subext, MASK_ZABHA),
1622 RISCV_EXT_FLAG_ENTRY ("zacas", x_riscv_za_subext, MASK_ZACAS),
1623
1624 RISCV_EXT_FLAG_ENTRY ("zba", x_riscv_zb_subext, MASK_ZBA),
1625 RISCV_EXT_FLAG_ENTRY ("zbb", x_riscv_zb_subext, MASK_ZBB),
1626 RISCV_EXT_FLAG_ENTRY ("zbc", x_riscv_zb_subext, MASK_ZBC),
1627 RISCV_EXT_FLAG_ENTRY ("zbs", x_riscv_zb_subext, MASK_ZBS),
1628
1629 RISCV_EXT_FLAG_ENTRY ("zfinx", x_riscv_zinx_subext, MASK_ZFINX),
1630 RISCV_EXT_FLAG_ENTRY ("zdinx", x_riscv_zinx_subext, MASK_ZDINX),
1631 RISCV_EXT_FLAG_ENTRY ("zhinx", x_riscv_zinx_subext, MASK_ZHINX),
1632 RISCV_EXT_FLAG_ENTRY ("zhinxmin", x_riscv_zinx_subext, MASK_ZHINXMIN),
1633
1634 RISCV_EXT_FLAG_ENTRY ("zbkb", x_riscv_zk_subext, MASK_ZBKB),
1635 RISCV_EXT_FLAG_ENTRY ("zbkc", x_riscv_zk_subext, MASK_ZBKC),
1636 RISCV_EXT_FLAG_ENTRY ("zbkx", x_riscv_zk_subext, MASK_ZBKX),
1637 RISCV_EXT_FLAG_ENTRY ("zknd", x_riscv_zk_subext, MASK_ZKND),
1638 RISCV_EXT_FLAG_ENTRY ("zkne", x_riscv_zk_subext, MASK_ZKNE),
1639 RISCV_EXT_FLAG_ENTRY ("zknh", x_riscv_zk_subext, MASK_ZKNH),
1640 RISCV_EXT_FLAG_ENTRY ("zkr", x_riscv_zk_subext, MASK_ZKR),
1641 RISCV_EXT_FLAG_ENTRY ("zksed", x_riscv_zk_subext, MASK_ZKSED),
1642 RISCV_EXT_FLAG_ENTRY ("zksh", x_riscv_zk_subext, MASK_ZKSH),
1643 RISCV_EXT_FLAG_ENTRY ("zkt", x_riscv_zk_subext, MASK_ZKT),
1644
1645 RISCV_EXT_FLAG_ENTRY ("zihintntl", x_riscv_zi_subext, MASK_ZIHINTNTL),
1646 RISCV_EXT_FLAG_ENTRY ("zihintpause", x_riscv_zi_subext, MASK_ZIHINTPAUSE),
1647 RISCV_EXT_FLAG_ENTRY ("ziccamoa", x_riscv_zi_subext, MASK_ZICCAMOA),
1648 RISCV_EXT_FLAG_ENTRY ("ziccif", x_riscv_zi_subext, MASK_ZICCIF),
1649 RISCV_EXT_FLAG_ENTRY ("zicclsm", x_riscv_zi_subext, MASK_ZICCLSM),
1650 RISCV_EXT_FLAG_ENTRY ("ziccrse", x_riscv_zi_subext, MASK_ZICCRSE),
1651
1652 RISCV_EXT_FLAG_ENTRY ("zicboz", x_riscv_zicmo_subext, MASK_ZICBOZ),
1653 RISCV_EXT_FLAG_ENTRY ("zicbom", x_riscv_zicmo_subext, MASK_ZICBOM),
1654 RISCV_EXT_FLAG_ENTRY ("zicbop", x_riscv_zicmo_subext, MASK_ZICBOP),
1655 RISCV_EXT_FLAG_ENTRY ("zic64b", x_riscv_zicmo_subext, MASK_ZIC64B),
1656
1657 RISCV_EXT_FLAG_ENTRY ("zicfiss", x_riscv_zi_subext, MASK_ZICFISS),
1658 RISCV_EXT_FLAG_ENTRY ("zicfilp", x_riscv_zi_subext, MASK_ZICFILP),
1659
1660 RISCV_EXT_FLAG_ENTRY ("zimop", x_riscv_mop_subext, MASK_ZIMOP),
1661 RISCV_EXT_FLAG_ENTRY ("zcmop", x_riscv_mop_subext, MASK_ZCMOP),
1662
1663 RISCV_EXT_FLAG_ENTRY ("zve32x", x_target_flags, MASK_VECTOR),
1664 RISCV_EXT_FLAG_ENTRY ("zve32f", x_target_flags, MASK_VECTOR),
1665 RISCV_EXT_FLAG_ENTRY ("zve64x", x_target_flags, MASK_VECTOR),
1666 RISCV_EXT_FLAG_ENTRY ("zve64f", x_target_flags, MASK_VECTOR),
1667 RISCV_EXT_FLAG_ENTRY ("zve64d", x_target_flags, MASK_VECTOR),
1668
1669 /* We don't need to put complete ELEN/ELEN_FP info here, due to the
1670 implication relation of vector extension.
1671 e.g. v -> zve64d ... zve32x, so v has set MASK_VECTOR_ELEN_FP_64,
1672 MASK_VECTOR_ELEN_FP_32, MASK_VECTOR_ELEN_64 and MASK_VECTOR_ELEN_32
1673 due to the extension implication. */
1674 RISCV_EXT_FLAG_ENTRY ("zve32x", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_32),
1675 RISCV_EXT_FLAG_ENTRY ("zve32f", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_32),
1676 RISCV_EXT_FLAG_ENTRY ("zve64x", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_64),
1677 RISCV_EXT_FLAG_ENTRY ("zve64f", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_32),
1678 RISCV_EXT_FLAG_ENTRY ("zve64d", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_64),
1679 RISCV_EXT_FLAG_ENTRY ("zvfbfmin", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_BF_16),
1680 RISCV_EXT_FLAG_ENTRY ("zvfbfwma", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_BF_16),
1681 RISCV_EXT_FLAG_ENTRY ("zvfhmin", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_16),
1682 RISCV_EXT_FLAG_ENTRY ("zvfh", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_16),
1683
1684 RISCV_EXT_FLAG_ENTRY ("zvbb", x_riscv_zvb_subext, MASK_ZVBB),
1685 RISCV_EXT_FLAG_ENTRY ("zvbc", x_riscv_zvb_subext, MASK_ZVBC),
1686 RISCV_EXT_FLAG_ENTRY ("zvkb", x_riscv_zvb_subext, MASK_ZVKB),
1687 RISCV_EXT_FLAG_ENTRY ("zvkg", x_riscv_zvk_subext, MASK_ZVKG),
1688 RISCV_EXT_FLAG_ENTRY ("zvkned", x_riscv_zvk_subext, MASK_ZVKNED),
1689 RISCV_EXT_FLAG_ENTRY ("zvknha", x_riscv_zvk_subext, MASK_ZVKNHA),
1690 RISCV_EXT_FLAG_ENTRY ("zvknhb", x_riscv_zvk_subext, MASK_ZVKNHB),
1691 RISCV_EXT_FLAG_ENTRY ("zvksed", x_riscv_zvk_subext, MASK_ZVKSED),
1692 RISCV_EXT_FLAG_ENTRY ("zvksh", x_riscv_zvk_subext, MASK_ZVKSH),
1693 RISCV_EXT_FLAG_ENTRY ("zvkn", x_riscv_zvk_subext, MASK_ZVKN),
1694 RISCV_EXT_FLAG_ENTRY ("zvknc", x_riscv_zvk_subext, MASK_ZVKNC),
1695 RISCV_EXT_FLAG_ENTRY ("zvkng", x_riscv_zvk_subext, MASK_ZVKNG),
1696 RISCV_EXT_FLAG_ENTRY ("zvks", x_riscv_zvk_subext, MASK_ZVKS),
1697 RISCV_EXT_FLAG_ENTRY ("zvksc", x_riscv_zvk_subext, MASK_ZVKSC),
1698 RISCV_EXT_FLAG_ENTRY ("zvksg", x_riscv_zvk_subext, MASK_ZVKSG),
1699 RISCV_EXT_FLAG_ENTRY ("zvkt", x_riscv_zvk_subext, MASK_ZVKT),
1700
1701 RISCV_EXT_FLAG_ENTRY ("zvl32b", x_riscv_zvl_flags, MASK_ZVL32B),
1702 RISCV_EXT_FLAG_ENTRY ("zvl64b", x_riscv_zvl_flags, MASK_ZVL64B),
1703 RISCV_EXT_FLAG_ENTRY ("zvl128b", x_riscv_zvl_flags, MASK_ZVL128B),
1704 RISCV_EXT_FLAG_ENTRY ("zvl256b", x_riscv_zvl_flags, MASK_ZVL256B),
1705 RISCV_EXT_FLAG_ENTRY ("zvl512b", x_riscv_zvl_flags, MASK_ZVL512B),
1706 RISCV_EXT_FLAG_ENTRY ("zvl1024b", x_riscv_zvl_flags, MASK_ZVL1024B),
1707 RISCV_EXT_FLAG_ENTRY ("zvl2048b", x_riscv_zvl_flags, MASK_ZVL2048B),
1708 RISCV_EXT_FLAG_ENTRY ("zvl4096b", x_riscv_zvl_flags, MASK_ZVL4096B),
1709 RISCV_EXT_FLAG_ENTRY ("zvl8192b", x_riscv_zvl_flags, MASK_ZVL8192B),
1710 RISCV_EXT_FLAG_ENTRY ("zvl16384b", x_riscv_zvl_flags, MASK_ZVL16384B),
1711 RISCV_EXT_FLAG_ENTRY ("zvl32768b", x_riscv_zvl_flags, MASK_ZVL32768B),
1712 RISCV_EXT_FLAG_ENTRY ("zvl65536b", x_riscv_zvl_flags, MASK_ZVL65536B),
1713
1714 RISCV_EXT_FLAG_ENTRY ("zfbfmin", x_riscv_zf_subext, MASK_ZFBFMIN),
1715 RISCV_EXT_FLAG_ENTRY ("zfhmin", x_riscv_zf_subext, MASK_ZFHMIN),
1716 RISCV_EXT_FLAG_ENTRY ("zfh", x_riscv_zf_subext, MASK_ZFH),
1717 RISCV_EXT_FLAG_ENTRY ("zvfbfmin", x_riscv_zf_subext, MASK_ZVFBFMIN),
1718 RISCV_EXT_FLAG_ENTRY ("zvfbfwma", x_riscv_zf_subext, MASK_ZVFBFWMA),
1719 RISCV_EXT_FLAG_ENTRY ("zvfhmin", x_riscv_zf_subext, MASK_ZVFHMIN),
1720 RISCV_EXT_FLAG_ENTRY ("zvfh", x_riscv_zf_subext, MASK_ZVFH),
1721
1722 RISCV_EXT_FLAG_ENTRY ("zfa", x_riscv_zfa_subext, MASK_ZFA),
1723
1724 RISCV_EXT_FLAG_ENTRY ("zmmul", x_riscv_zm_subext, MASK_ZMMUL),
1725
1726 /* Code-size reduction extensions. */
1727 RISCV_EXT_FLAG_ENTRY ("zca", x_riscv_zc_subext, MASK_ZCA),
1728 RISCV_EXT_FLAG_ENTRY ("zcb", x_riscv_zc_subext, MASK_ZCB),
1729 RISCV_EXT_FLAG_ENTRY ("zce", x_riscv_zc_subext, MASK_ZCE),
1730 RISCV_EXT_FLAG_ENTRY ("zcf", x_riscv_zc_subext, MASK_ZCF),
1731 RISCV_EXT_FLAG_ENTRY ("zcd", x_riscv_zc_subext, MASK_ZCD),
1732 RISCV_EXT_FLAG_ENTRY ("zcmp", x_riscv_zc_subext, MASK_ZCMP),
1733 RISCV_EXT_FLAG_ENTRY ("zcmt", x_riscv_zc_subext, MASK_ZCMT),
1734
1735 RISCV_EXT_FLAG_ENTRY ("svinval", x_riscv_sv_subext, MASK_SVINVAL),
1736 RISCV_EXT_FLAG_ENTRY ("svnapot", x_riscv_sv_subext, MASK_SVNAPOT),
1737 RISCV_EXT_FLAG_ENTRY ("svvptc", x_riscv_sv_subext, MASK_SVVPTC),
1738
1739 RISCV_EXT_FLAG_ENTRY ("ztso", x_riscv_ztso_subext, MASK_ZTSO),
1740
1741 RISCV_EXT_FLAG_ENTRY ("xcvmac", x_riscv_xcv_subext, MASK_XCVMAC),
1742 RISCV_EXT_FLAG_ENTRY ("xcvalu", x_riscv_xcv_subext, MASK_XCVALU),
1743 RISCV_EXT_FLAG_ENTRY ("xcvelw", x_riscv_xcv_subext, MASK_XCVELW),
1744 RISCV_EXT_FLAG_ENTRY ("xcvsimd", x_riscv_xcv_subext, MASK_XCVSIMD),
1745 RISCV_EXT_FLAG_ENTRY ("xcvbi", x_riscv_xcv_subext, MASK_XCVBI),
1746
1747 RISCV_EXT_FLAG_ENTRY ("xtheadba", x_riscv_xthead_subext, MASK_XTHEADBA),
1748 RISCV_EXT_FLAG_ENTRY ("xtheadbb", x_riscv_xthead_subext, MASK_XTHEADBB),
1749 RISCV_EXT_FLAG_ENTRY ("xtheadbs", x_riscv_xthead_subext, MASK_XTHEADBS),
1750 RISCV_EXT_FLAG_ENTRY ("xtheadcmo", x_riscv_xthead_subext, MASK_XTHEADCMO),
1751 RISCV_EXT_FLAG_ENTRY ("xtheadcondmov", x_riscv_xthead_subext, MASK_XTHEADCONDMOV),
1752 RISCV_EXT_FLAG_ENTRY ("xtheadfmemidx", x_riscv_xthead_subext, MASK_XTHEADFMEMIDX),
1753 RISCV_EXT_FLAG_ENTRY ("xtheadfmv", x_riscv_xthead_subext, MASK_XTHEADFMV),
1754 RISCV_EXT_FLAG_ENTRY ("xtheadint", x_riscv_xthead_subext, MASK_XTHEADINT),
1755 RISCV_EXT_FLAG_ENTRY ("xtheadmac", x_riscv_xthead_subext, MASK_XTHEADMAC),
1756 RISCV_EXT_FLAG_ENTRY ("xtheadmemidx", x_riscv_xthead_subext, MASK_XTHEADMEMIDX),
1757 RISCV_EXT_FLAG_ENTRY ("xtheadmempair", x_riscv_xthead_subext, MASK_XTHEADMEMPAIR),
1758 RISCV_EXT_FLAG_ENTRY ("xtheadsync", x_riscv_xthead_subext, MASK_XTHEADSYNC),
1759 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_xthead_subext, MASK_XTHEADVECTOR),
1760 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_32),
1761 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_64),
1762 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_32),
1763 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_64),
1764 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_vector_elen_flags, MASK_VECTOR_ELEN_FP_16),
1765 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_zvl_flags, MASK_ZVL32B),
1766 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_zvl_flags, MASK_ZVL64B),
1767 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_zvl_flags, MASK_ZVL128B),
1768 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_zf_subext, MASK_ZVFHMIN),
1769 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_riscv_zf_subext, MASK_ZVFH),
1770 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_target_flags, MASK_FULL_V),
1771 RISCV_EXT_FLAG_ENTRY ("xtheadvector", x_target_flags, MASK_VECTOR),
1772
1773 RISCV_EXT_FLAG_ENTRY ("xventanacondops", x_riscv_xventana_subext, MASK_XVENTANACONDOPS),
1774
1775 RISCV_EXT_FLAG_ENTRY ("xsfvcp", x_riscv_sifive_subext, MASK_XSFVCP),
1776 RISCV_EXT_FLAG_ENTRY ("xsfcease", x_riscv_sifive_subext, MASK_XSFCEASE),
1777 RISCV_EXT_FLAG_ENTRY ("xsfvqmaccqoq", x_riscv_sifive_subext, MASK_XSFVQMACCQOQ),
1778 RISCV_EXT_FLAG_ENTRY ("xsfvqmaccdod", x_riscv_sifive_subext, MASK_XSFVQMACCDOD),
1779 RISCV_EXT_FLAG_ENTRY ("xsfvfnrclipxfqf", x_riscv_sifive_subext, MASK_XSFVFNRCLIPXFQF),
1780
1781 {NULL, NULL, NULL, 0}
1782 };
1783
1784 /* Types for recording extension to RISC-V C-API bitmask. */
1785 struct riscv_ext_bitmask_table_t {
1786 const char *ext;
1787 int groupid;
1788 int bit_position;
1789 };
1790
1791 /* Mapping table between extension to RISC-V C-API extension bitmask.
1792 This table should sort the extension by Linux hwprobe order to get the
1793 minimal feature bits. */
1794 static const riscv_ext_bitmask_table_t riscv_ext_bitmask_table[] =
1795 {
1796 #define RISCV_EXT_BITMASK(NAME, GROUPID, BITPOS) \
1797 {NAME, GROUPID, BITPOS},
1798 #include "riscv-ext-bitmask.def"
1799 {NULL, -1, -1}
1800 };
1801
1802 /* Apply SUBSET_LIST to OPTS if OPTS is not null. */
1803
1804 void
1805 riscv_set_arch_by_subset_list (riscv_subset_list *subset_list,
1806 struct gcc_options *opts)
1807 {
1808 if (opts)
1809 {
1810 const riscv_ext_flag_table_t *arch_ext_flag_tab;
1811 /* Clean up target flags before we set. */
1812 for (arch_ext_flag_tab = &riscv_ext_flag_table[0]; arch_ext_flag_tab->ext;
1813 ++arch_ext_flag_tab)
1814 opts->*arch_ext_flag_tab->var_ref &= ~arch_ext_flag_tab->mask;
1815
1816 if (subset_list->xlen () == 32)
1817 opts->x_target_flags &= ~MASK_64BIT;
1818 else if (subset_list->xlen () == 64)
1819 opts->x_target_flags |= MASK_64BIT;
1820
1821 for (arch_ext_flag_tab = &riscv_ext_flag_table[0];
1822 arch_ext_flag_tab->ext;
1823 ++arch_ext_flag_tab)
1824 {
1825 if (subset_list->lookup (arch_ext_flag_tab->ext))
1826 opts->*arch_ext_flag_tab->var_ref |= arch_ext_flag_tab->mask;
1827 }
1828 }
1829 }
1830
1831 /* Check if the ISA extension of the "subset" is a subset of the "opts". */
1832
1833 bool
1834 riscv_ext_is_subset (struct cl_target_option *opts,
1835 struct cl_target_option *subset)
1836 {
1837 const riscv_ext_flag_table_t *arch_ext_flag_tab;
1838 for (arch_ext_flag_tab = &riscv_ext_flag_table[0];
1839 arch_ext_flag_tab->ext;
1840 ++arch_ext_flag_tab)
1841 {
1842 if (subset->*arch_ext_flag_tab->cl_var_ref & arch_ext_flag_tab->mask)
1843 {
1844 if (!(opts->*arch_ext_flag_tab->cl_var_ref & arch_ext_flag_tab->mask))
1845 return false;
1846 }
1847 }
1848 return true;
1849 }
1850
1851 /* Return the mask of ISA extension in x_target_flags of gcc_options. */
1852
1853 int
1854 riscv_x_target_flags_isa_mask (void)
1855 {
1856 int mask = 0;
1857 const riscv_ext_flag_table_t *arch_ext_flag_tab;
1858 for (arch_ext_flag_tab = &riscv_ext_flag_table[0];
1859 arch_ext_flag_tab->ext;
1860 ++arch_ext_flag_tab)
1861 {
1862 if (arch_ext_flag_tab->var_ref == &gcc_options::x_target_flags)
1863 mask |= arch_ext_flag_tab->mask;
1864 }
1865 return mask;
1866 }
1867
1868 /* Get the minimal feature bits in Linux hwprobe of the given ISA string.
1869
1870 Used for generating Function Multi-Versioning (FMV) dispatcher for RISC-V.
1871
1872 The minimal feature bits refer to using the earliest extension that appeared
1873 in the Linux hwprobe to support the specified ISA string. This ensures that
1874 older kernels, which may lack certain implied extensions, can still run the
1875 FMV dispatcher correctly. */
1876
1877 bool
1878 riscv_minimal_hwprobe_feature_bits (const char *isa,
1879 struct riscv_feature_bits *res,
1880 location_t loc)
1881 {
1882 riscv_subset_list *subset_list;
1883 subset_list = riscv_subset_list::parse (isa, loc);
1884 if (!subset_list)
1885 return false;
1886
1887 /* Initialize the result feature bits to zero. */
1888 res->length = RISCV_FEATURE_BITS_LENGTH;
1889 for (int i = 0; i < RISCV_FEATURE_BITS_LENGTH; ++i)
1890 res->features[i] = 0;
1891
1892 /* Use a std::set to record all visited implied extensions. */
1893 std::set <std::string> implied_exts;
1894
1895 /* Iterate through the extension bitmask table in Linux hwprobe order to get
1896 the minimal covered feature bits. Avoiding some sub-extensions which will
1897 be implied by the super-extensions like V implied Zve32x. */
1898 const riscv_ext_bitmask_table_t *ext_bitmask_tab;
1899 for (ext_bitmask_tab = &riscv_ext_bitmask_table[0];
1900 ext_bitmask_tab->ext;
1901 ++ext_bitmask_tab)
1902 {
1903 /* Skip the extension if it is not in the subset list or already implied
1904 by previous extension. */
1905 if (subset_list->lookup (ext_bitmask_tab->ext) == NULL
1906 || implied_exts.count (ext_bitmask_tab->ext))
1907 continue;
1908
1909 res->features[ext_bitmask_tab->groupid]
1910 |= 1ULL << ext_bitmask_tab->bit_position;
1911
1912 /* Find the sub-extension using BFS and set the corresponding bit. */
1913 std::queue <const char *> search_q;
1914 search_q.push (ext_bitmask_tab->ext);
1915
1916 while (!search_q.empty ())
1917 {
1918 const char * search_ext = search_q.front ();
1919 search_q.pop ();
1920
1921 /* Iterate through the implied extension table. */
1922 const riscv_implied_info_t *implied_info;
1923 for (implied_info = &riscv_implied_info[0];
1924 implied_info->ext;
1925 ++implied_info)
1926 {
1927 /* When the search extension matches the implied extension and
1928 the implied extension has not been visited, mark the implied
1929 extension in the implied_exts set and push it into the
1930 queue. */
1931 if (implied_info->match (subset_list, search_ext)
1932 && implied_exts.count (implied_info->implied_ext) == 0)
1933 {
1934 implied_exts.insert (implied_info->implied_ext);
1935 search_q.push (implied_info->implied_ext);
1936 }
1937 }
1938 }
1939 }
1940 return true;
1941 }
1942
1943 /* Parse a RISC-V ISA string into an option mask. Must clear or set all arch
1944 dependent mask bits, in case more than one -march string is passed. */
1945
1946 void
1947 riscv_parse_arch_string (const char *isa,
1948 struct gcc_options *opts,
1949 location_t loc)
1950 {
1951 riscv_subset_list *subset_list;
1952 subset_list = riscv_subset_list::parse (isa, loc);
1953 if (!subset_list)
1954 return;
1955
1956 if (cmdline_subset_list)
1957 delete cmdline_subset_list;
1958
1959 cmdline_subset_list = subset_list;
1960
1961 riscv_set_arch_by_subset_list (subset_list, opts);
1962 }
1963
1964 /* Return the riscv_cpu_info entry for CPU, NULL if not found. */
1965
1966 const riscv_cpu_info *
1967 riscv_find_cpu (const char *cpu)
1968 {
1969 const riscv_cpu_info *cpu_info = &riscv_cpu_tables[0];
1970 for (;cpu_info->name != NULL; ++cpu_info)
1971 {
1972 const char *name = cpu_info->name;
1973 if (strcmp (cpu, name) == 0)
1974 return cpu_info;
1975 }
1976 return NULL;
1977 }
1978
1979 /* Implement TARGET_HANDLE_OPTION. */
1980
1981 static bool
1982 riscv_handle_option (struct gcc_options *opts,
1983 struct gcc_options *opts_set ATTRIBUTE_UNUSED,
1984 const struct cl_decoded_option *decoded,
1985 location_t loc)
1986 {
1987 switch (decoded->opt_index)
1988 {
1989 case OPT_march_:
1990 riscv_parse_arch_string (decoded->arg, opts, loc);
1991 return true;
1992
1993 case OPT_mcpu_:
1994 if (riscv_find_cpu (decoded->arg) == NULL)
1995 error_at (loc, "%<-mcpu=%s%>: unknown CPU",
1996 decoded->arg);
1997 return true;
1998
1999 default:
2000 return true;
2001 }
2002 }
2003
2004 /* Expand arch string with implied extensions. */
2005
2006 const char *
2007 riscv_expand_arch (int argc ATTRIBUTE_UNUSED,
2008 const char **argv)
2009 {
2010 gcc_assert (argc == 1);
2011 location_t loc = UNKNOWN_LOCATION;
2012 riscv_parse_arch_string (argv[0], NULL, loc);
2013 const std::string arch = riscv_arch_str (false);
2014 if (arch.length())
2015 return xasprintf ("-march=%s", arch.c_str());
2016 else
2017 return "";
2018 }
2019
2020 /* Expand default -mtune option from -mcpu option, use default --with-tune value
2021 if -mcpu don't have valid value. */
2022
2023 const char *
2024 riscv_default_mtune (int argc, const char **argv)
2025 {
2026 gcc_assert (argc == 2);
2027 const riscv_cpu_info *cpu = riscv_find_cpu (argv[0]);
2028 const char *default_mtune = argv[1];
2029 if (cpu)
2030 return cpu->tune;
2031 else
2032 return default_mtune;
2033 }
2034
2035 /* Expand arch string with implied extensions from -mcpu option. */
2036
2037 const char *
2038 riscv_expand_arch_from_cpu (int argc ATTRIBUTE_UNUSED,
2039 const char **argv)
2040 {
2041 gcc_assert (argc > 0 && argc <= 2);
2042 const char *default_arch_str = NULL;
2043 const char *arch_str = NULL;
2044 if (argc >= 2)
2045 default_arch_str = argv[1];
2046
2047 const riscv_cpu_info *cpu = riscv_find_cpu (argv[0]);
2048
2049 if (cpu == NULL)
2050 {
2051 if (default_arch_str == NULL)
2052 return "";
2053 else
2054 arch_str = default_arch_str;
2055 }
2056 else
2057 arch_str = cpu->arch;
2058
2059 location_t loc = UNKNOWN_LOCATION;
2060
2061 riscv_parse_arch_string (arch_str, NULL, loc);
2062 const std::string arch = riscv_arch_str (false);
2063 return xasprintf ("-march=%s", arch.c_str());
2064 }
2065
2066 /* Report error if not found suitable multilib. */
2067 const char *
2068 riscv_multi_lib_check (int argc ATTRIBUTE_UNUSED,
2069 const char **argv ATTRIBUTE_UNUSED)
2070 {
2071 if (riscv_no_matched_multi_lib)
2072 fatal_error (
2073 input_location,
2074 "Cannot find suitable multilib set for %<-march=%s%>/%<-mabi=%s%>",
2075 riscv_current_arch_str.c_str (),
2076 riscv_current_abi_str.c_str ());
2077
2078 return "";
2079 }
2080
2081 /* Find last switch with the prefix, options are take last one in general,
2082 return NULL if not found, and return the option value if found, it could
2083 return empty string if the option has no value. */
2084
2085 static const char *
2086 find_last_appear_switch (
2087 const struct switchstr *switches,
2088 int n_switches,
2089 const char *prefix)
2090 {
2091 int i;
2092 size_t len = strlen (prefix);
2093
2094 for (i = 0; i < n_switches; ++i)
2095 {
2096 const struct switchstr *this_switch = &switches[n_switches - i - 1];
2097
2098 if (this_switch->live_cond & SWITCH_FALSE)
2099 continue;
2100
2101 if (strncmp (this_switch->part1, prefix, len) == 0)
2102 return this_switch->part1 + len;
2103 }
2104
2105 return NULL;
2106 }
2107
2108 /* Utils functions to check STR is start with PREFIX or not. */
2109
2110 static bool
2111 prefixed_with (const std::string &str, const char *prefix)
2112 {
2113 return strncmp (prefix, str.c_str (), strlen (prefix)) == 0;
2114 }
2115
2116 /* Parse the path and cond string into riscv_multi_lib_info_t, return false
2117 if parsing failed. */
2118
2119 bool
2120 riscv_multi_lib_info_t::parse (
2121 struct riscv_multi_lib_info_t *multi_lib_info,
2122 const std::string &path,
2123 const std::vector<std::string> &conds)
2124 {
2125 const char *default_arch_str = STRINGIZING (TARGET_RISCV_DEFAULT_ARCH);
2126 const char *default_abi_str = STRINGIZING (TARGET_RISCV_DEFAULT_ABI);
2127 multi_lib_info->conds = conds;
2128 if (path == ".")
2129 {
2130 multi_lib_info->arch_str = default_arch_str;
2131 multi_lib_info->abi_str = default_abi_str;
2132 }
2133 else
2134 {
2135 std::vector<std::string>::const_iterator itr;
2136 for (itr = conds.begin (); itr != conds.end (); ++itr)
2137 if (prefixed_with (*itr, "march="))
2138 multi_lib_info->arch_str = itr->c_str () + strlen ("march=");
2139 else if (prefixed_with (*itr, "mabi="))
2140 multi_lib_info->abi_str = itr->c_str () + strlen ("mabi=");
2141
2142 /* Skip this multi-lib if this configuration is exactly same as
2143 default multi-lib settings. */
2144 if (multi_lib_info->arch_str == default_arch_str
2145 && multi_lib_info->abi_str == default_abi_str)
2146 return false;
2147 }
2148
2149 multi_lib_info->subset_list =
2150 riscv_subset_list::parse (multi_lib_info->arch_str.c_str (), input_location);
2151
2152 return true;
2153 }
2154
2155 /* Checking ARG is not appeared in SWITCHES if NOT_ARG is set or
2156 ARG is appeared if NOT_ARG is not set. */
2157
2158 static bool
2159 riscv_check_cond (
2160 const struct switchstr *switches,
2161 int n_switches,
2162 const std::string &arg,
2163 bool not_arg)
2164 {
2165 int i;
2166 for (i = 0; i < n_switches; ++i)
2167 {
2168 const struct switchstr *this_switch = &switches[n_switches - i - 1];
2169
2170 if ((this_switch->live_cond & SWITCH_IGNORE) != 0)
2171 continue;
2172
2173 if (this_switch->live_cond & SWITCH_FALSE)
2174 continue;
2175
2176 /* ARG should not appear if NOT_ARG is set. */
2177 if (arg == this_switch->part1)
2178 return not_arg ? false : true;
2179 }
2180
2181 /* Not found ARG? that's ok if NOT_ARG is not set. */
2182 return not_arg ? true : false;
2183 }
2184
2185 /* Check the other cond is found or not, return -1 if we should reject this
2186 multi-lib option set, otherwise return updated MATCH_SCORE. */
2187
2188 static int
2189 riscv_check_conds (
2190 const struct switchstr *switches,
2191 int n_switches,
2192 int match_score,
2193 const std::vector<std::string> &conds)
2194 {
2195 bool not_arg;
2196 bool ok;
2197 int ok_count = 0;
2198 std::vector<std::string>::const_iterator itr;
2199 const char *checking_arg;
2200
2201 if (match_score == 0)
2202 return 0;
2203
2204 for (itr = conds.begin (); itr != conds.end (); ++itr)
2205 {
2206 /* We'll check march= and mabi= in other place. */
2207 if (prefixed_with (*itr, "march=") || prefixed_with (*itr, "mabi="))
2208 continue;
2209
2210 checking_arg = itr->c_str ();
2211 if (checking_arg[0] == '!')
2212 {
2213 not_arg = true;
2214 /* Skip '!'. */
2215 checking_arg = checking_arg + 1;
2216 }
2217 else
2218 not_arg = false;
2219
2220 ok = riscv_check_cond (switches, n_switches, checking_arg, not_arg);
2221
2222 if (!ok)
2223 return -1;
2224
2225 ok_count++;
2226 }
2227
2228 /* 100 is magic number, it's just used for make sure this multi-lib has
2229 higher priority if we found any some option is listed in the option check
2230 list. */
2231 return match_score + ok_count * 100;
2232 }
2233
2234 static const char *
2235 riscv_select_multilib_by_abi (
2236 const std::string &riscv_current_abi_str,
2237 const std::vector<riscv_multi_lib_info_t> &multilib_infos)
2238 {
2239 for (ssize_t i = multilib_infos.size () - 1; i >= 0; --i)
2240 if (riscv_current_abi_str == multilib_infos[i].abi_str)
2241 return xstrdup (multilib_infos[i].path.c_str ());
2242
2243 return NULL;
2244 }
2245
2246 static const char *
2247 riscv_select_multilib (
2248 const std::string &riscv_current_abi_str,
2249 const riscv_subset_list *subset_list, const struct switchstr *switches,
2250 int n_switches, const std::vector<riscv_multi_lib_info_t> &multilib_infos)
2251 {
2252 int match_score = 0;
2253 int max_match_score = 0;
2254 int best_match_multi_lib = -1;
2255 /* Try to decision which set we should used. */
2256 /* We have 3 level decision tree here, ABI, check input arch/ABI must
2257 be superset of multi-lib arch, and other rest option checking. */
2258 for (size_t i = 0; i < multilib_infos.size (); ++i)
2259 {
2260 /* Check ABI is same first. */
2261 if (riscv_current_abi_str != multilib_infos[i].abi_str)
2262 continue;
2263
2264 /* Found a potential compatible multi-lib setting!
2265 Calculate the match score. */
2266 match_score = subset_list->match_score (multilib_infos[i].subset_list);
2267
2268 /* Checking other cond in the multi-lib setting. */
2269 match_score = riscv_check_conds (switches, n_switches, match_score,
2270 multilib_infos[i].conds);
2271
2272 /* Record highest match score multi-lib setting. */
2273 if (match_score > max_match_score)
2274 {
2275 best_match_multi_lib = i;
2276 max_match_score = match_score;
2277 }
2278 }
2279
2280 if (best_match_multi_lib == -1)
2281 {
2282 riscv_no_matched_multi_lib = true;
2283 return NULL;
2284 }
2285 else
2286 return xstrdup (multilib_infos[best_match_multi_lib].path.c_str ());
2287 }
2288
2289 #ifndef RISCV_USE_CUSTOMISED_MULTI_LIB
2290 #define RISCV_USE_CUSTOMISED_MULTI_LIB select_by_builtin
2291 #endif
2292
2293 /* Implement TARGET_COMPUTE_MULTILIB. */
2294 static const char *
2295 riscv_compute_multilib (
2296 const struct switchstr *switches,
2297 int n_switches,
2298 const char *multilib_dir,
2299 const char *multilib_defaults ATTRIBUTE_UNUSED,
2300 const char *multilib_select,
2301 const char *multilib_matches ATTRIBUTE_UNUSED,
2302 const char *multilib_exclusions ATTRIBUTE_UNUSED,
2303 const char *multilib_reuse ATTRIBUTE_UNUSED)
2304 {
2305 enum riscv_multilib_select_kind select_kind = RISCV_USE_CUSTOMISED_MULTI_LIB;
2306
2307 if (select_kind == select_by_builtin)
2308 return multilib_dir;
2309
2310 const char *p;
2311 const char *this_path;
2312 size_t this_path_len;
2313 bool result;
2314 riscv_no_matched_multi_lib = false;
2315 riscv_subset_list *subset_list = NULL;
2316
2317 std::vector<riscv_multi_lib_info_t> multilib_infos;
2318 std::vector<std::string> option_conds;
2319 std::string option_cond;
2320 riscv_multi_lib_info_t multilib_info;
2321
2322 /* Already found suitable, multi-lib, just use that. */
2323 if (multilib_dir != NULL)
2324 return multilib_dir;
2325
2326 /* Find march. */
2327 riscv_current_arch_str =
2328 find_last_appear_switch (switches, n_switches, "march=");
2329 /* Find mabi. */
2330 riscv_current_abi_str =
2331 find_last_appear_switch (switches, n_switches, "mabi=");
2332
2333 /* Failed to find -march or -mabi, but it should not happened since we have
2334 set both in OPTION_DEFAULT_SPECS. */
2335 if (riscv_current_arch_str.empty () || riscv_current_abi_str.empty ())
2336 return multilib_dir;
2337
2338 subset_list = riscv_subset_list::parse (riscv_current_arch_str.c_str (),
2339 input_location);
2340
2341 /* Failed to parse -march, fallback to using what gcc use. */
2342 if (subset_list == NULL)
2343 return multilib_dir;
2344
2345 /* Parsing MULTILIB_SELECT, ignore MULTILIB_REUSE here, we have our own rules.
2346 TODO: most codes are grab from gcc.c, maybe we should refine that? */
2347 p = multilib_select;
2348
2349 while (*p != '\0')
2350 {
2351 /* Ignore newlines. */
2352 if (*p == '\n')
2353 {
2354 ++p;
2355 continue;
2356 }
2357
2358 /* Format of each multilib:
2359 <path> <opt1> <opt2> ... <optN>; */
2360 /* Get the path. */
2361 this_path = p;
2362 while (*p != ' ')
2363 {
2364 if (*p == '\0')
2365 {
2366 fatal_error (input_location, "multilib select %qs %qs is invalid",
2367 multilib_select, multilib_reuse);
2368 }
2369 ++p;
2370 }
2371
2372 this_path_len = p - this_path;
2373 const char *multi_os_dir_pos
2374 = (const char *) memchr (this_path, ':', this_path_len);
2375 if (multi_os_dir_pos)
2376 multilib_info.path
2377 = std::string (this_path, multi_os_dir_pos - this_path);
2378 else
2379 multilib_info.path = std::string (this_path, this_path_len);
2380
2381 option_conds.clear ();
2382 /* Pasrse option check list into vector<string>.
2383 e.g. "march=rv64imafd mabi=lp64 !mcmodel=medany" to
2384 ["march=rv64imafd", "mabi=lp64", "!mcmodel=medany"]. */
2385 while (*p != ';')
2386 {
2387 option_cond = "";
2388 /* Skip space. */
2389 while (*p == ' ') p++;
2390
2391 while (*p && *p != ' ' && *p != ';')
2392 option_cond.push_back (*p++);
2393
2394 /* Ignore `!march=` and `!mabi=`, we will handle march and mabi
2395 later. */
2396 if (option_cond.size ()
2397 && !prefixed_with (option_cond, "!march=")
2398 && !prefixed_with (option_cond, "!mabi="))
2399 option_conds.push_back (option_cond);
2400 }
2401
2402 result =
2403 riscv_multi_lib_info_t::parse (
2404 &multilib_info,
2405 std::string (this_path, this_path_len),
2406 option_conds);
2407
2408 if (result)
2409 multilib_infos.push_back (multilib_info);
2410
2411 p++;
2412 }
2413
2414 switch (select_kind)
2415 {
2416 case select_by_abi:
2417 return riscv_select_multilib_by_abi (riscv_current_abi_str,
2418 multilib_infos);
2419 case select_by_abi_arch_cmodel:
2420 return riscv_select_multilib (riscv_current_abi_str, subset_list,
2421 switches, n_switches, multilib_infos);
2422 case select_by_builtin:
2423 gcc_unreachable ();
2424 default:
2425 gcc_unreachable ();
2426 }
2427 }
2428
2429 #undef TARGET_COMPUTE_MULTILIB
2430 #define TARGET_COMPUTE_MULTILIB riscv_compute_multilib
2431
2432 vec<const char *>
2433 riscv_get_valid_option_values (int option_code,
2434 const char *prefix ATTRIBUTE_UNUSED)
2435 {
2436 vec<const char *> v;
2437 v.create (0);
2438 opt_code opt = (opt_code) option_code;
2439
2440 switch (opt)
2441 {
2442 case OPT_mtune_:
2443 {
2444 const char **tune = &riscv_tunes[0];
2445 for (;*tune; ++tune)
2446 v.safe_push (*tune);
2447
2448 const riscv_cpu_info *cpu_info = &riscv_cpu_tables[0];
2449 for (;cpu_info->name; ++cpu_info)
2450 {
2451 /* Skip duplicates. */
2452 bool skip = false;
2453 int i;
2454 const char *str;
2455 FOR_EACH_VEC_ELT (v, i, str)
2456 {
2457 if (!strcmp (str, cpu_info->name))
2458 skip = true;
2459 }
2460 if (!skip)
2461 v.safe_push (cpu_info->name);
2462 }
2463 }
2464 break;
2465 case OPT_mcpu_:
2466 {
2467 const riscv_cpu_info *cpu_info = &riscv_cpu_tables[0];
2468 for (;cpu_info->name; ++cpu_info)
2469 v.safe_push (cpu_info->name);
2470 }
2471 break;
2472 default:
2473 break;
2474 }
2475
2476 return v;
2477 }
2478
2479 const char *
2480 riscv_arch_help (int, const char **)
2481 {
2482 /* Collect all exts, and sort it in canonical order. */
2483 struct extension_comparator {
2484 bool operator()(const std::string& a, const std::string& b) const {
2485 return subset_cmp(a, b) >= 1;
2486 }
2487 };
2488 std::map<std::string, std::set<unsigned>, extension_comparator> all_exts;
2489 for (const riscv_ext_version &ext : riscv_ext_version_table)
2490 {
2491 if (!ext.name)
2492 break;
2493 if (ext.name[0] == 'g')
2494 continue;
2495 unsigned version_value = (ext.major_version * RISCV_MAJOR_VERSION_BASE)
2496 + (ext.minor_version
2497 * RISCV_MINOR_VERSION_BASE);
2498 all_exts[ext.name].insert(version_value);
2499 }
2500
2501 printf("All available -march extensions for RISC-V:\n");
2502 printf("\t%-20sVersion\n", "Name");
2503 for (auto const &ext_info : all_exts)
2504 {
2505 printf("\t%-20s\t", ext_info.first.c_str());
2506 bool first = true;
2507 for (auto version : ext_info.second)
2508 {
2509 if (first)
2510 first = false;
2511 else
2512 printf(", ");
2513 unsigned major = version / RISCV_MAJOR_VERSION_BASE;
2514 unsigned minor = (version % RISCV_MAJOR_VERSION_BASE)
2515 / RISCV_MINOR_VERSION_BASE;
2516 printf("%u.%u", major, minor);
2517 }
2518 printf("\n");
2519 }
2520 exit (0);
2521 }
2522
2523 /* Implement TARGET_OPTION_OPTIMIZATION_TABLE. */
2524 static const struct default_options riscv_option_optimization_table[] =
2525 {
2526 { OPT_LEVELS_1_PLUS, OPT_fsection_anchors, NULL, 1 },
2527 /* Enable -fsched-pressure starting at -O1. */
2528 { OPT_LEVELS_1_PLUS, OPT_fsched_pressure, NULL, 1 },
2529 { OPT_LEVELS_2_PLUS, OPT_free, NULL, 1 },
2530 #if TARGET_DEFAULT_ASYNC_UNWIND_TABLES == 1
2531 { OPT_LEVELS_ALL, OPT_fasynchronous_unwind_tables, NULL, 1 },
2532 { OPT_LEVELS_ALL, OPT_funwind_tables, NULL, 1},
2533 #endif
2534 { OPT_LEVELS_NONE, 0, NULL, 0 }
2535 };
2536
2537 #undef TARGET_OPTION_OPTIMIZATION_TABLE
2538 #define TARGET_OPTION_OPTIMIZATION_TABLE riscv_option_optimization_table
2539
2540 #undef TARGET_HANDLE_OPTION
2541 #define TARGET_HANDLE_OPTION riscv_handle_option
2542
2543 #undef TARGET_GET_VALID_OPTION_VALUES
2544 #define TARGET_GET_VALID_OPTION_VALUES riscv_get_valid_option_values
2545
2546 struct gcc_targetm_common targetm_common = TARGETM_COMMON_INITIALIZER;