Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | ## |
Taylor Simpson | 10849c2 | 2023-03-06 18:58:19 -0800 | [diff] [blame] | 4 | ## Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved. |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 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 | |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 25 | |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 26 | ## |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 27 | ## Generate the TCG code to call the helper |
| 28 | ## For A2_add: Rd32=add(Rs32,Rt32), { RdV=RsV+RtV;} |
| 29 | ## We produce: |
Taylor Simpson | 1e53633 | 2022-11-08 08:28:56 -0800 | [diff] [blame] | 30 | ## static void generate_A2_add(DisasContext *ctx) |
Taylor Simpson | e28b77a | 2023-03-06 18:58:26 -0800 | [diff] [blame] | 31 | ## { |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 32 | ## Insn *insn G_GNUC_UNUSED = ctx->insn; |
Taylor Simpson | e28b77a | 2023-03-06 18:58:26 -0800 | [diff] [blame] | 33 | ## const int RdN = insn->regno[0]; |
| 34 | ## TCGv RdV = get_result_gpr(ctx, RdN); |
| 35 | ## TCGv RsV = hex_gpr[insn->regno[1]]; |
| 36 | ## TCGv RtV = hex_gpr[insn->regno[2]]; |
| 37 | ## <GEN> |
Taylor Simpson | 07540a2 | 2023-04-27 15:59:53 -0700 | [diff] [blame] | 38 | ## gen_log_reg_write(ctx, RdN, RdV); |
Taylor Simpson | e28b77a | 2023-03-06 18:58:26 -0800 | [diff] [blame] | 39 | ## } |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 40 | ## |
| 41 | ## where <GEN> depends on hex_common.skip_qemu_helper(tag) |
| 42 | ## if hex_common.skip_qemu_helper(tag) is True |
| 43 | ## <GEN> is fGEN_TCG_A2_add({ RdV=RsV+RtV;}); |
| 44 | ## if hex_common.skip_qemu_helper(tag) is False |
Richard Henderson | ad75a51 | 2023-09-13 16:37:36 -0700 | [diff] [blame] | 45 | ## <GEN> is gen_helper_A2_add(RdV, tcg_env, RsV, RtV); |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 46 | ## |
| 47 | def gen_tcg_func(f, tag, regs, imms): |
Marco Liebel | cd6c4ed | 2023-03-20 02:25:32 -0700 | [diff] [blame] | 48 | f.write(f"static void generate_{tag}(DisasContext *ctx)\n") |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 49 | f.write("{\n") |
Taylor Simpson | 1e53633 | 2022-11-08 08:28:56 -0800 | [diff] [blame] | 50 | |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 51 | f.write(" Insn *insn G_GNUC_UNUSED = ctx->insn;\n") |
Taylor Simpson | 1e53633 | 2022-11-08 08:28:56 -0800 | [diff] [blame] | 52 | |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 53 | if hex_common.need_ea(tag): |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 54 | f.write(" TCGv EA G_GNUC_UNUSED = tcg_temp_new();\n") |
| 55 | |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 56 | ## Declare all the operands (regs and immediates) |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 57 | i = 0 |
Matheus Tavares Bernardino | 3608c24 | 2023-05-24 11:41:47 -0300 | [diff] [blame] | 58 | for regtype, regid in regs: |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 59 | reg = hex_common.get_register(tag, regtype, regid) |
| 60 | reg.decl_tcg(f, tag, i) |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 61 | i += 1 |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 62 | for immlett, bits, immshift in imms: |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 63 | i = 1 if immlett.isupper() else 0 |
| 64 | f.write(f" int {hex_common.imm_name(immlett)} = insn->immed[{i}];\n") |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 65 | |
Alessandro Di Federico | e71fdc4 | 2022-09-23 19:38:30 +0200 | [diff] [blame] | 66 | if hex_common.is_idef_parser_enabled(tag): |
| 67 | declared = [] |
| 68 | ## Handle registers |
Matheus Tavares Bernardino | 3608c24 | 2023-05-24 11:41:47 -0300 | [diff] [blame] | 69 | for regtype, regid in regs: |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 70 | reg = hex_common.get_register(tag, regtype, regid) |
| 71 | reg.idef_arg(declared) |
Alessandro Di Federico | e71fdc4 | 2022-09-23 19:38:30 +0200 | [diff] [blame] | 72 | ## Handle immediates |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 73 | for immlett, bits, immshift in imms: |
Alessandro Di Federico | e71fdc4 | 2022-09-23 19:38:30 +0200 | [diff] [blame] | 74 | declared.append(hex_common.imm_name(immlett)) |
| 75 | |
| 76 | arguments = ", ".join(["ctx", "ctx->insn", "ctx->pkt"] + declared) |
Marco Liebel | cd6c4ed | 2023-03-20 02:25:32 -0700 | [diff] [blame] | 77 | f.write(f" emit_{tag}({arguments});\n") |
Alessandro Di Federico | e71fdc4 | 2022-09-23 19:38:30 +0200 | [diff] [blame] | 78 | |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 79 | elif hex_common.skip_qemu_helper(tag): |
Marco Liebel | cd6c4ed | 2023-03-20 02:25:32 -0700 | [diff] [blame] | 80 | f.write(f" fGEN_TCG_{tag}({hex_common.semdict[tag]});\n") |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 81 | else: |
| 82 | ## Generate the call to the helper |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 83 | declared = [] |
| 84 | ret_type = hex_common.helper_ret_type(tag, regs).call_arg |
| 85 | if ret_type != "void": |
| 86 | declared.append(ret_type) |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 87 | |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 88 | for arg in hex_common.helper_args(tag, regs, imms): |
| 89 | declared.append(arg.call_arg) |
| 90 | |
| 91 | arguments = ", ".join(declared) |
| 92 | f.write(f" gen_helper_{tag}({arguments});\n") |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 93 | |
| 94 | ## Write all the outputs |
Matheus Tavares Bernardino | 3608c24 | 2023-05-24 11:41:47 -0300 | [diff] [blame] | 95 | for regtype, regid in regs: |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 96 | reg = hex_common.get_register(tag, regtype, regid) |
| 97 | if reg.is_written(): |
| 98 | reg.log_write(f, tag) |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 99 | |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 100 | f.write("}\n\n") |
| 101 | |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 102 | |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 103 | def gen_def_tcg_func(f, tag, tagregs, tagimms): |
| 104 | regs = tagregs[tag] |
| 105 | imms = tagimms[tag] |
| 106 | |
| 107 | gen_tcg_func(f, tag, regs, imms) |
| 108 | |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 109 | |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 110 | def main(): |
| 111 | hex_common.read_semantics_file(sys.argv[1]) |
| 112 | hex_common.read_attribs_file(sys.argv[2]) |
| 113 | hex_common.read_overrides_file(sys.argv[3]) |
Taylor Simpson | d51bcab | 2021-05-18 12:01:09 -0500 | [diff] [blame] | 114 | hex_common.read_overrides_file(sys.argv[4]) |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 115 | hex_common.calculate_attribs() |
Taylor Simpson | b447807 | 2023-12-10 15:07:05 -0700 | [diff] [blame] | 116 | hex_common.init_registers() |
Alessandro Di Federico | e71fdc4 | 2022-09-23 19:38:30 +0200 | [diff] [blame] | 117 | ## Whether or not idef-parser is enabled is |
| 118 | ## determined by the number of arguments to |
| 119 | ## this script: |
| 120 | ## |
| 121 | ## 5 args. -> not enabled, |
| 122 | ## 6 args. -> idef-parser enabled. |
| 123 | ## |
| 124 | ## The 6:th arg. then holds a list of the successfully |
| 125 | ## parsed instructions. |
| 126 | is_idef_parser_enabled = len(sys.argv) > 6 |
| 127 | if is_idef_parser_enabled: |
| 128 | hex_common.read_idef_parser_enabled_file(sys.argv[5]) |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 129 | tagregs = hex_common.get_tagregs() |
| 130 | tagimms = hex_common.get_tagimms() |
| 131 | |
Alessandro Di Federico | e71fdc4 | 2022-09-23 19:38:30 +0200 | [diff] [blame] | 132 | output_file = sys.argv[-1] |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 133 | with open(output_file, "w") as f: |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 134 | f.write("#ifndef HEXAGON_TCG_FUNCS_H\n") |
| 135 | f.write("#define HEXAGON_TCG_FUNCS_H\n\n") |
Alessandro Di Federico | e71fdc4 | 2022-09-23 19:38:30 +0200 | [diff] [blame] | 136 | if is_idef_parser_enabled: |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 137 | f.write('#include "idef-generated-emitter.h.inc"\n\n') |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 138 | |
| 139 | for tag in hex_common.tags: |
| 140 | ## Skip the priv instructions |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 141 | if "A_PRIV" in hex_common.attribdict[tag]: |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 142 | continue |
| 143 | ## Skip the guest instructions |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 144 | if "A_GUEST" in hex_common.attribdict[tag]: |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 145 | continue |
| 146 | ## Skip the diag instructions |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 147 | if tag == "Y6_diag": |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 148 | continue |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 149 | if tag == "Y6_diag0": |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 150 | continue |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 151 | if tag == "Y6_diag1": |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 152 | continue |
| 153 | |
| 154 | gen_def_tcg_func(f, tag, tagregs, tagimms) |
| 155 | |
| 156 | f.write("#endif /* HEXAGON_TCG_FUNCS_H */\n") |
| 157 | |
Marco Liebel | 5bb322e | 2023-03-20 02:25:33 -0700 | [diff] [blame] | 158 | |
Taylor Simpson | 793958c | 2021-02-07 23:46:10 -0600 | [diff] [blame] | 159 | if __name__ == "__main__": |
| 160 | main() |