xref: /qemu/target/hexagon/gen_idef_parser_funcs.py (revision 7fa7aa8114dc5f97f28446a9fe98182d4968be63)
17c19dcc5SAlessandro Di Federico#!/usr/bin/env python3
27c19dcc5SAlessandro Di Federico
37c19dcc5SAlessandro Di Federico##
4a4696661STaylor Simpson##  Copyright(c) 2019-2024 rev.ng Labs Srl. All Rights Reserved.
57c19dcc5SAlessandro Di Federico##
67c19dcc5SAlessandro Di Federico##  This program is free software; you can redistribute it and/or modify
77c19dcc5SAlessandro Di Federico##  it under the terms of the GNU General Public License as published by
87c19dcc5SAlessandro Di Federico##  the Free Software Foundation; either version 2 of the License, or
97c19dcc5SAlessandro Di Federico##  (at your option) any later version.
107c19dcc5SAlessandro Di Federico##
117c19dcc5SAlessandro Di Federico##  This program is distributed in the hope that it will be useful,
127c19dcc5SAlessandro Di Federico##  but WITHOUT ANY WARRANTY; without even the implied warranty of
137c19dcc5SAlessandro Di Federico##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
147c19dcc5SAlessandro Di Federico##  GNU General Public License for more details.
157c19dcc5SAlessandro Di Federico##
167c19dcc5SAlessandro Di Federico##  You should have received a copy of the GNU General Public License
177c19dcc5SAlessandro Di Federico##  along with this program; if not, see <http://www.gnu.org/licenses/>.
187c19dcc5SAlessandro Di Federico##
197c19dcc5SAlessandro Di Federico
207c19dcc5SAlessandro Di Federicoimport sys
217c19dcc5SAlessandro Di Federicoimport re
227c19dcc5SAlessandro Di Federicoimport string
23*e2957967SAnton Johanssonimport argparse
247c19dcc5SAlessandro Di Federicofrom io import StringIO
257c19dcc5SAlessandro Di Federico
267c19dcc5SAlessandro Di Federicoimport hex_common
277c19dcc5SAlessandro Di Federico
285bb322e2SMarco Liebel
297c19dcc5SAlessandro Di Federico##
307c19dcc5SAlessandro Di Federico## Generate code to be fed to the idef_parser
317c19dcc5SAlessandro Di Federico##
327c19dcc5SAlessandro Di Federico## Consider A2_add:
337c19dcc5SAlessandro Di Federico##
347c19dcc5SAlessandro Di Federico##     Rd32=add(Rs32,Rt32), { RdV=RsV+RtV;}
357c19dcc5SAlessandro Di Federico##
367c19dcc5SAlessandro Di Federico## We produce:
377c19dcc5SAlessandro Di Federico##
387c19dcc5SAlessandro Di Federico##     A2_add(RdV, in RsV, in RtV) {
397c19dcc5SAlessandro Di Federico##       { RdV=RsV+RtV;}
407c19dcc5SAlessandro Di Federico##     }
417c19dcc5SAlessandro Di Federico##
427c19dcc5SAlessandro Di Federico## A2_add represents the instruction tag. Then we have a list of TCGv
437c19dcc5SAlessandro Di Federico## that the code generated by the parser can expect in input. Some of
447c19dcc5SAlessandro Di Federico## them are inputs ("in" prefix), while some others are outputs.
457c19dcc5SAlessandro Di Federico##
467c19dcc5SAlessandro Di Federicodef main():
47*e2957967SAnton Johansson    parser = argparse.ArgumentParser(
48*e2957967SAnton Johansson        "Emit instruction implementations that can be fed to idef-parser"
49*e2957967SAnton Johansson    )
50*e2957967SAnton Johansson    parser.add_argument("semantics", help="semantics file")
51*e2957967SAnton Johansson    parser.add_argument("out", help="output file")
52*e2957967SAnton Johansson    args = parser.parse_args()
53*e2957967SAnton Johansson    hex_common.read_semantics_file(args.semantics)
547c19dcc5SAlessandro Di Federico    hex_common.calculate_attribs()
551f03e9a9STaylor Simpson    hex_common.init_registers()
567c19dcc5SAlessandro Di Federico    tagregs = hex_common.get_tagregs()
577c19dcc5SAlessandro Di Federico    tagimms = hex_common.get_tagimms()
587c19dcc5SAlessandro Di Federico
59*e2957967SAnton Johansson    with open(args.out, "w") as f:
60b8daa5fcSPhilippe Mathieu-Daudé        f.write('#include "macros.h.inc"\n\n')
617c19dcc5SAlessandro Di Federico
627c19dcc5SAlessandro Di Federico        for tag in hex_common.tags:
637c19dcc5SAlessandro Di Federico            ## Skip the priv instructions
645bb322e2SMarco Liebel            if "A_PRIV" in hex_common.attribdict[tag]:
657c19dcc5SAlessandro Di Federico                continue
667c19dcc5SAlessandro Di Federico            ## Skip the guest instructions
675bb322e2SMarco Liebel            if "A_GUEST" in hex_common.attribdict[tag]:
687c19dcc5SAlessandro Di Federico                continue
697c19dcc5SAlessandro Di Federico            ## Skip instructions that saturate in a ternary expression
705bb322e2SMarco Liebel            if tag in {"S2_asr_r_r_sat", "S2_asl_r_r_sat"}:
717c19dcc5SAlessandro Di Federico                continue
727c19dcc5SAlessandro Di Federico            ## Skip instructions using switch
735bb322e2SMarco Liebel            if tag in {"S4_vrcrotate_acc", "S4_vrcrotate"}:
747c19dcc5SAlessandro Di Federico                continue
757c19dcc5SAlessandro Di Federico            ## Skip trap instructions
765bb322e2SMarco Liebel            if tag in {"J2_trap0", "J2_trap1"}:
777c19dcc5SAlessandro Di Federico                continue
787c19dcc5SAlessandro Di Federico            ## Skip 128-bit instructions
795bb322e2SMarco Liebel            if tag in {"A7_croundd_ri", "A7_croundd_rr"}:
807c19dcc5SAlessandro Di Federico                continue
815bb322e2SMarco Liebel            if tag in {
825bb322e2SMarco Liebel                "M7_wcmpyrw",
835bb322e2SMarco Liebel                "M7_wcmpyrwc",
845bb322e2SMarco Liebel                "M7_wcmpyiw",
855bb322e2SMarco Liebel                "M7_wcmpyiwc",
865bb322e2SMarco Liebel                "M7_wcmpyrw_rnd",
875bb322e2SMarco Liebel                "M7_wcmpyrwc_rnd",
885bb322e2SMarco Liebel                "M7_wcmpyiw_rnd",
895bb322e2SMarco Liebel                "M7_wcmpyiwc_rnd",
905bb322e2SMarco Liebel            }:
917c19dcc5SAlessandro Di Federico                continue
927c19dcc5SAlessandro Di Federico            ## Skip interleave/deinterleave instructions
935bb322e2SMarco Liebel            if tag in {"S2_interleave", "S2_deinterleave"}:
947c19dcc5SAlessandro Di Federico                continue
957c19dcc5SAlessandro Di Federico            ## Skip instructions using bit reverse
965bb322e2SMarco Liebel            if tag in {
975bb322e2SMarco Liebel                "S2_brev",
985bb322e2SMarco Liebel                "S2_brevp",
995bb322e2SMarco Liebel                "S2_ct0",
1005bb322e2SMarco Liebel                "S2_ct1",
1015bb322e2SMarco Liebel                "S2_ct0p",
1025bb322e2SMarco Liebel                "S2_ct1p",
1035bb322e2SMarco Liebel                "A4_tlbmatch",
1045bb322e2SMarco Liebel            }:
1057c19dcc5SAlessandro Di Federico                continue
1067c19dcc5SAlessandro Di Federico            ## Skip other unsupported instructions
1075bb322e2SMarco Liebel            if tag == "S2_cabacdecbin" or tag == "A5_ACS":
1087c19dcc5SAlessandro Di Federico                continue
1095bb322e2SMarco Liebel            if tag.startswith("Y"):
1107c19dcc5SAlessandro Di Federico                continue
1115bb322e2SMarco Liebel            if tag.startswith("V6_"):
1127c19dcc5SAlessandro Di Federico                continue
113163e5fa3STaylor Simpson            if ( tag.startswith("F") and
114163e5fa3STaylor Simpson                 tag not in {
115163e5fa3STaylor Simpson                     "F2_sfimm_p",
116163e5fa3STaylor Simpson                     "F2_sfimm_n",
117163e5fa3STaylor Simpson                     "F2_dfimm_p",
118163e5fa3STaylor Simpson                     "F2_dfimm_n",
119163e5fa3STaylor Simpson                     "F2_dfmpyll",
120163e5fa3STaylor Simpson                     "F2_dfmpylh"
121163e5fa3STaylor Simpson                 }):
1227c19dcc5SAlessandro Di Federico                continue
1235bb322e2SMarco Liebel            if tag.endswith("_locked"):
1247c19dcc5SAlessandro Di Federico                continue
1255bb322e2SMarco Liebel            if "A_COF" in hex_common.attribdict[tag]:
1267c19dcc5SAlessandro Di Federico                continue
127406c74f2STaylor Simpson            if ( tag.startswith('R6_release_') ):
128406c74f2STaylor Simpson                continue
129d54c5615STaylor Simpson            ## Skip instructions that are incompatible with short-circuit
130d54c5615STaylor Simpson            ## packet register writes
131d54c5615STaylor Simpson            if ( tag == 'S2_insert' or
132d54c5615STaylor Simpson                 tag == 'S2_insert_rp' or
133d54c5615STaylor Simpson                 tag == 'S2_asr_r_svw_trun' or
134d54c5615STaylor Simpson                 tag == 'A2_swiz' ):
135d54c5615STaylor Simpson                continue
1367c19dcc5SAlessandro Di Federico
1377c19dcc5SAlessandro Di Federico            regs = tagregs[tag]
1387c19dcc5SAlessandro Di Federico            imms = tagimms[tag]
1397c19dcc5SAlessandro Di Federico
1407c19dcc5SAlessandro Di Federico            arguments = []
1413608c241SMatheus Tavares Bernardino            for regtype, regid in regs:
1421f03e9a9STaylor Simpson                reg = hex_common.get_register(tag, regtype, regid)
1431f03e9a9STaylor Simpson                prefix = "in " if reg.is_read() else ""
1441f03e9a9STaylor Simpson                arguments.append(f"{prefix}{reg.reg_tcg()}")
1457c19dcc5SAlessandro Di Federico
1467c19dcc5SAlessandro Di Federico            for immlett, bits, immshift in imms:
1477c19dcc5SAlessandro Di Federico                arguments.append(hex_common.imm_name(immlett))
1487c19dcc5SAlessandro Di Federico
149cd6c4edfSMarco Liebel            f.write(f"{tag}({', '.join(arguments)}) {{\n")
1505bb322e2SMarco Liebel            f.write("    ")
1517c19dcc5SAlessandro Di Federico            if hex_common.need_ea(tag):
1525bb322e2SMarco Liebel                f.write("size4u_t EA; ")
153cd6c4edfSMarco Liebel            f.write(f"{hex_common.semdict[tag]}\n")
1547c19dcc5SAlessandro Di Federico            f.write("}\n\n")
1557c19dcc5SAlessandro Di Federico
1565bb322e2SMarco Liebel
1577c19dcc5SAlessandro Di Federicoif __name__ == "__main__":
1587c19dcc5SAlessandro Di Federico    main()
159