| 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 | ## Generate the code to analyze the instruction |
| 27 | ## For A2_add: Rd32=add(Rs32,Rt32), { RdV=RsV+RtV;} |
| 28 | ## We produce: |
| 29 | ## static void analyze_A2_add(DisasContext *ctx) |
| 30 | ## { |
| 31 | ## Insn *insn G_GNUC_UNUSED = ctx->insn; |
| 32 | ## const int RdN = insn->regno[0]; |
| 33 | ## ctx_log_reg_write(ctx, RdN, false); |
| 34 | ## const int RsN = insn->regno[1]; |
| 35 | ## ctx_log_reg_read(ctx, RsN); |
| 36 | ## const int RtN = insn->regno[2]; |
| 37 | ## ctx_log_reg_read(ctx, RtN); |
| 38 | ## } |
| 39 | ## |
| 40 | def gen_analyze_func(f, tag, regs, imms): |
| 41 | f.write(f"static void analyze_{tag}(DisasContext *ctx)\n") |
| 42 | f.write("{\n") |
| 43 | |
| 44 | if hex_common.tag_ignore(tag): |
| 45 | f.write("}\n\n") |
| 46 | return |
| 47 | |
| 48 | if hex_common.is_sysemu_tag(tag): |
| 49 | f.write("#ifndef CONFIG_USER_ONLY\n") |
| 50 | |
| 51 | f.write(" Insn *insn G_GNUC_UNUSED = ctx->insn;\n") |
| 52 | if (hex_common.is_hvx_insn(tag)): |
| 53 | if hex_common.has_hvx_helper(tag): |
| 54 | f.write( |
| 55 | " const bool G_GNUC_UNUSED insn_has_hvx_helper = true;\n" |
| 56 | ) |
| 57 | f.write(" ctx_start_hvx_insn(ctx);\n") |
| 58 | else: |
| 59 | f.write( |
| 60 | " const bool G_GNUC_UNUSED insn_has_hvx_helper = false;\n" |
| 61 | ) |
| 62 | |
| 63 | ## Declare all the registers |
| 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() or reg.is_written(): |
| 68 | reg.decl_reg_num(f, regno) |
| 69 | |
| 70 | ## Analyze the register reads |
| 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_read(): |
| 75 | reg.analyze_read(f, regno) |
| 76 | |
| 77 | f.write(" mark_implicit_reads(ctx);\n") |
| 78 | |
| 79 | ## Analyze the register writes |
| 80 | for regno, register in enumerate(regs): |
| 81 | reg_type, reg_id = register |
| 82 | reg = hex_common.get_register(tag, reg_type, reg_id) |
| 83 | if reg.is_written(): |
| 84 | reg.analyze_write(f, tag, regno) |
| 85 | |
| 86 | f.write(" mark_implicit_writes(ctx);\n") |
| 87 | |
| 88 | if hex_common.is_sysemu_tag(tag): |
| 89 | f.write("#endif /* !CONFIG_USER_ONLY */\n") |
| 90 | |
| 91 | f.write("}\n\n") |
| 92 | |
| 93 | |
| 94 | def main(): |
| 95 | args = hex_common.parse_common_args( |
| 96 | "Emit functions analyzing register accesses" |
| 97 | ) |
| 98 | tagregs = hex_common.get_tagregs() |
| 99 | tagimms = hex_common.get_tagimms() |
| 100 | |
| 101 | with open(args.out, "w") as f: |
| 102 | f.write("#ifndef HEXAGON_ANALYZE_FUNCS_C_INC\n") |
| 103 | f.write("#define HEXAGON_ANALYZE_FUNCS_C_INC\n\n") |
| 104 | |
| 105 | for tag in hex_common.tags: |
| 106 | gen_analyze_func(f, tag, tagregs[tag], tagimms[tag]) |
| 107 | |
| 108 | f.write("#endif /* HEXAGON_ANALYZE_FUNCS_C_INC */\n") |
| 109 | |
| 110 | |
| 111 | if __name__ == "__main__": |
| 112 | main() |