xref: /qemu/target/hexagon/gen_analyze_funcs.py (revision d3176a9f387f8b6b56882045d36f5b3f82565d90)
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 
20 import sys
21 import re
22 import string
23 import 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 ##
41 def 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 
80 def main():
81     args = hex_common.parse_common_args(
82         "Emit functions analyzing register accesses"
83     )
84     tagregs = hex_common.get_tagregs()
85     tagimms = hex_common.get_tagimms()
86 
87     with open(args.out, "w") as f:
88         f.write("#ifndef HEXAGON_ANALYZE_FUNCS_C_INC\n")
89         f.write("#define HEXAGON_ANALYZE_FUNCS_C_INC\n\n")
90 
91         for tag in hex_common.tags:
92             gen_analyze_func(f, tag, tagregs[tag], tagimms[tag])
93 
94         f.write("#endif    /* HEXAGON_ANALYZE_FUNCS_C_INC */\n")
95 
96 
97 if __name__ == "__main__":
98     main()
99