| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Tool for canonical RISC-V architecture string. |
| 4 |
# Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 5 |
# Contributed by Andrew Waterman ([email protected]). |
| 6 |
# |
| 7 |
# This file is part of GCC. |
| 8 |
# |
| 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. |
| 13 |
# |
| 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. |
| 18 |
# |
| 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/>. |
| 22 |
|
| 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. |
| 26 |
|
| 27 |
from __future__ import print_function |
| 28 |
import sys |
| 29 |
import argparse |
| 30 |
import collections |
| 31 |
import itertools |
| 32 |
from functools import reduce |
| 33 |
|
| 34 |
SUPPORTED_ISA_SPEC = ["2.2", "20190608", "20191213"] |
| 35 |
CANONICAL_ORDER = "imafdgqlcbkjtpvn" |
| 36 |
LONG_EXT_PREFIXES = ['z', 's', 'h', 'x'] |
| 37 |
|
| 38 |
# |
| 39 |
# IMPLIED_EXT(ext) -> implied extension list. |
| 40 |
# |
| 41 |
IMPLIED_EXT = { |
| 42 |
"d" : ["f", "zicsr"], |
| 43 |
|
| 44 |
"a" : ["zaamo", "zalrsc"], |
| 45 |
"zabha" : ["zaamo"], |
| 46 |
"zacas" : ["zaamo"], |
| 47 |
|
| 48 |
"f" : ["zicsr"], |
| 49 |
"b" : ["zba", "zbb", "zbs"], |
| 50 |
"zdinx" : ["zfinx", "zicsr"], |
| 51 |
"zfinx" : ["zicsr"], |
| 52 |
"zhinx" : ["zhinxmin", "zfinx", "zicsr"], |
| 53 |
"zhinxmin" : ["zfinx", "zicsr"], |
| 54 |
|
| 55 |
"zk" : ["zkn", "zkr", "zkt"], |
| 56 |
"zkn" : ["zbkb", "zbkc", "zbkx", "zkne", "zknd", "zknh"], |
| 57 |
"zks" : ["zbkb", "zbkc", "zbkx", "zksed", "zksh"], |
| 58 |
|
| 59 |
"v" : ["zvl128b", "zve64d"], |
| 60 |
"zve32x" : ["zvl32b"], |
| 61 |
"zve64x" : ["zve32x", "zvl64b"], |
| 62 |
"zve32f" : ["f", "zve32x"], |
| 63 |
"zve64f" : ["f", "zve32f", "zve64x"], |
| 64 |
"zve64d" : ["d", "zve64f"], |
| 65 |
|
| 66 |
"zvl64b" : ["zvl32b"], |
| 67 |
"zvl128b" : ["zvl64b"], |
| 68 |
"zvl256b" : ["zvl128b"], |
| 69 |
"zvl512b" : ["zvl256b"], |
| 70 |
"zvl1024b" : ["zvl512b"], |
| 71 |
"zvl2048b" : ["zvl1024b"], |
| 72 |
"zvl4096b" : ["zvl2048b"], |
| 73 |
"zvl8192b" : ["zvl4096b"], |
| 74 |
"zvl16384b" : ["zvl8192b"], |
| 75 |
"zvl32768b" : ["zvl16384b"], |
| 76 |
"zvl65536b" : ["zvl32768b"], |
| 77 |
|
| 78 |
"zvkn" : ["zvkned", "zvknhb", "zvkb", "zvkt"], |
| 79 |
"zvknc" : ["zvkn", "zvbc"], |
| 80 |
"zvkng" : ["zvkn", "zvkg"], |
| 81 |
"zvks" : ["zvksed", "zvksh", "zvkb", "zvkt"], |
| 82 |
"zvksc" : ["zvks", "zvbc"], |
| 83 |
"zvksg" : ["zvks", "zvkg"], |
| 84 |
"zvbb" : ["zvkb"], |
| 85 |
"zvbc" : ["zve64x"], |
| 86 |
"zvkb" : ["zve32x"], |
| 87 |
"zvkg" : ["zve32x"], |
| 88 |
"zvkned" : ["zve32x"], |
| 89 |
"zvknha" : ["zve32x"], |
| 90 |
"zvknhb" : ["zve64x"], |
| 91 |
"zvksed" : ["zve32x"], |
| 92 |
"zvksh" : ["zve32x"], |
| 93 |
} |
| 94 |
|
| 95 |
def arch_canonicalize(arch, isa_spec): |
| 96 |
# TODO: Support extension version. |
| 97 |
is_isa_spec_2p2 = isa_spec == '2.2' |
| 98 |
new_arch = "" |
| 99 |
extra_long_ext = [] |
| 100 |
std_exts = [] |
| 101 |
if arch[:5] in ['rv32e', 'rv32i', 'rv32g', 'rv64e', 'rv64i', 'rv64g']: |
| 102 |
new_arch = arch[:5].replace("g", "i") |
| 103 |
if arch[:5] in ['rv32g', 'rv64g']: |
| 104 |
std_exts = ['m', 'a', 'f', 'd'] |
| 105 |
if not is_isa_spec_2p2: |
| 106 |
extra_long_ext = ['zicsr', 'zifencei'] |
| 107 |
else: |
| 108 |
raise Exception("Unexpected arch: `%s`" % arch[:5]) |
| 109 |
|
| 110 |
# Find any Z, S, H or X |
| 111 |
long_ext_prefixes_idx = map(lambda x: arch.find(x), LONG_EXT_PREFIXES) |
| 112 |
|
| 113 |
# Filter out any non-existent index. |
| 114 |
long_ext_prefixes_idx = list(filter(lambda x: x != -1, long_ext_prefixes_idx)) |
| 115 |
if long_ext_prefixes_idx: |
| 116 |
first_long_ext_idx = min(long_ext_prefixes_idx) |
| 117 |
long_exts = arch[first_long_ext_idx:].split("_") |
| 118 |
std_exts += list(arch[5:first_long_ext_idx]) |
| 119 |
else: |
| 120 |
long_exts = [] |
| 121 |
std_exts += list(arch[5:]) |
| 122 |
|
| 123 |
long_exts += extra_long_ext |
| 124 |
|
| 125 |
# |
| 126 |
# Handle implied extensions. |
| 127 |
# |
| 128 |
any_change = True |
| 129 |
while any_change: |
| 130 |
any_change = False |
| 131 |
for ext in std_exts + long_exts: |
| 132 |
if ext in IMPLIED_EXT: |
| 133 |
implied_exts = IMPLIED_EXT[ext] |
| 134 |
for implied_ext in implied_exts: |
| 135 |
if implied_ext == 'zicsr' and is_isa_spec_2p2: |
| 136 |
continue |
| 137 |
|
| 138 |
if implied_ext not in std_exts + long_exts: |
| 139 |
long_exts.append(implied_ext) |
| 140 |
any_change = True |
| 141 |
|
| 142 |
# Single letter extension might appear in the long_exts list, |
| 143 |
# because we just append extensions list to the arch string. |
| 144 |
std_exts += list(filter(lambda x:len(x) == 1, long_exts)) |
| 145 |
|
| 146 |
def longext_sort (exts): |
| 147 |
if not exts.startswith("zxm") and exts.startswith("z"): |
| 148 |
# If "Z" extensions are named, they should be ordered first by CANONICAL. |
| 149 |
if exts[1] not in CANONICAL_ORDER: |
| 150 |
raise Exception("Unsupported extension `%s`" % exts) |
| 151 |
canonical_sort = CANONICAL_ORDER.index(exts[1]) |
| 152 |
else: |
| 153 |
canonical_sort = -1 |
| 154 |
return (exts.startswith("x"), exts.startswith("zxm"), |
| 155 |
LONG_EXT_PREFIXES.index(exts[0]), canonical_sort, exts[1:]) |
| 156 |
|
| 157 |
# Removing duplicates. |
| 158 |
long_exts = list(set(long_exts)) |
| 159 |
|
| 160 |
# Multi-letter extension must be in lexicographic order. |
| 161 |
long_exts = list(sorted(filter(lambda x:len(x) != 1, long_exts), |
| 162 |
key=longext_sort)) |
| 163 |
|
| 164 |
# Put extensions in canonical order. |
| 165 |
for ext in CANONICAL_ORDER: |
| 166 |
if ext in std_exts: |
| 167 |
new_arch += ext |
| 168 |
|
| 169 |
# Check every extension is processed. |
| 170 |
for ext in std_exts: |
| 171 |
if ext == '_': |
| 172 |
continue |
| 173 |
if ext not in CANONICAL_ORDER: |
| 174 |
raise Exception("Unsupported extension `%s`" % ext) |
| 175 |
|
| 176 |
# Concat rest of the multi-char extensions. |
| 177 |
if long_exts: |
| 178 |
new_arch += "_" + "_".join(long_exts) |
| 179 |
|
| 180 |
return new_arch |
| 181 |
|
| 182 |
if len(sys.argv) < 2: |
| 183 |
print ("Usage: %s <arch_str> [<arch_str>*]" % sys.argv) |
| 184 |
sys.exit(1) |
| 185 |
|
| 186 |
parser = argparse.ArgumentParser() |
| 187 |
parser.add_argument('-misa-spec', type=str, |
| 188 |
default='20191213', |
| 189 |
choices=SUPPORTED_ISA_SPEC) |
| 190 |
parser.add_argument('arch_strs', nargs=argparse.REMAINDER) |
| 191 |
|
| 192 |
args = parser.parse_args() |
| 193 |
|
| 194 |
for arch in args.arch_strs: |
| 195 |
print (arch_canonicalize(arch, args.misa_spec)) |