1#!/usr/bin/env python3
2
3##
4##  Copyright(c) 2022-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
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 sys
21import re
22import string
23import hex_common
24
25
26##
27## Generate the code to analyze the instruction
28##     For A2_add: Rd32=add(Rs32,Rt32), { RdV=RsV+RtV;}
29##     We produce:
30##     static void analyze_A2_add(DisasContext *ctx)
31##     {
32##         Insn *insn G_GNUC_UNUSED = ctx->insn;
33##         const int RdN = insn->regno[0];
34##         ctx_log_reg_write(ctx, RdN, false);
35##         const int RsN = insn->regno[1];
36##         ctx_log_reg_read(ctx, RsN);
37##         const int RtN = insn->regno[2];
38##         ctx_log_reg_read(ctx, RtN);
39##     }
40##
41def gen_analyze_func(f, tag, regs, imms):
42    f.write(f"static void analyze_{tag}(DisasContext *ctx)\n")
43    f.write("{\n")
44
45    f.write("    Insn *insn G_GNUC_UNUSED = ctx->insn;\n")
46
47    i = 0
48    ## Analyze all the registers
49    for regtype, regid in regs:
50        reg = hex_common.get_register(tag, regtype, regid)
51        if reg.is_written():
52            reg.analyze_write(f, tag, i)
53        else:
54            reg.analyze_read(f, i)
55        i += 1
56
57    has_generated_helper = not hex_common.skip_qemu_helper(
58        tag
59    ) and not hex_common.is_idef_parser_enabled(tag)
60
61    ## Mark HVX instructions with generated helpers
62    if (has_generated_helper and
63        "A_CVI" in hex_common.attribdict[tag]):
64        f.write("    ctx->has_hvx_helper = true;\n")
65
66    f.write("}\n\n")
67
68
69def main():
70    hex_common.read_semantics_file(sys.argv[1])
71    hex_common.read_attribs_file(sys.argv[2])
72    hex_common.read_overrides_file(sys.argv[3])
73    hex_common.read_overrides_file(sys.argv[4])
74    ## Whether or not idef-parser is enabled is
75    ## determined by the number of arguments to
76    ## this script:
77    ##
78    ##   5 args. -> not enabled,
79    ##   6 args. -> idef-parser enabled.
80    ##
81    ## The 6:th arg. then holds a list of the successfully
82    ## parsed instructions.
83    is_idef_parser_enabled = len(sys.argv) > 6
84    if is_idef_parser_enabled:
85        hex_common.read_idef_parser_enabled_file(sys.argv[5])
86    hex_common.calculate_attribs()
87    hex_common.init_registers()
88    tagregs = hex_common.get_tagregs()
89    tagimms = hex_common.get_tagimms()
90
91    with open(sys.argv[-1], "w") as f:
92        f.write("#ifndef HEXAGON_TCG_FUNCS_H\n")
93        f.write("#define HEXAGON_TCG_FUNCS_H\n\n")
94
95        for tag in hex_common.tags:
96            gen_analyze_func(f, tag, tagregs[tag], tagimms[tag])
97
98        f.write("#endif    /* HEXAGON_TCG_FUNCS_H */\n")
99
100
101if __name__ == "__main__":
102    main()
103