xref: /openbmc/qemu/target/hexagon/gen_helper_funcs.py (revision 28188253dc26bc3038f30eed0d79798b51f81a94)
1793958c9STaylor Simpson#!/usr/bin/env python3
2793958c9STaylor Simpson
3793958c9STaylor Simpson##
4*a4696661STaylor Simpson##  Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
5793958c9STaylor Simpson##
6793958c9STaylor Simpson##  This program is free software; you can redistribute it and/or modify
7793958c9STaylor Simpson##  it under the terms of the GNU General Public License as published by
8793958c9STaylor Simpson##  the Free Software Foundation; either version 2 of the License, or
9793958c9STaylor Simpson##  (at your option) any later version.
10793958c9STaylor Simpson##
11793958c9STaylor Simpson##  This program is distributed in the hope that it will be useful,
12793958c9STaylor Simpson##  but WITHOUT ANY WARRANTY; without even the implied warranty of
13793958c9STaylor Simpson##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14793958c9STaylor Simpson##  GNU General Public License for more details.
15793958c9STaylor Simpson##
16793958c9STaylor Simpson##  You should have received a copy of the GNU General Public License
17793958c9STaylor Simpson##  along with this program; if not, see <http://www.gnu.org/licenses/>.
18793958c9STaylor Simpson##
19793958c9STaylor Simpson
20793958c9STaylor Simpsonimport sys
21793958c9STaylor Simpsonimport re
22793958c9STaylor Simpsonimport string
23793958c9STaylor Simpsonimport hex_common
24793958c9STaylor Simpson
255bb322e2SMarco Liebel
26793958c9STaylor Simpson##
27793958c9STaylor Simpson## Generate the TCG code to call the helper
28793958c9STaylor Simpson##     For A2_add: Rd32=add(Rs32,Rt32), { RdV=RsV+RtV;}
29793958c9STaylor Simpson##     We produce:
30793958c9STaylor Simpson##       int32_t HELPER(A2_add)(CPUHexagonState *env, int32_t RsV, int32_t RtV)
31793958c9STaylor Simpson##       {
32793958c9STaylor Simpson##           int32_t RdV = 0;
33793958c9STaylor Simpson##           { RdV=RsV+RtV;}
34793958c9STaylor Simpson##           return RdV;
35793958c9STaylor Simpson##       }
36793958c9STaylor Simpson##
37793958c9STaylor Simpsondef gen_helper_function(f, tag, tagregs, tagimms):
38793958c9STaylor Simpson    regs = tagregs[tag]
39793958c9STaylor Simpson    imms = tagimms[tag]
40793958c9STaylor Simpson
41a3295f54STaylor Simpson    ret_type = hex_common.helper_ret_type(tag, regs).func_arg
42793958c9STaylor Simpson
43a3295f54STaylor Simpson    declared = []
44a3295f54STaylor Simpson    for arg in hex_common.helper_args(tag, regs, imms):
45a3295f54STaylor Simpson        declared.append(arg.func_arg)
46793958c9STaylor Simpson
47a3295f54STaylor Simpson    arguments = ", ".join(declared)
48a3295f54STaylor Simpson    f.write(f"{ret_type} HELPER({tag})({arguments})\n")
49a3295f54STaylor Simpson    f.write("{\n")
505bb322e2SMarco Liebel    if hex_common.need_ea(tag):
51a3295f54STaylor Simpson        f.write(hex_common.code_fmt(f"""\
52a3295f54STaylor Simpson            uint32_t EA;
53a3295f54STaylor Simpson        """))
54793958c9STaylor Simpson    ## Declare the return variable
55a3295f54STaylor Simpson    if not hex_common.is_predicated(tag):
563608c241SMatheus Tavares Bernardino        for regtype, regid in regs:
57a3295f54STaylor Simpson            reg = hex_common.get_register(tag, regtype, regid)
58a3295f54STaylor Simpson            if reg.is_writeonly() and not reg.is_hvx_reg():
59a3295f54STaylor Simpson                f.write(hex_common.code_fmt(f"""\
60a3295f54STaylor Simpson                    {reg.helper_arg_type()} {reg.helper_arg_name()} = 0;
61a3295f54STaylor Simpson                """))
62793958c9STaylor Simpson
63a3295f54STaylor Simpson    ## Print useful information about HVX registers
643608c241SMatheus Tavares Bernardino    for regtype, regid in regs:
65a3295f54STaylor Simpson        reg = hex_common.get_register(tag, regtype, regid)
66a3295f54STaylor Simpson        if reg.is_hvx_reg():
67a3295f54STaylor Simpson            reg.helper_hvx_desc(f)
68ccd9eec8STaylor Simpson
69e5d0d78dSTaylor Simpson    if hex_common.need_slot(tag):
70e5d0d78dSTaylor Simpson        if "A_LOAD" in hex_common.attribdict[tag]:
71a3295f54STaylor Simpson            f.write(hex_common.code_fmt(f"""\
72a3295f54STaylor Simpson                bool pkt_has_store_s1 = slotval & 0x1;
73a3295f54STaylor Simpson            """))
74a3295f54STaylor Simpson        f.write(hex_common.code_fmt(f"""\
75a3295f54STaylor Simpson            uint32_t slot = slotval >> 1;
76a3295f54STaylor Simpson        """))
77e5d0d78dSTaylor Simpson
785bb322e2SMarco Liebel    if "A_FPOP" in hex_common.attribdict[tag]:
79a3295f54STaylor Simpson        f.write(hex_common.code_fmt(f"""\
80a3295f54STaylor Simpson            arch_fpop_start(env);
81a3295f54STaylor Simpson        """))
82793958c9STaylor Simpson
83a3295f54STaylor Simpson    f.write(hex_common.code_fmt(f"""\
84a3295f54STaylor Simpson        {hex_common.semdict[tag]}
85a3295f54STaylor Simpson    """))
86793958c9STaylor Simpson
875bb322e2SMarco Liebel    if "A_FPOP" in hex_common.attribdict[tag]:
88a3295f54STaylor Simpson        f.write(hex_common.code_fmt(f"""\
89a3295f54STaylor Simpson            arch_fpop_end(env);
90a3295f54STaylor Simpson        """))
91793958c9STaylor Simpson
92a3295f54STaylor Simpson    ## Return the scalar result
933608c241SMatheus Tavares Bernardino    for regtype, regid in regs:
94a3295f54STaylor Simpson        reg = hex_common.get_register(tag, regtype, regid)
95a3295f54STaylor Simpson        if reg.is_written() and not reg.is_hvx_reg():
96a3295f54STaylor Simpson            f.write(hex_common.code_fmt(f"""\
97a3295f54STaylor Simpson                return {reg.helper_arg_name()};
98a3295f54STaylor Simpson            """))
99a3295f54STaylor Simpson
100793958c9STaylor Simpson    f.write("}\n\n")
101793958c9STaylor Simpson    ## End of the helper definition
102793958c9STaylor Simpson
1035bb322e2SMarco Liebel
104793958c9STaylor Simpsondef main():
105*a4696661STaylor Simpson    hex_common.read_common_files()
106793958c9STaylor Simpson    tagregs = hex_common.get_tagregs()
107793958c9STaylor Simpson    tagimms = hex_common.get_tagimms()
108793958c9STaylor Simpson
109e71fdc4fSAlessandro Di Federico    output_file = sys.argv[-1]
1105bb322e2SMarco Liebel    with open(output_file, "w") as f:
111793958c9STaylor Simpson        for tag in hex_common.tags:
112793958c9STaylor Simpson            ## Skip the priv instructions
1135bb322e2SMarco Liebel            if "A_PRIV" in hex_common.attribdict[tag]:
114793958c9STaylor Simpson                continue
115793958c9STaylor Simpson            ## Skip the guest instructions
1165bb322e2SMarco Liebel            if "A_GUEST" in hex_common.attribdict[tag]:
117793958c9STaylor Simpson                continue
118793958c9STaylor Simpson            ## Skip the diag instructions
1195bb322e2SMarco Liebel            if tag == "Y6_diag":
120793958c9STaylor Simpson                continue
1215bb322e2SMarco Liebel            if tag == "Y6_diag0":
122793958c9STaylor Simpson                continue
1235bb322e2SMarco Liebel            if tag == "Y6_diag1":
124793958c9STaylor Simpson                continue
1255bb322e2SMarco Liebel            if hex_common.skip_qemu_helper(tag):
126793958c9STaylor Simpson                continue
1275bb322e2SMarco Liebel            if hex_common.is_idef_parser_enabled(tag):
128e71fdc4fSAlessandro Di Federico                continue
129793958c9STaylor Simpson
130793958c9STaylor Simpson            gen_helper_function(f, tag, tagregs, tagimms)
131793958c9STaylor Simpson
1325bb322e2SMarco Liebel
133793958c9STaylor Simpsonif __name__ == "__main__":
134793958c9STaylor Simpson    main()
135