|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import sys |
| 5 | + |
| 6 | +EXT_OPTS = { |
| 7 | + "zba": "zba=true", |
| 8 | + "zbb": "zbb=true", |
| 9 | + "zbc": "zbc=true", |
| 10 | + "zbs": "zbs=true", |
| 11 | + "v": "v=true", |
| 12 | + "zve32f": "Zve32f=true", |
| 13 | + "zve64f": "Zve64f=true", |
| 14 | + "zfh": "Zfh=true", |
| 15 | + "zfhmin": "Zfhmin=true", |
| 16 | +} |
| 17 | + |
| 18 | +SUPPORTTED_EXTS = "iemafdcbvph" |
| 19 | +MC_EXT_PREFIX = "zsx" |
| 20 | + |
| 21 | +def parse_opt(argv): |
| 22 | + parser = argparse.ArgumentParser() |
| 23 | + parser.add_argument('-march', '--with-arch', type=str, dest='march') |
| 24 | + parser.add_argument('-selftest', action='store_true') |
| 25 | + opt = parser.parse_args() |
| 26 | + return opt |
| 27 | + |
| 28 | +def parse_mc_ext(ext_str, idx): |
| 29 | + end_idx = ext_str[idx+1:].find('_') |
| 30 | + if end_idx == -1: |
| 31 | + end_idx = len(ext_str) |
| 32 | + else: |
| 33 | + end_idx = end_idx + idx + 1 |
| 34 | + major = 0 |
| 35 | + minor = 0 |
| 36 | + version_begin_idx = end_idx |
| 37 | + if ext_str[end_idx-1].isdigit(): |
| 38 | + # This ext is come with version. |
| 39 | + v_idx = end_idx - 1 |
| 40 | + while (ext_str[v_idx].isdigit()) and v_idx > idx: |
| 41 | + v_idx -= 1 |
| 42 | + major = int(ext_str[v_idx+1:end_idx]) |
| 43 | + version_begin_idx = v_idx+1 |
| 44 | + if (ext_str[v_idx] == 'p'): |
| 45 | + minor = major |
| 46 | + major_v_idx = v_idx - 1 |
| 47 | + while (ext_str[major_v_idx].isdigit()) and major_v_idx > idx: |
| 48 | + major_v_idx -= 1 |
| 49 | + major = int(ext_str[major_v_idx+1:v_idx]) |
| 50 | + version_begin_idx = major_v_idx+1 |
| 51 | + |
| 52 | + return end_idx, ext_str[idx:version_begin_idx], major, minor |
| 53 | + |
| 54 | +def parse_version(ext_str, idx): |
| 55 | + major = 2 |
| 56 | + minor = 0 |
| 57 | + strlen = len(ext_str) |
| 58 | + end_idx = idx + 1 |
| 59 | + if idx+1 < strlen and ext_str[idx+1].isdigit(): |
| 60 | + v_idx = idx + 1 |
| 61 | + while v_idx < strlen and (ext_str[v_idx].isdigit()): |
| 62 | + v_idx += 1 |
| 63 | + major = int(ext_str[idx+1:v_idx]) |
| 64 | + end_idx = v_idx |
| 65 | + if (ext_str[v_idx] == 'p'): |
| 66 | + minor_v_idx = v_idx + 1 |
| 67 | + while minor_v_idx < strlen and (ext_str[minor_v_idx].isdigit()): |
| 68 | + minor_v_idx += 1 |
| 69 | + minor = int(ext_str[v_idx+1:minor_v_idx]) |
| 70 | + end_idx = minor_v_idx |
| 71 | + |
| 72 | + return end_idx, ext_str[idx], major, minor |
| 73 | + |
| 74 | +def parse_march(march): |
| 75 | + if len(march) < 5: |
| 76 | + return None |
| 77 | + march = march.replace("rv64g", "rv64imafd").replace("rv32g", "rv32imafd") |
| 78 | + if march[0:5] not in ['rv64i', 'rv32i', 'rv32e']: |
| 79 | + print (march[0:5]) |
| 80 | + return None |
| 81 | + |
| 82 | + ext_str = march[4:] |
| 83 | + idx = 0 |
| 84 | + extstrlens = len(ext_str) |
| 85 | + exts = dict() |
| 86 | + while idx < extstrlens: |
| 87 | + if ext_str[idx] in SUPPORTTED_EXTS: |
| 88 | + idx, ext_name, major, minor = parse_version(ext_str, idx) |
| 89 | + elif ext_str[idx] in MC_EXT_PREFIX: |
| 90 | + idx, ext_name, major, minor = parse_mc_ext(ext_str, idx) |
| 91 | + elif ext_str[idx] == '_': |
| 92 | + idx = idx + 1 |
| 93 | + continue |
| 94 | + else: |
| 95 | + raise Exception("Unrecognized ext : `%s`, %s" % |
| 96 | + (ext_str[idx], ext_str)) |
| 97 | + exts[ext_name] = (major, minor) |
| 98 | + return exts |
| 99 | + |
| 100 | +def get_vlen(ext_dict): |
| 101 | + vlen = 0 |
| 102 | + for ext in ext_dict.keys(): |
| 103 | + if ext == 'v': |
| 104 | + vlen = max(vlen, 128) |
| 105 | + elif (ext.startswith('zvl') and ext[-1] == 'b'): |
| 106 | + zvlen = int(arch[3:-1]) |
| 107 | + vlen = max(vlen, zvlen) |
| 108 | + elif ext.startswith("zve"): |
| 109 | + zvelen = int(arch[3:-1]) |
| 110 | + vlen = max(vlen, zvelen) |
| 111 | + return vlen |
| 112 | + |
| 113 | +def conver_arch_to_qemu_cpu_opt(march): |
| 114 | + if len(march) < 5: |
| 115 | + return None |
| 116 | + |
| 117 | + ext_dict = parse_march(march) |
| 118 | + |
| 119 | + cpu_opt = [] |
| 120 | + cpu_opt.append(march[0:4]) # rv32 or rv64 |
| 121 | + |
| 122 | + vlen = get_vlen(ext_dict) |
| 123 | + |
| 124 | + if vlen != 0: |
| 125 | + cpu_opt.append("vlen=%d" % vlen) |
| 126 | + |
| 127 | + for ext in ext_dict.keys(): |
| 128 | + if ext in EXT_OPTS: |
| 129 | + cpu_opt.append(EXT_OPTS[ext]) |
| 130 | + return ",".join(cpu_opt) |
| 131 | + |
| 132 | +def selftest(): |
| 133 | + print(parse_march("rv64gc")) |
| 134 | + |
| 135 | +def main(argv): |
| 136 | + opt = parse_opt(argv) |
| 137 | + if opt.selftest: |
| 138 | + selftest() |
| 139 | + return 0 |
| 140 | + cpu_opt = conver_arch_to_qemu_cpu_opt(opt.march) |
| 141 | + print (cpu_opt) |
| 142 | + |
| 143 | +if __name__ == '__main__': |
| 144 | + sys.exit(main(sys.argv)) |
0 commit comments