blob: 3d8e3cb6a2646e14f2b16e1015e6b52eac94bb0c [file] [log] [blame]
Taylor Simpson793958c2021-02-07 23:46:10 -06001#!/usr/bin/env python3
2
3##
Taylor Simpson10849c22023-03-06 18:58:19 -08004## Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
Taylor Simpson793958c2021-02-07 23:46:10 -06005##
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
Marco Liebel5bb322e2023-03-20 02:25:33 -070025
Taylor Simpson793958c2021-02-07 23:46:10 -060026##
Taylor Simpson793958c2021-02-07 23:46:10 -060027## Generate the TCG code to call the helper
28## For A2_add: Rd32=add(Rs32,Rt32), { RdV=RsV+RtV;}
29## We produce:
Taylor Simpson1e536332022-11-08 08:28:56 -080030## static void generate_A2_add(DisasContext *ctx)
Taylor Simpsone28b77a2023-03-06 18:58:26 -080031## {
Taylor Simpsonb4478072023-12-10 15:07:05 -070032## Insn *insn G_GNUC_UNUSED = ctx->insn;
Taylor Simpsone28b77a2023-03-06 18:58:26 -080033## 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 Simpson07540a22023-04-27 15:59:53 -070038## gen_log_reg_write(ctx, RdN, RdV);
Taylor Simpsone28b77a2023-03-06 18:58:26 -080039## }
Taylor Simpson793958c2021-02-07 23:46:10 -060040##
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 Hendersonad75a512023-09-13 16:37:36 -070045## <GEN> is gen_helper_A2_add(RdV, tcg_env, RsV, RtV);
Taylor Simpson793958c2021-02-07 23:46:10 -060046##
47def gen_tcg_func(f, tag, regs, imms):
Marco Liebelcd6c4ed2023-03-20 02:25:32 -070048 f.write(f"static void generate_{tag}(DisasContext *ctx)\n")
Marco Liebel5bb322e2023-03-20 02:25:33 -070049 f.write("{\n")
Taylor Simpson1e536332022-11-08 08:28:56 -080050
Taylor Simpsonb4478072023-12-10 15:07:05 -070051 f.write(" Insn *insn G_GNUC_UNUSED = ctx->insn;\n")
Taylor Simpson1e536332022-11-08 08:28:56 -080052
Marco Liebel5bb322e2023-03-20 02:25:33 -070053 if hex_common.need_ea(tag):
Taylor Simpsonb4478072023-12-10 15:07:05 -070054 f.write(" TCGv EA G_GNUC_UNUSED = tcg_temp_new();\n")
55
Taylor Simpson793958c2021-02-07 23:46:10 -060056 ## Declare all the operands (regs and immediates)
Taylor Simpsonb4478072023-12-10 15:07:05 -070057 i = 0
Matheus Tavares Bernardino3608c242023-05-24 11:41:47 -030058 for regtype, regid in regs:
Taylor Simpsonb4478072023-12-10 15:07:05 -070059 reg = hex_common.get_register(tag, regtype, regid)
60 reg.decl_tcg(f, tag, i)
Taylor Simpson793958c2021-02-07 23:46:10 -060061 i += 1
Marco Liebel5bb322e2023-03-20 02:25:33 -070062 for immlett, bits, immshift in imms:
Taylor Simpsonb4478072023-12-10 15:07:05 -070063 i = 1 if immlett.isupper() else 0
64 f.write(f" int {hex_common.imm_name(immlett)} = insn->immed[{i}];\n")
Taylor Simpson793958c2021-02-07 23:46:10 -060065
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +020066 if hex_common.is_idef_parser_enabled(tag):
67 declared = []
68 ## Handle registers
Matheus Tavares Bernardino3608c242023-05-24 11:41:47 -030069 for regtype, regid in regs:
Taylor Simpsonb4478072023-12-10 15:07:05 -070070 reg = hex_common.get_register(tag, regtype, regid)
71 reg.idef_arg(declared)
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +020072 ## Handle immediates
Marco Liebel5bb322e2023-03-20 02:25:33 -070073 for immlett, bits, immshift in imms:
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +020074 declared.append(hex_common.imm_name(immlett))
75
76 arguments = ", ".join(["ctx", "ctx->insn", "ctx->pkt"] + declared)
Marco Liebelcd6c4ed2023-03-20 02:25:32 -070077 f.write(f" emit_{tag}({arguments});\n")
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +020078
Marco Liebel5bb322e2023-03-20 02:25:33 -070079 elif hex_common.skip_qemu_helper(tag):
Marco Liebelcd6c4ed2023-03-20 02:25:32 -070080 f.write(f" fGEN_TCG_{tag}({hex_common.semdict[tag]});\n")
Taylor Simpson793958c2021-02-07 23:46:10 -060081 else:
82 ## Generate the call to the helper
Taylor Simpsonb4478072023-12-10 15:07:05 -070083 declared = []
84 ret_type = hex_common.helper_ret_type(tag, regs).call_arg
85 if ret_type != "void":
86 declared.append(ret_type)
Taylor Simpson793958c2021-02-07 23:46:10 -060087
Taylor Simpsonb4478072023-12-10 15:07:05 -070088 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 Simpson793958c2021-02-07 23:46:10 -060093
94 ## Write all the outputs
Matheus Tavares Bernardino3608c242023-05-24 11:41:47 -030095 for regtype, regid in regs:
Taylor Simpsonb4478072023-12-10 15:07:05 -070096 reg = hex_common.get_register(tag, regtype, regid)
97 if reg.is_written():
98 reg.log_write(f, tag)
Taylor Simpson793958c2021-02-07 23:46:10 -060099
Taylor Simpson793958c2021-02-07 23:46:10 -0600100 f.write("}\n\n")
101
Marco Liebel5bb322e2023-03-20 02:25:33 -0700102
Taylor Simpson793958c2021-02-07 23:46:10 -0600103def 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 Liebel5bb322e2023-03-20 02:25:33 -0700109
Taylor Simpson793958c2021-02-07 23:46:10 -0600110def 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 Simpsond51bcab2021-05-18 12:01:09 -0500114 hex_common.read_overrides_file(sys.argv[4])
Taylor Simpson793958c2021-02-07 23:46:10 -0600115 hex_common.calculate_attribs()
Taylor Simpsonb4478072023-12-10 15:07:05 -0700116 hex_common.init_registers()
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +0200117 ## 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 Simpson793958c2021-02-07 23:46:10 -0600129 tagregs = hex_common.get_tagregs()
130 tagimms = hex_common.get_tagimms()
131
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +0200132 output_file = sys.argv[-1]
Marco Liebel5bb322e2023-03-20 02:25:33 -0700133 with open(output_file, "w") as f:
Taylor Simpson793958c2021-02-07 23:46:10 -0600134 f.write("#ifndef HEXAGON_TCG_FUNCS_H\n")
135 f.write("#define HEXAGON_TCG_FUNCS_H\n\n")
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +0200136 if is_idef_parser_enabled:
Marco Liebel5bb322e2023-03-20 02:25:33 -0700137 f.write('#include "idef-generated-emitter.h.inc"\n\n')
Taylor Simpson793958c2021-02-07 23:46:10 -0600138
139 for tag in hex_common.tags:
140 ## Skip the priv instructions
Marco Liebel5bb322e2023-03-20 02:25:33 -0700141 if "A_PRIV" in hex_common.attribdict[tag]:
Taylor Simpson793958c2021-02-07 23:46:10 -0600142 continue
143 ## Skip the guest instructions
Marco Liebel5bb322e2023-03-20 02:25:33 -0700144 if "A_GUEST" in hex_common.attribdict[tag]:
Taylor Simpson793958c2021-02-07 23:46:10 -0600145 continue
146 ## Skip the diag instructions
Marco Liebel5bb322e2023-03-20 02:25:33 -0700147 if tag == "Y6_diag":
Taylor Simpson793958c2021-02-07 23:46:10 -0600148 continue
Marco Liebel5bb322e2023-03-20 02:25:33 -0700149 if tag == "Y6_diag0":
Taylor Simpson793958c2021-02-07 23:46:10 -0600150 continue
Marco Liebel5bb322e2023-03-20 02:25:33 -0700151 if tag == "Y6_diag1":
Taylor Simpson793958c2021-02-07 23:46:10 -0600152 continue
153
154 gen_def_tcg_func(f, tag, tagregs, tagimms)
155
156 f.write("#endif /* HEXAGON_TCG_FUNCS_H */\n")
157
Marco Liebel5bb322e2023-03-20 02:25:33 -0700158
Taylor Simpson793958c2021-02-07 23:46:10 -0600159if __name__ == "__main__":
160 main()