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;
7191963819STaylor Simpson##         insn->new_read_idx = -1;
72325a64afSTaylor Simpson##         insn->dest_idx = 0;
734614b8f3STaylor Simpson##         insn->has_pred_dest = false;
741547a2d3STaylor Simpson##         return true;
751547a2d3STaylor Simpson##     }
761547a2d3STaylor Simpson##
77f6c01009STaylor Simpsondef gen_trans_funcs(f):
781547a2d3STaylor Simpson    f.write(f"/* DO NOT MODIFY - This file is generated by {sys.argv[0]} */\n\n")
791547a2d3STaylor Simpson    for tag in sorted(encs.keys(), key=iset.tags.index):
801547a2d3STaylor Simpson        regs = ordered_unique(regre.findall(iset.iset[tag]["syntax"]))
811547a2d3STaylor Simpson        imms = ordered_unique(immre.findall(iset.iset[tag]["syntax"]))
821547a2d3STaylor Simpson
831547a2d3STaylor Simpson        f.write(textwrap.dedent(f"""\
841547a2d3STaylor Simpson            static bool trans_{tag}(DisasContext *ctx, arg_{tag} *args)
851547a2d3STaylor Simpson            {open_curly}
861547a2d3STaylor Simpson                Insn *insn = ctx->insn;
871547a2d3STaylor Simpson                insn->opcode = {tag};
881547a2d3STaylor Simpson        """))
891547a2d3STaylor Simpson
9091963819STaylor Simpson        new_read_idx = -1
91325a64afSTaylor Simpson        dest_idx = -1
92*a1852002SMatheus Tavares Bernardino        dest_idx_reg_id = None
934614b8f3STaylor Simpson        has_pred_dest = "false"
9491963819STaylor Simpson        for regno, (reg_type, reg_id, *_) in enumerate(regs):
9591963819STaylor Simpson            reg = hex_common.get_register(tag, reg_type, reg_id)
961547a2d3STaylor Simpson            f.write(code_fmt(f"""\
971547a2d3STaylor Simpson                insn->regno[{regno}] = args->{reg_type}{reg_id};
981547a2d3STaylor Simpson            """))
9991963819STaylor Simpson            if reg.is_read() and reg.is_new():
10091963819STaylor Simpson                new_read_idx = regno
101*a1852002SMatheus Tavares Bernardino            if reg.is_written():
102*a1852002SMatheus Tavares Bernardino                # dest_idx should be the first destination alphabetically
103*a1852002SMatheus Tavares Bernardino                if dest_idx_reg_id is None or reg_id < dest_idx_reg_id:
104325a64afSTaylor Simpson                    dest_idx = regno
105*a1852002SMatheus Tavares Bernardino                    dest_idx_reg_id = reg_id
1064614b8f3STaylor Simpson            if reg_type == "P" and reg.is_written() and not reg.is_read():
1074614b8f3STaylor Simpson                has_pred_dest = "true"
1081547a2d3STaylor Simpson
1091547a2d3STaylor Simpson        if len(imms) != 0:
1101547a2d3STaylor Simpson            mark_which_imm_extended(f, tag)
1111547a2d3STaylor Simpson
1121547a2d3STaylor Simpson        for imm in imms:
1131547a2d3STaylor Simpson            imm_type = imm[0]
1141547a2d3STaylor Simpson            imm_letter = "i" if imm_type.islower() else "I"
1151547a2d3STaylor Simpson            immno = 0 if imm_type.islower() else 1
1161547a2d3STaylor Simpson            imm_shift = int(imm[2]) if imm[2] else 0
1171547a2d3STaylor Simpson            if imm_shift:
1181547a2d3STaylor Simpson                f.write(code_fmt(f"""\
1191547a2d3STaylor Simpson                    insn->immed[{immno}] =
1201547a2d3STaylor Simpson                        shift_left(ctx, args->{imm_type}{imm_letter},
1211547a2d3STaylor Simpson                                   {imm_shift}, {immno});
1221547a2d3STaylor Simpson                """))
1231547a2d3STaylor Simpson            else:
1241547a2d3STaylor Simpson                f.write(code_fmt(f"""\
1251547a2d3STaylor Simpson                    insn->immed[{immno}] = args->{imm_type}{imm_letter};
1261547a2d3STaylor Simpson                """))
1271547a2d3STaylor Simpson
12891963819STaylor Simpson        f.write(code_fmt(f"""\
12991963819STaylor Simpson            insn->new_read_idx = {new_read_idx};
130325a64afSTaylor Simpson            insn->dest_idx = {dest_idx};
1314614b8f3STaylor Simpson            insn->has_pred_dest = {has_pred_dest};
13291963819STaylor Simpson        """))
1331547a2d3STaylor Simpson        f.write(textwrap.dedent(f"""\
1341547a2d3STaylor Simpson                return true;
1351547a2d3STaylor Simpson            {close_curly}
1361547a2d3STaylor Simpson        """))
1371547a2d3STaylor Simpson
1381547a2d3STaylor Simpson
1391547a2d3STaylor Simpsonif __name__ == "__main__":
1401547a2d3STaylor Simpson    hex_common.read_semantics_file(sys.argv[1])
14191963819STaylor Simpson    hex_common.init_registers()
1421547a2d3STaylor Simpson    with open(sys.argv[2], "w") as f:
143f6c01009STaylor Simpson        gen_trans_funcs(f)
144