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 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 42 43def code_fmt(txt): 44 return textwrap.indent(textwrap.dedent(txt), " ") 45 46open_curly = "{" 47close_curly = "}" 48 49def mark_which_imm_extended(f, tag): 50 immre = re.compile(r"IMMEXT\([rRsSuUm]") 51 imm = immre.findall(hex_common.semdict[tag]) 52 if len(imm) == 0: 53 # No extended operand found 54 return 55 letter = re.split("\\(", imm[0])[1] 56 f.write(code_fmt(f"""\ 57 insn->which_extended = {0 if letter.islower() else 1}; 58 """)) 59 60## 61## Generate the QEMU decodetree trans_<tag> function for each instruction 62## For A2_add: Rd32=add(Rs32,Rt32) 63## We produce: 64## static bool trans_A2_add(DisasContext *ctx, arg_A2_add *args) 65## { 66## Insn *insn = ctx->insn; 67## insn->opcode = A2_add; 68## insn->regno[0] = args->Rd; 69## insn->regno[1] = args->Rs; 70## insn->regno[2] = args->Rt; 71## return true; 72## } 73## 74def gen_trans_funcs(f): 75 f.write(f"/* DO NOT MODIFY - This file is generated by {sys.argv[0]} */\n\n") 76 for tag in sorted(encs.keys(), key=iset.tags.index): 77 regs = ordered_unique(regre.findall(iset.iset[tag]["syntax"])) 78 imms = ordered_unique(immre.findall(iset.iset[tag]["syntax"])) 79 80 f.write(textwrap.dedent(f"""\ 81 static bool trans_{tag}(DisasContext *ctx, arg_{tag} *args) 82 {open_curly} 83 Insn *insn = ctx->insn; 84 insn->opcode = {tag}; 85 """)) 86 87 regno = 0 88 for reg in regs: 89 reg_type = reg[0] 90 reg_id = reg[1] 91 f.write(code_fmt(f"""\ 92 insn->regno[{regno}] = args->{reg_type}{reg_id}; 93 """)) 94 regno += 1 95 96 if len(imms) != 0: 97 mark_which_imm_extended(f, tag) 98 99 for imm in imms: 100 imm_type = imm[0] 101 imm_letter = "i" if imm_type.islower() else "I" 102 immno = 0 if imm_type.islower() else 1 103 imm_shift = int(imm[2]) if imm[2] else 0 104 if imm_shift: 105 f.write(code_fmt(f"""\ 106 insn->immed[{immno}] = 107 shift_left(ctx, args->{imm_type}{imm_letter}, 108 {imm_shift}, {immno}); 109 """)) 110 else: 111 f.write(code_fmt(f"""\ 112 insn->immed[{immno}] = args->{imm_type}{imm_letter}; 113 """)) 114 115 f.write(textwrap.dedent(f"""\ 116 return true; 117 {close_curly} 118 """)) 119 120 121if __name__ == "__main__": 122 hex_common.read_semantics_file(sys.argv[1]) 123 with open(sys.argv[2], "w") as f: 124 gen_trans_funcs(f) 125