1#!/usr/bin/env python3
2
3##
4##  Copyright(c) 2022-2024 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    if (hex_common.is_hvx_insn(tag)):
47        if hex_common.has_hvx_helper(tag):
48            f.write(
49                "    const bool G_GNUC_UNUSED insn_has_hvx_helper = true;\n"
50            )
51            f.write("    ctx_start_hvx_insn(ctx);\n")
52        else:
53            f.write(
54                "    const bool G_GNUC_UNUSED insn_has_hvx_helper = false;\n"
55            )
56
57    ## Declare all the registers
58    for regno, register in enumerate(regs):
59        reg_type, reg_id = register
60        reg = hex_common.get_register(tag, reg_type, reg_id)
61        reg.decl_reg_num(f, regno)
62
63    ## Analyze the register reads
64    for regno, register in enumerate(regs):
65        reg_type, reg_id = register
66        reg = hex_common.get_register(tag, reg_type, reg_id)
67        if reg.is_read():
68            reg.analyze_read(f, regno)
69
70    ## Analyze the register writes
71    for regno, register in enumerate(regs):
72        reg_type, reg_id = register
73        reg = hex_common.get_register(tag, reg_type, reg_id)
74        if reg.is_written():
75            reg.analyze_write(f, tag, regno)
76
77    f.write("}\n\n")
78
79
80def main():
81    hex_common.read_common_files()
82    tagregs = hex_common.get_tagregs()
83    tagimms = hex_common.get_tagimms()
84
85    with open(sys.argv[-1], "w") as f:
86        f.write("#ifndef HEXAGON_ANALYZE_FUNCS_C_INC\n")
87        f.write("#define HEXAGON_ANALYZE_FUNCS_C_INC\n\n")
88
89        for tag in hex_common.tags:
90            gen_analyze_func(f, tag, tagregs[tag], tagimms[tag])
91
92        f.write("#endif    /* HEXAGON_ANALYZE_FUNCS_C_INC */\n")
93
94
95if __name__ == "__main__":
96    main()
97