]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/riscv/arch-canonicalize
3 # Tool for canonical RISC-V architecture string.
4 # Copyright (C) 2011-2023 Free Software Foundation, Inc.
5 # Contributed by Andrew Waterman (andrew@sifive.com).
7 # This file is part of GCC.
9 # GCC is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3, or (at your option)
14 # GCC is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with GCC; see the file COPYING3. If not see
21 # <http://www.gnu.org/licenses/>.
23 # TODO: Extract riscv_subset_t from riscv-common.cc and make it can be compiled
24 # standalone to replace this script, that also prevents us implementing
25 # that twice and keep sync again and again.
27 from __future__
import print_function
32 from functools
import reduce
34 SUPPORTED_ISA_SPEC
= ["2.2", "20190608", "20191213"]
35 CANONICAL_ORDER
= "imafdgqlcbkjtpvn"
36 LONG_EXT_PREFIXES
= ['z', 's', 'h', 'x']
39 # IMPLIED_EXT(ext) -> implied extension list.
44 "zdinx" : ["zfinx", "zicsr"],
46 "zhinx" : ["zhinxmin", "zfinx", "zicsr"],
47 "zhinxmin" : ["zfinx", "zicsr"],
49 "zk" : ["zkn", "zkr", "zkt"],
50 "zkn" : ["zbkb", "zbkc", "zbkx", "zkne", "zknd", "zknh"],
51 "zks" : ["zbkb", "zbkc", "zbkx", "zksed", "zksh"],
53 "v" : ["zvl128b", "zve64d"],
54 "zve32x" : ["zvl32b"],
55 "zve64x" : ["zve32x", "zvl64b"],
56 "zve32f" : ["f", "zve32x"],
57 "zve64f" : ["f", "zve32f", "zve64x"],
58 "zve64d" : ["d", "zve64f"],
60 "zvl64b" : ["zvl32b"],
61 "zvl128b" : ["zvl64b"],
62 "zvl256b" : ["zvl128b"],
63 "zvl512b" : ["zvl256b"],
64 "zvl1024b" : ["zvl512b"],
65 "zvl2048b" : ["zvl1024b"],
66 "zvl4096b" : ["zvl2048b"],
67 "zvl8192b" : ["zvl4096b"],
68 "zvl16384b" : ["zvl8192b"],
69 "zvl32768b" : ["zvl16384b"],
70 "zvl65536b" : ["zvl32768b"],
73 def arch_canonicalize(arch
, isa_spec
):
74 # TODO: Support extension version.
75 is_isa_spec_2p2
= isa_spec
== '2.2'
79 if arch
[:5] in ['rv32e', 'rv32i', 'rv32g', 'rv64i', 'rv64g']:
80 new_arch
= arch
[:5].replace("g", "i")
81 if arch
[:5] in ['rv32g', 'rv64g']:
82 std_exts
= ['m', 'a', 'f', 'd']
83 if not is_isa_spec_2p2
:
84 extra_long_ext
= ['zicsr', 'zifencei']
86 raise Exception("Unexpected arch: `%s`" % arch
[:5])
88 # Find any Z, S, H or X
89 long_ext_prefixes_idx
= map(lambda x
: arch
.find(x
), LONG_EXT_PREFIXES
)
91 # Filter out any non-existent index.
92 long_ext_prefixes_idx
= list(filter(lambda x
: x
!= -1, long_ext_prefixes_idx
))
93 if long_ext_prefixes_idx
:
94 first_long_ext_idx
= min(long_ext_prefixes_idx
)
95 long_exts
= arch
[first_long_ext_idx
:].split("_")
96 std_exts
+= list(arch
[5:first_long_ext_idx
])
99 std_exts
+= list(arch
[5:])
101 long_exts
+= extra_long_ext
104 # Handle implied extensions.
109 for ext
in std_exts
+ long_exts
:
110 if ext
in IMPLIED_EXT
:
111 implied_exts
= IMPLIED_EXT
[ext
]
112 for implied_ext
in implied_exts
:
113 if implied_ext
== 'zicsr' and is_isa_spec_2p2
:
116 if implied_ext
not in std_exts
+ long_exts
:
117 long_exts
.append(implied_ext
)
120 # Single letter extension might appear in the long_exts list,
121 # becasue we just append extensions list to the arch string.
122 std_exts
+= list(filter(lambda x
:len(x
) == 1, long_exts
))
124 def longext_sort (exts
):
125 if not exts
.startswith("zxm") and exts
.startswith("z"):
126 # If "Z" extensions are named, they should be ordered first by CANONICAL.
127 if exts
[1] not in CANONICAL_ORDER
:
128 raise Exception("Unsupported extension `%s`" % exts
)
129 canonical_sort
= CANONICAL_ORDER
.index(exts
[1])
132 return (exts
.startswith("x"), exts
.startswith("zxm"),
133 LONG_EXT_PREFIXES
.index(exts
[0]), canonical_sort
, exts
[1:])
135 # Removing duplicates.
136 long_exts
= list(set(long_exts
))
138 # Multi-letter extension must be in lexicographic order.
139 long_exts
= list(sorted(filter(lambda x
:len(x
) != 1, long_exts
),
142 # Put extensions in canonical order.
143 for ext
in CANONICAL_ORDER
:
147 # Check every extension is processed.
151 if ext
not in CANONICAL_ORDER
:
152 raise Exception("Unsupported extension `%s`" % ext
)
154 # Concat rest of the multi-char extensions.
156 new_arch
+= "_" + "_".join(long_exts
)
160 if len(sys
.argv
) < 2:
161 print ("Usage: %s <arch_str> [<arch_str>*]" % sys
.argv
)
164 parser
= argparse
.ArgumentParser()
165 parser
.add_argument('-misa-spec', type=str,
167 choices
=SUPPORTED_ISA_SPEC
)
168 parser
.add_argument('arch_strs', nargs
=argparse
.REMAINDER
)
170 args
= parser
.parse_args()
172 for arch
in args
.arch_strs
:
173 print (arch_canonicalize(arch
, args
.misa_spec
))