11547a2d3STaylor Simpson#!/usr/bin/env python3 21547a2d3STaylor Simpson 31547a2d3STaylor Simpson## 41547a2d3STaylor Simpson## Copyright (c) 2024 Taylor Simpson <ltaylorsimpson@gmail.com> 51547a2d3STaylor Simpson## 61547a2d3STaylor Simpson## This program is free software; you can redistribute it and/or modify 71547a2d3STaylor Simpson## it under the terms of the GNU General Public License as published by 81547a2d3STaylor Simpson## the Free Software Foundation; either version 2 of the License, or 91547a2d3STaylor Simpson## (at your option) any later version. 101547a2d3STaylor Simpson## 111547a2d3STaylor Simpson## This program is distributed in the hope that it will be useful, 121547a2d3STaylor Simpson## but WITHOUT ANY WARRANTY; without even the implied warranty of 131547a2d3STaylor Simpson## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 141547a2d3STaylor Simpson## GNU General Public License for more details. 151547a2d3STaylor Simpson## 161547a2d3STaylor Simpson## You should have received a copy of the GNU General Public License 171547a2d3STaylor Simpson## along with this program; if not, see <http://www.gnu.org/licenses/>. 181547a2d3STaylor Simpson## 191547a2d3STaylor Simpson 201547a2d3STaylor Simpsonimport io 211547a2d3STaylor Simpsonimport re 221547a2d3STaylor Simpson 231547a2d3STaylor Simpsonimport sys 241547a2d3STaylor Simpsonimport textwrap 251547a2d3STaylor Simpsonimport iset 261547a2d3STaylor Simpsonimport hex_common 271547a2d3STaylor Simpson 281547a2d3STaylor Simpsonencs = { 291547a2d3STaylor Simpson tag: "".join(reversed(iset.iset[tag]["enc"].replace(" ", ""))) 301547a2d3STaylor Simpson for tag in iset.tags 311547a2d3STaylor Simpson if iset.iset[tag]["enc"] != "MISSING ENCODING" 321547a2d3STaylor Simpson} 331547a2d3STaylor Simpson 341547a2d3STaylor Simpson 351547a2d3STaylor Simpsonregre = re.compile(r"((?<!DUP)[MNORCPQXSGVZA])([stuvwxyzdefg]+)([.]?[LlHh]?)(\d+S?)") 361547a2d3STaylor Simpsonimmre = re.compile(r"[#]([rRsSuUm])(\d+)(?:[:](\d+))?") 371547a2d3STaylor Simpson 381547a2d3STaylor Simpson 391547a2d3STaylor Simpsondef ordered_unique(l): 401547a2d3STaylor Simpson return sorted(set(l), key=l.index) 411547a2d3STaylor Simpson 421547a2d3STaylor Simpson 431547a2d3STaylor Simpsondef code_fmt(txt): 441547a2d3STaylor Simpson return textwrap.indent(textwrap.dedent(txt), " ") 451547a2d3STaylor Simpson 461547a2d3STaylor Simpsonopen_curly = "{" 471547a2d3STaylor Simpsonclose_curly = "}" 481547a2d3STaylor Simpson 491547a2d3STaylor Simpsondef mark_which_imm_extended(f, tag): 501547a2d3STaylor Simpson immre = re.compile(r"IMMEXT\([rRsSuUm]") 511547a2d3STaylor Simpson imm = immre.findall(hex_common.semdict[tag]) 521547a2d3STaylor Simpson if len(imm) == 0: 531547a2d3STaylor Simpson # No extended operand found 541547a2d3STaylor Simpson return 551547a2d3STaylor Simpson letter = re.split("\\(", imm[0])[1] 561547a2d3STaylor Simpson f.write(code_fmt(f"""\ 571547a2d3STaylor Simpson insn->which_extended = {0 if letter.islower() else 1}; 581547a2d3STaylor Simpson """)) 591547a2d3STaylor Simpson 601547a2d3STaylor Simpson## 611547a2d3STaylor Simpson## Generate the QEMU decodetree trans_<tag> function for each instruction 621547a2d3STaylor Simpson## For A2_add: Rd32=add(Rs32,Rt32) 631547a2d3STaylor Simpson## We produce: 641547a2d3STaylor Simpson## static bool trans_A2_add(DisasContext *ctx, arg_A2_add *args) 651547a2d3STaylor Simpson## { 661547a2d3STaylor Simpson## Insn *insn = ctx->insn; 671547a2d3STaylor Simpson## insn->opcode = A2_add; 681547a2d3STaylor Simpson## insn->regno[0] = args->Rd; 691547a2d3STaylor Simpson## insn->regno[1] = args->Rs; 701547a2d3STaylor Simpson## insn->regno[2] = args->Rt; 711547a2d3STaylor Simpson## return true; 721547a2d3STaylor Simpson## } 731547a2d3STaylor Simpson## 74*f6c01009STaylor Simpsondef gen_trans_funcs(f): 751547a2d3STaylor Simpson f.write(f"/* DO NOT MODIFY - This file is generated by {sys.argv[0]} */\n\n") 761547a2d3STaylor Simpson for tag in sorted(encs.keys(), key=iset.tags.index): 771547a2d3STaylor Simpson regs = ordered_unique(regre.findall(iset.iset[tag]["syntax"])) 781547a2d3STaylor Simpson imms = ordered_unique(immre.findall(iset.iset[tag]["syntax"])) 791547a2d3STaylor Simpson 801547a2d3STaylor Simpson f.write(textwrap.dedent(f"""\ 811547a2d3STaylor Simpson static bool trans_{tag}(DisasContext *ctx, arg_{tag} *args) 821547a2d3STaylor Simpson {open_curly} 831547a2d3STaylor Simpson Insn *insn = ctx->insn; 841547a2d3STaylor Simpson insn->opcode = {tag}; 851547a2d3STaylor Simpson """)) 861547a2d3STaylor Simpson 871547a2d3STaylor Simpson regno = 0 881547a2d3STaylor Simpson for reg in regs: 891547a2d3STaylor Simpson reg_type = reg[0] 901547a2d3STaylor Simpson reg_id = reg[1] 911547a2d3STaylor Simpson f.write(code_fmt(f"""\ 921547a2d3STaylor Simpson insn->regno[{regno}] = args->{reg_type}{reg_id}; 931547a2d3STaylor Simpson """)) 941547a2d3STaylor Simpson regno += 1 951547a2d3STaylor Simpson 961547a2d3STaylor Simpson if len(imms) != 0: 971547a2d3STaylor Simpson mark_which_imm_extended(f, tag) 981547a2d3STaylor Simpson 991547a2d3STaylor Simpson for imm in imms: 1001547a2d3STaylor Simpson imm_type = imm[0] 1011547a2d3STaylor Simpson imm_letter = "i" if imm_type.islower() else "I" 1021547a2d3STaylor Simpson immno = 0 if imm_type.islower() else 1 1031547a2d3STaylor Simpson imm_shift = int(imm[2]) if imm[2] else 0 1041547a2d3STaylor Simpson if imm_shift: 1051547a2d3STaylor Simpson f.write(code_fmt(f"""\ 1061547a2d3STaylor Simpson insn->immed[{immno}] = 1071547a2d3STaylor Simpson shift_left(ctx, args->{imm_type}{imm_letter}, 1081547a2d3STaylor Simpson {imm_shift}, {immno}); 1091547a2d3STaylor Simpson """)) 1101547a2d3STaylor Simpson else: 1111547a2d3STaylor Simpson f.write(code_fmt(f"""\ 1121547a2d3STaylor Simpson insn->immed[{immno}] = args->{imm_type}{imm_letter}; 1131547a2d3STaylor Simpson """)) 1141547a2d3STaylor Simpson 1151547a2d3STaylor Simpson f.write(textwrap.dedent(f"""\ 1161547a2d3STaylor Simpson return true; 1171547a2d3STaylor Simpson {close_curly} 1181547a2d3STaylor Simpson """)) 1191547a2d3STaylor Simpson 1201547a2d3STaylor Simpson 1211547a2d3STaylor Simpsonif __name__ == "__main__": 1221547a2d3STaylor Simpson hex_common.read_semantics_file(sys.argv[1]) 1231547a2d3STaylor Simpson with open(sys.argv[2], "w") as f: 124*f6c01009STaylor Simpson gen_trans_funcs(f) 125