3 # Tool for canonical RISC-V architecture string.
4 # Copyright (C) 2011-2024 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"],
72 "zvkn" : ["zvkned", "zvknhb", "zvkb", "zvkt"],
73 "zvknc" : ["zvkn", "zvbc"],
74 "zvkng" : ["zvkn", "zvkg"],
75 "zvks" : ["zvksed", "zvksh", "zvkb", "zvkt"],
76 "zvksc" : ["zvks", "zvbc"],
77 "zvksg" : ["zvks", "zvkg"],
82 "zvkned" : ["zve32x"],
83 "zvknha" : ["zve32x"],
84 "zvknhb" : ["zve64x"],
85 "zvksed" : ["zve32x"],
89 def arch_canonicalize(arch
, isa_spec
):
90 # TODO: Support extension version.
91 is_isa_spec_2p2
= isa_spec
== '2.2'
95 if arch
[:5] in ['rv32e', 'rv32i', 'rv32g', 'rv64e', 'rv64i', 'rv64g']:
96 new_arch
= arch
[:5].replace("g", "i")
97 if arch
[:5] in ['rv32g', 'rv64g']:
98 std_exts
= ['m', 'a', 'f', 'd']
99 if not is_isa_spec_2p2
:
100 extra_long_ext
= ['zicsr', 'zifencei']
102 raise Exception("Unexpected arch: `%s`" % arch
[:5])
104 # Find any Z, S, H or X
105 long_ext_prefixes_idx
= map(lambda x
: arch
.find(x
), LONG_EXT_PREFIXES
)
107 # Filter out any non-existent index.
108 long_ext_prefixes_idx
= list(filter(lambda x
: x
!= -1, long_ext_prefixes_idx
))
109 if long_ext_prefixes_idx
:
110 first_long_ext_idx
= min(long_ext_prefixes_idx
)
111 long_exts
= arch
[first_long_ext_idx
:].split("_")
112 std_exts
+= list(arch
[5:first_long_ext_idx
])
115 std_exts
+= list(arch
[5:])
117 long_exts
+= extra_long_ext
120 # Handle implied extensions.
125 for ext
in std_exts
+ long_exts
:
126 if ext
in IMPLIED_EXT
:
127 implied_exts
= IMPLIED_EXT
[ext
]
128 for implied_ext
in implied_exts
:
129 if implied_ext
== 'zicsr' and is_isa_spec_2p2
:
132 if implied_ext
not in std_exts
+ long_exts
:
133 long_exts
.append(implied_ext
)
136 # Single letter extension might appear in the long_exts list,
137 # becasue we just append extensions list to the arch string.
138 std_exts
+= list(filter(lambda x
:len(x
) == 1, long_exts
))
140 def longext_sort (exts
):
141 if not exts
.startswith("zxm") and exts
.startswith("z"):
142 # If "Z" extensions are named, they should be ordered first by CANONICAL.
143 if exts
[1] not in CANONICAL_ORDER
:
144 raise Exception("Unsupported extension `%s`" % exts
)
145 canonical_sort
= CANONICAL_ORDER
.index(exts
[1])
148 return (exts
.startswith("x"), exts
.startswith("zxm"),
149 LONG_EXT_PREFIXES
.index(exts
[0]), canonical_sort
, exts
[1:])
151 # Removing duplicates.
152 long_exts
= list(set(long_exts
))
154 # Multi-letter extension must be in lexicographic order.
155 long_exts
= list(sorted(filter(lambda x
:len(x
) != 1, long_exts
),
158 # Put extensions in canonical order.
159 for ext
in CANONICAL_ORDER
:
163 # Check every extension is processed.
167 if ext
not in CANONICAL_ORDER
:
168 raise Exception("Unsupported extension `%s`" % ext
)
170 # Concat rest of the multi-char extensions.
172 new_arch
+= "_" + "_".join(long_exts
)
176 if len(sys
.argv
) < 2:
177 print ("Usage: %s <arch_str> [<arch_str>*]" % sys
.argv
)
180 parser
= argparse
.ArgumentParser()
181 parser
.add_argument('-misa-spec', type=str,
183 choices
=SUPPORTED_ISA_SPEC
)
184 parser
.add_argument('arch_strs', nargs
=argparse
.REMAINDER
)
186 args
= parser
.parse_args()
188 for arch
in args
.arch_strs
:
189 print (arch_canonicalize(arch
, args
.misa_spec
))