Prepare for SDCC 4.5.0 release.
[sdcc.git] / sdcc / support / cpp / gcc / config / riscv / arch-canonicalize
blobf36a2ca4593b412199ec69b2fe5332296b764df1
1 #!/usr/bin/env python
3 # Tool for canonical RISC-V architecture string.
4 # Copyright (C) 2011-2022 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 = "imafdgqlcbjktpvn"
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 "zk" : ["zkn", "zkr", "zkt"],
45 "zkn" : ["zbkb", "zbkc", "zbkx", "zkne", "zknd", "zknh"],
46 "zks" : ["zbkb", "zbkc", "zbkx", "zksed", "zksh"],
48 "v" : ["zvl128b", "zve64d"],
49 "zve32x" : ["zvl32b"],
50 "zve64x" : ["zve32x", "zvl64b"],
51 "zve32f" : ["f", "zve32x"],
52 "zve64f" : ["f", "zve32f", "zve64x"],
53 "zve64d" : ["d", "zve64f"],
55 "zvl64b" : ["zvl32b"],
56 "zvl128b" : ["zvl64b"],
57 "zvl256b" : ["zvl128b"],
58 "zvl512b" : ["zvl256b"],
59 "zvl1024b" : ["zvl512b"],
60 "zvl2048b" : ["zvl1024b"],
61 "zvl4096b" : ["zvl2048b"],
62 "zvl8192b" : ["zvl4096b"],
63 "zvl16384b" : ["zvl8192b"],
64 "zvl32768b" : ["zvl16384b"],
65 "zvl65536b" : ["zvl32768b"],
68 def arch_canonicalize(arch, isa_spec):
69 # TODO: Support extension version.
70 is_isa_spec_2p2 = isa_spec == '2.2'
71 new_arch = ""
72 extra_long_ext = []
73 if arch[:5] in ['rv32e', 'rv32i', 'rv32g', 'rv64i', 'rv64g']:
74 new_arch = arch[:5].replace("g", "imafd")
75 if arch[:5] in ['rv32g', 'rv64g']:
76 if not is_isa_spec_2p2:
77 extra_long_ext = ['zicsr', 'zifencei']
78 else:
79 raise Exception("Unexpected arch: `%s`" % arch[:5])
81 # Find any Z, S, H or X
82 long_ext_prefixes_idx = map(lambda x: arch.find(x), LONG_EXT_PREFIXES)
84 # Filter out any non-existent index.
85 long_ext_prefixes_idx = list(filter(lambda x: x != -1, long_ext_prefixes_idx))
86 if long_ext_prefixes_idx:
87 first_long_ext_idx = min(long_ext_prefixes_idx)
88 long_exts = arch[first_long_ext_idx:].split("_")
89 std_exts = list(arch[5:first_long_ext_idx])
90 else:
91 long_exts = []
92 std_exts = list(arch[5:])
94 long_exts += extra_long_ext
97 # Handle implied extensions.
99 any_change = True
100 while any_change:
101 any_change = False
102 for ext in std_exts + long_exts:
103 if ext in IMPLIED_EXT:
104 implied_exts = IMPLIED_EXT[ext]
105 for implied_ext in implied_exts:
106 if implied_ext == 'zicsr' and is_isa_spec_2p2:
107 continue
109 if implied_ext not in std_exts + long_exts:
110 long_exts.append(implied_ext)
111 any_change = True
113 # Single letter extension might appear in the long_exts list,
114 # becasue we just append extensions list to the arch string.
115 std_exts += list(filter(lambda x:len(x) == 1, long_exts))
117 def longext_sort (exts):
118 if not exts.startswith("zxm") and exts.startswith("z"):
119 # If "Z" extensions are named, they should be ordered first by CANONICAL.
120 if exts[1] not in CANONICAL_ORDER:
121 raise Exception("Unsupported extension `%s`" % exts)
122 canonical_sort = CANONICAL_ORDER.index(exts[1])
123 else:
124 canonical_sort = -1
125 return (exts.startswith("x"), exts.startswith("zxm"),
126 LONG_EXT_PREFIXES.index(exts[0]), canonical_sort, exts[1:])
128 # Removing duplicates.
129 long_exts = list(set(long_exts))
131 # Multi-letter extension must be in lexicographic order.
132 long_exts = list(sorted(filter(lambda x:len(x) != 1, long_exts),
133 key=longext_sort))
135 # Put extensions in canonical order.
136 for ext in CANONICAL_ORDER:
137 if ext in std_exts:
138 new_arch += ext
140 # Check every extension is processed.
141 for ext in std_exts:
142 if ext == '_':
143 continue
144 if ext not in CANONICAL_ORDER:
145 raise Exception("Unsupported extension `%s`" % ext)
147 # Concat rest of the multi-char extensions.
148 if long_exts:
149 new_arch += "_" + "_".join(long_exts)
151 return new_arch
153 if len(sys.argv) < 2:
154 print ("Usage: %s <arch_str> [<arch_str>*]" % sys.argv)
155 sys.exit(1)
157 parser = argparse.ArgumentParser()
158 parser.add_argument('-misa-spec', type=str,
159 default='20191213',
160 choices=SUPPORTED_ISA_SPEC)
161 parser.add_argument('arch_strs', nargs=argparse.REMAINDER)
163 args = parser.parse_args()
165 for arch in args.arch_strs:
166 print (arch_canonicalize(arch, args.misa_spec))