1#!/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 27import argparse 28 29encs = { 30 tag: "".join(reversed(iset.iset[tag]["enc"].replace(" ", ""))) 31 for tag in iset.tags 32 if iset.iset[tag]["enc"] != "MISSING ENCODING" 33} 34 35 36regre = re.compile(r"((?<!DUP)[MNORCPQXSGVZA])([stuvwxyzdefg]+)([.]?[LlHh]?)(\d+S?)") 37immre = re.compile(r"[#]([rRsSuUm])(\d+)(?:[:](\d+))?") 38 39 40def ordered_unique(l): 41 return sorted(set(l), key=l.index) 42 43num_registers = {"R": 32, "V": 32} 44 45operand_letters = { 46 "P", 47 "i", 48 "I", 49 "r", 50 "s", 51 "t", 52 "u", 53 "v", 54 "w", 55 "x", 56 "y", 57 "z", 58 "d", 59 "e", 60 "f", 61 "g", 62} 63 64# 65# These instructions have unused operand letters in their encoding 66# They don't correspond to actual operands in the instruction semantics 67# We will mark them as ignored in QEMU decodetree 68# 69tags_with_unused_d_encoding = { 70 "R6_release_at_vi", 71 "R6_release_st_vi", 72 "S4_stored_rl_at_vi", 73 "S4_stored_rl_st_vi", 74 "S2_storew_rl_at_vi", 75 "S2_stored_rl_at_vi", 76 "S2_storew_rl_st_vi", 77} 78 79tags_with_unused_t_encoding = { 80 "R6_release_at_vi", 81 "R6_release_st_vi", 82} 83 84def skip_tag(tag, class_to_decode): 85 enc_class = iset.iset[tag]["enc_class"] 86 return enc_class != class_to_decode 87 88 89## 90## Generate the QEMU decodetree file for each instruction in class_to_decode 91## For A2_add: Rd32=add(Rs32,Rt32) 92## We produce: 93## %A2_add_Rd 0:5 94## %A2_add_Rs 16:5 95## %A2_add_Rt 8:5 96## @A2_add 11110011000.......-.....---..... Rd=%A2_add_Rd Rs=%A2_add_Rs Rt=%A2_add_Rt %PP 97## A2_add ..................-.....---..... @A2_add 98## 99def gen_decodetree_file(f, class_to_decode): 100 is_subinsn = class_to_decode.startswith("SUBINSN_") 101 f.write(f"## DO NOT MODIFY - This file is generated by {sys.argv[0]}\n\n") 102 if not is_subinsn: 103 f.write("%PP\t14:2\n\n") 104 for tag in sorted(encs.keys(), key=iset.tags.index): 105 if skip_tag(tag, class_to_decode): 106 continue 107 108 enc = encs[tag] 109 enc_str = "".join(reversed(encs[tag])) 110 f.write(("#" * 80) + "\n" 111 f"## {tag}:\t{enc_str}\n" 112 "##\n") 113 114 # The subinstructions come with a 13-bit encoding, but 115 # decodetree.py needs 16 bits 116 if is_subinsn: 117 enc_str = "---" + enc_str 118 119 regs = ordered_unique(regre.findall(iset.iset[tag]["syntax"])) 120 imms = ordered_unique(immre.findall(iset.iset[tag]["syntax"])) 121 122 # Write the field definitions for the registers 123 for regno, reg in enumerate(regs): 124 reg_type, reg_id, _, reg_enc_size = reg 125 reg_letter = reg_id[0] 126 reg_num_choices = int(reg_enc_size.rstrip("S")) 127 reg_mapping = reg_type + "".join("_" for letter in reg_id) + \ 128 reg_enc_size 129 reg_enc_fields = re.findall(reg_letter + "+", enc) 130 131 # Check for some errors 132 if len(reg_enc_fields) == 0: 133 raise Exception(f"{tag} missing register field!") 134 if len(reg_enc_fields) > 1: 135 raise Exception(f"{tag} has split register field!") 136 reg_enc_field = reg_enc_fields[0] 137 if 2 ** len(reg_enc_field) != reg_num_choices: 138 raise Exception(f"{tag} has incorrect register field width!") 139 140 f.write(f"%{tag}_{reg_type}{reg_id}\t" 141 f"{enc.index(reg_enc_field)}:{len(reg_enc_field)}") 142 143 if (reg_type in num_registers and 144 reg_num_choices != num_registers[reg_type]): 145 f.write(f"\t!function=decode_mapped_reg_{reg_mapping}") 146 f.write("\n") 147 148 # Write the field definitions for the immediates 149 for imm in imms: 150 immno = 1 if imm[0].isupper() else 0 151 imm_type = imm[0] 152 imm_width = int(imm[1]) 153 imm_letter = "i" if imm_type.islower() else "I" 154 fields = [] 155 sign_mark = "s" if imm_type.lower() in "sr" else "" 156 for m in reversed(list(re.finditer(imm_letter + "+", enc))): 157 fields.append(f"{m.start()}:{sign_mark}{m.end() - m.start()}") 158 sign_mark = "" 159 field_str = " ".join(fields) 160 f.write(f"%{tag}_{imm_type}{imm_letter}\t{field_str}\n") 161 162 ## Handle instructions with unused encoding letters 163 ## Change the unused letters to ignored 164 if tag in tags_with_unused_d_encoding: 165 enc_str = enc_str.replace("d", "-") 166 if tag in tags_with_unused_t_encoding: 167 enc_str = enc_str.replace("t", "-") 168 169 # Replace the operand letters with . 170 for x in operand_letters: 171 enc_str = enc_str.replace(x, ".") 172 173 # Write the instruction format 174 f.write(f"@{tag}\t{enc_str}") 175 for reg in regs: 176 reg_type = reg[0] 177 reg_id = reg[1] 178 f.write(f" {reg_type}{reg_id}=%{tag}_{reg_type}{reg_id}") 179 for imm in imms: 180 imm_type = imm[0] 181 imm_letter = "i" if imm_type.islower() else "I" 182 f.write(f" {imm_type}{imm_letter}=%{tag}_{imm_type}{imm_letter}") 183 184 if not is_subinsn: 185 f.write(" %PP") 186 f.write("\n") 187 188 # Replace the 0s and 1s with . 189 enc_str = enc_str.replace("0", ".").replace("1", ".") 190 191 # Write the instruction pattern 192 f.write(f"{tag}\t{enc_str} @{tag}\n") 193 194 195def main(): 196 parser = argparse.ArgumentParser( 197 description="Emit opaque macro calls with instruction semantics" 198 ) 199 parser.add_argument("semantics", help="semantics file") 200 parser.add_argument("class_to_decode", help="instruction class to decode") 201 parser.add_argument("out", help="output file") 202 args = parser.parse_args() 203 204 hex_common.read_semantics_file(args.semantics) 205 with open(args.out, "w") as f: 206 gen_decodetree_file(f, args.class_to_decode) 207 208if __name__ == "__main__": 209 main() 210