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 47 ## Declare all the registers 48 for regno, register in enumerate(regs): 49 reg_type, reg_id = register 50 reg = hex_common.get_register(tag, reg_type, reg_id) 51 reg.decl_reg_num(f, regno) 52 53 ## Analyze the register reads 54 for regno, register in enumerate(regs): 55 reg_type, reg_id = register 56 reg = hex_common.get_register(tag, reg_type, reg_id) 57 if reg.is_read(): 58 reg.analyze_read(f, regno) 59 60 ## Analyze the register writes 61 for regno, register in enumerate(regs): 62 reg_type, reg_id = register 63 reg = hex_common.get_register(tag, reg_type, reg_id) 64 if reg.is_written(): 65 reg.analyze_write(f, tag, regno) 66 67 has_generated_helper = not hex_common.skip_qemu_helper( 68 tag 69 ) and not hex_common.is_idef_parser_enabled(tag) 70 71 ## Mark HVX instructions with generated helpers 72 if (has_generated_helper and 73 "A_CVI" in hex_common.attribdict[tag]): 74 f.write(" ctx->has_hvx_helper = true;\n") 75 76 f.write("}\n\n") 77 78 79def main(): 80 hex_common.read_semantics_file(sys.argv[1]) 81 hex_common.read_attribs_file(sys.argv[2]) 82 hex_common.read_overrides_file(sys.argv[3]) 83 hex_common.read_overrides_file(sys.argv[4]) 84 ## Whether or not idef-parser is enabled is 85 ## determined by the number of arguments to 86 ## this script: 87 ## 88 ## 5 args. -> not enabled, 89 ## 6 args. -> idef-parser enabled. 90 ## 91 ## The 6:th arg. then holds a list of the successfully 92 ## parsed instructions. 93 is_idef_parser_enabled = len(sys.argv) > 6 94 if is_idef_parser_enabled: 95 hex_common.read_idef_parser_enabled_file(sys.argv[5]) 96 hex_common.calculate_attribs() 97 hex_common.init_registers() 98 tagregs = hex_common.get_tagregs() 99 tagimms = hex_common.get_tagimms() 100 101 with open(sys.argv[-1], "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 111if __name__ == "__main__": 112 main() 113