blob: a4fcd622c541e4f2943508079d18ca4af26727aa [file] [log] [blame]
Taylor Simpson1547a2d2024-01-15 15:14:41 -07001#!/usr/bin/env python3
2
3##
4## Copyright (c) 2024 Taylor Simpson <ltaylorsimpson@gmail.com>
5##
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 2 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, see <http://www.gnu.org/licenses/>.
18##
19
20import io
21import re
22
23import sys
24import textwrap
25import iset
26import hex_common
27
28encs = {
29 tag: "".join(reversed(iset.iset[tag]["enc"].replace(" ", "")))
30 for tag in iset.tags
31 if iset.iset[tag]["enc"] != "MISSING ENCODING"
32}
33
34
35regre = re.compile(r"((?<!DUP)[MNORCPQXSGVZA])([stuvwxyzdefg]+)([.]?[LlHh]?)(\d+S?)")
36immre = re.compile(r"[#]([rRsSuUm])(\d+)(?:[:](\d+))?")
37
38
39def ordered_unique(l):
40 return sorted(set(l), key=l.index)
41
42num_registers = {"R": 32, "V": 32}
43
44operand_letters = {
45 "P",
46 "i",
47 "I",
48 "r",
49 "s",
50 "t",
51 "u",
52 "v",
53 "w",
54 "x",
55 "y",
56 "z",
57 "d",
58 "e",
59 "f",
60 "g",
61}
62
63#
64# These instructions have unused operand letters in their encoding
65# They don't correspond to actual operands in the instruction semantics
66# We will mark them as ignored in QEMU decodetree
67#
68tags_with_unused_d_encoding = {
69 "R6_release_at_vi",
70 "R6_release_st_vi",
71 "S4_stored_rl_at_vi",
72 "S4_stored_rl_st_vi",
73 "S2_storew_rl_at_vi",
74 "S2_stored_rl_at_vi",
75 "S2_storew_rl_st_vi",
76}
77
78tags_with_unused_t_encoding = {
79 "R6_release_at_vi",
80 "R6_release_st_vi",
81}
82
83def skip_tag(tag, class_to_decode):
84 enc_class = iset.iset[tag]["enc_class"]
85 return enc_class != class_to_decode
86
87
88##
89## Generate the QEMU decodetree file for each instruction in class_to_decode
90## For A2_add: Rd32=add(Rs32,Rt32)
91## We produce:
92## %A2_add_Rd 0:5
93## %A2_add_Rs 16:5
94## %A2_add_Rt 8:5
95## @A2_add 11110011000.......-.....---..... Rd=%A2_add_Rd Rs=%A2_add_Rs Rt=%A2_add_Rt %PP
96## A2_add ..................-.....---..... @A2_add
97##
98def gen_decodetree_file(f, class_to_decode):
Taylor Simpsonf6c01002024-01-15 15:14:42 -070099 is_subinsn = class_to_decode.startswith("SUBINSN_")
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700100 f.write(f"## DO NOT MODIFY - This file is generated by {sys.argv[0]}\n\n")
Taylor Simpsonf6c01002024-01-15 15:14:42 -0700101 if not is_subinsn:
102 f.write("%PP\t14:2\n\n")
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700103 for tag in sorted(encs.keys(), key=iset.tags.index):
104 if skip_tag(tag, class_to_decode):
105 continue
106
107 enc = encs[tag]
108 enc_str = "".join(reversed(encs[tag]))
109 f.write(("#" * 80) + "\n"
110 f"## {tag}:\t{enc_str}\n"
111 "##\n")
112
Taylor Simpsonf6c01002024-01-15 15:14:42 -0700113 # The subinstructions come with a 13-bit encoding, but
114 # decodetree.py needs 16 bits
115 if is_subinsn:
116 enc_str = "---" + enc_str
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700117
118 regs = ordered_unique(regre.findall(iset.iset[tag]["syntax"]))
119 imms = ordered_unique(immre.findall(iset.iset[tag]["syntax"]))
120
121 # Write the field definitions for the registers
122 for regno, reg in enumerate(regs):
123 reg_type, reg_id, _, reg_enc_size = reg
124 reg_letter = reg_id[0]
125 reg_num_choices = int(reg_enc_size.rstrip("S"))
126 reg_mapping = reg_type + "".join("_" for letter in reg_id) + \
127 reg_enc_size
128 reg_enc_fields = re.findall(reg_letter + "+", enc)
129
130 # Check for some errors
131 if len(reg_enc_fields) == 0:
132 raise Exception(f"{tag} missing register field!")
133 if len(reg_enc_fields) > 1:
134 raise Exception(f"{tag} has split register field!")
135 reg_enc_field = reg_enc_fields[0]
136 if 2 ** len(reg_enc_field) != reg_num_choices:
137 raise Exception(f"{tag} has incorrect register field width!")
138
139 f.write(f"%{tag}_{reg_type}{reg_id}\t"
140 f"{enc.index(reg_enc_field)}:{len(reg_enc_field)}")
141
142 if (reg_type in num_registers and
143 reg_num_choices != num_registers[reg_type]):
144 f.write(f"\t!function=decode_mapped_reg_{reg_mapping}")
145 f.write("\n")
146
147 # Write the field definitions for the immediates
148 for imm in imms:
149 immno = 1 if imm[0].isupper() else 0
150 imm_type = imm[0]
151 imm_width = int(imm[1])
152 imm_letter = "i" if imm_type.islower() else "I"
153 fields = []
154 sign_mark = "s" if imm_type.lower() in "sr" else ""
155 for m in reversed(list(re.finditer(imm_letter + "+", enc))):
156 fields.append(f"{m.start()}:{sign_mark}{m.end() - m.start()}")
157 sign_mark = ""
158 field_str = " ".join(fields)
159 f.write(f"%{tag}_{imm_type}{imm_letter}\t{field_str}\n")
160
161 ## Handle instructions with unused encoding letters
162 ## Change the unused letters to ignored
163 if tag in tags_with_unused_d_encoding:
164 enc_str = enc_str.replace("d", "-")
165 if tag in tags_with_unused_t_encoding:
166 enc_str = enc_str.replace("t", "-")
167
168 # Replace the operand letters with .
169 for x in operand_letters:
170 enc_str = enc_str.replace(x, ".")
171
172 # Write the instruction format
173 f.write(f"@{tag}\t{enc_str}")
174 for reg in regs:
175 reg_type = reg[0]
176 reg_id = reg[1]
177 f.write(f" {reg_type}{reg_id}=%{tag}_{reg_type}{reg_id}")
178 for imm in imms:
179 imm_type = imm[0]
180 imm_letter = "i" if imm_type.islower() else "I"
181 f.write(f" {imm_type}{imm_letter}=%{tag}_{imm_type}{imm_letter}")
182
Taylor Simpsonf6c01002024-01-15 15:14:42 -0700183 if not is_subinsn:
184 f.write(" %PP")
185 f.write("\n")
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700186
187 # Replace the 0s and 1s with .
188 enc_str = enc_str.replace("0", ".").replace("1", ".")
189
190 # Write the instruction pattern
191 f.write(f"{tag}\t{enc_str} @{tag}\n")
192
193
194if __name__ == "__main__":
195 hex_common.read_semantics_file(sys.argv[1])
196 class_to_decode = sys.argv[2]
197 with open(sys.argv[3], "w") as f:
198 gen_decodetree_file(f, class_to_decode)