Add hppa*-*-hpux* to targets which do not support split DWARF
[official-gcc.git] / gcc / config / riscv / arch-canonicalize
blob629bed853471c2ff709956a174f343d10f655dfd
1 #!/usr/bin/env python
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)
12 # any later version.
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
28 import sys
29 import argparse
30 import collections
31 import itertools
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.
41 IMPLIED_EXT = {
42 "d" : ["f", "zicsr"],
43 "f" : ["zicsr"],
44 "zdinx" : ["zfinx", "zicsr"],
45 "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"],
78 "zvbb" : ["zvkb"],
79 "zvbc" : ["zve64x"],
80 "zvkb" : ["zve32x"],
81 "zvkg" : ["zve32x"],
82 "zvkned" : ["zve32x"],
83 "zvknha" : ["zve32x"],
84 "zvknhb" : ["zve64x"],
85 "zvksed" : ["zve32x"],
86 "zvksh" : ["zve32x"],
89 def arch_canonicalize(arch, isa_spec):
90 # TODO: Support extension version.
91 is_isa_spec_2p2 = isa_spec == '2.2'
92 new_arch = ""
93 extra_long_ext = []
94 std_exts = []
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']
101 else:
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])
113 else:
114 long_exts = []
115 std_exts += list(arch[5:])
117 long_exts += extra_long_ext
120 # Handle implied extensions.
122 any_change = True
123 while any_change:
124 any_change = False
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:
130 continue
132 if implied_ext not in std_exts + long_exts:
133 long_exts.append(implied_ext)
134 any_change = True
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])
146 else:
147 canonical_sort = -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),
156 key=longext_sort))
158 # Put extensions in canonical order.
159 for ext in CANONICAL_ORDER:
160 if ext in std_exts:
161 new_arch += ext
163 # Check every extension is processed.
164 for ext in std_exts:
165 if ext == '_':
166 continue
167 if ext not in CANONICAL_ORDER:
168 raise Exception("Unsupported extension `%s`" % ext)
170 # Concat rest of the multi-char extensions.
171 if long_exts:
172 new_arch += "_" + "_".join(long_exts)
174 return new_arch
176 if len(sys.argv) < 2:
177 print ("Usage: %s <arch_str> [<arch_str>*]" % sys.argv)
178 sys.exit(1)
180 parser = argparse.ArgumentParser()
181 parser.add_argument('-misa-spec', type=str,
182 default='20191213',
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))