xref: /qemu/target/hexagon/gen_analyze_funcs.py (revision 4d13bb51d2db3134754d3361d289f719a61c4673)
1#!/usr/bin/env python3
2
3##
4##  Copyright(c) 2022-2023 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## Helpers for gen_analyze_func
27##
28def is_predicated(tag):
29    return 'A_CONDEXEC' in hex_common.attribdict[tag]
30
31def analyze_opn_old(f, tag, regtype, regid, regno):
32    regN = "%s%sN" % (regtype, regid)
33    predicated = "true" if is_predicated(tag) else "false"
34    if (regtype == "R"):
35        if (regid in {"ss", "tt"}):
36            f.write("//    const int %s = insn->regno[%d];\n" % \
37                (regN, regno))
38        elif (regid in {"dd", "ee", "xx", "yy"}):
39            f.write("    const int %s = insn->regno[%d];\n" % (regN, regno))
40            f.write("    ctx_log_reg_write_pair(ctx, %s, %s);\n" % \
41                (regN, predicated))
42        elif (regid in {"s", "t", "u", "v"}):
43            f.write("//    const int %s = insn->regno[%d];\n" % \
44                (regN, regno))
45        elif (regid in {"d", "e", "x", "y"}):
46            f.write("    const int %s = insn->regno[%d];\n" % (regN, regno))
47            f.write("    ctx_log_reg_write(ctx, %s, %s);\n" % \
48                (regN, predicated))
49        else:
50            print("Bad register parse: ", regtype, regid)
51    elif (regtype == "P"):
52        if (regid in {"s", "t", "u", "v"}):
53            f.write("//    const int %s = insn->regno[%d];\n" % \
54                (regN, regno))
55        elif (regid in {"d", "e", "x"}):
56            f.write("    const int %s = insn->regno[%d];\n" % (regN, regno))
57            f.write("    ctx_log_pred_write(ctx, %s);\n" % (regN))
58        else:
59            print("Bad register parse: ", regtype, regid)
60    elif (regtype == "C"):
61        if (regid == "ss"):
62            f.write("//    const int %s = insn->regno[%d] + HEX_REG_SA0;\n" % \
63                (regN, regno))
64        elif (regid == "dd"):
65            f.write("    const int %s = insn->regno[%d] + HEX_REG_SA0;\n" % \
66                (regN, regno))
67            f.write("    ctx_log_reg_write_pair(ctx, %s, %s);\n" % \
68                (regN, predicated))
69        elif (regid == "s"):
70            f.write("//    const int %s = insn->regno[%d] + HEX_REG_SA0;\n" % \
71                (regN, regno))
72        elif (regid == "d"):
73            f.write("    const int %s = insn->regno[%d] + HEX_REG_SA0;\n" % \
74                (regN, regno))
75            f.write("    ctx_log_reg_write(ctx, %s, %s);\n" % \
76                (regN, predicated))
77        else:
78            print("Bad register parse: ", regtype, regid)
79    elif (regtype == "M"):
80        if (regid == "u"):
81            f.write("//    const int %s = insn->regno[%d];\n"% \
82                (regN, regno))
83        else:
84            print("Bad register parse: ", regtype, regid)
85    elif (regtype == "V"):
86        if (regid in {"dd", "xx"}):
87            f.write("//    const int %s = insn->regno[%d];\n" %\
88                (regN, regno))
89        elif (regid in {"uu", "vv"}):
90            f.write("//    const int %s = insn->regno[%d];\n" % \
91                (regN, regno))
92        elif (regid in {"s", "u", "v", "w"}):
93            f.write("//    const int %s = insn->regno[%d];\n" % \
94                (regN, regno))
95        elif (regid in {"d", "x", "y"}):
96            f.write("//    const int %s = insn->regno[%d];\n" % \
97                (regN, regno))
98        else:
99            print("Bad register parse: ", regtype, regid)
100    elif (regtype == "Q"):
101        if (regid in {"d", "e", "x"}):
102            f.write("//    const int %s = insn->regno[%d];\n" % \
103                (regN, regno))
104        elif (regid in {"s", "t", "u", "v"}):
105            f.write("//    const int %s = insn->regno[%d];\n" % \
106                (regN, regno))
107        else:
108            print("Bad register parse: ", regtype, regid)
109    elif (regtype == "G"):
110        if (regid in {"dd"}):
111            f.write("//    const int %s = insn->regno[%d];\n" % \
112                (regN, regno))
113        elif (regid in {"d"}):
114            f.write("//    const int %s = insn->regno[%d];\n" % \
115                (regN, regno))
116        elif (regid in {"ss"}):
117            f.write("//    const int %s = insn->regno[%d];\n" % \
118                (regN, regno))
119        elif (regid in {"s"}):
120            f.write("//    const int %s = insn->regno[%d];\n" % \
121                (regN, regno))
122        else:
123            print("Bad register parse: ", regtype, regid)
124    elif (regtype == "S"):
125        if (regid in {"dd"}):
126            f.write("//    const int %s = insn->regno[%d];\n" % \
127                (regN, regno))
128        elif (regid in {"d"}):
129            f.write("//    const int %s = insn->regno[%d];\n" % \
130                (regN, regno))
131        elif (regid in {"ss"}):
132            f.write("//    const int %s = insn->regno[%d];\n" % \
133                (regN, regno))
134        elif (regid in {"s"}):
135            f.write("//    const int %s = insn->regno[%d];\n" % \
136                (regN, regno))
137        else:
138            print("Bad register parse: ", regtype, regid)
139    else:
140        print("Bad register parse: ", regtype, regid)
141
142def analyze_opn_new(f, tag, regtype, regid, regno):
143    regN = "%s%sN" % (regtype, regid)
144    if (regtype == "N"):
145        if (regid in {"s", "t"}):
146            f.write("//    const int %s = insn->regno[%d];\n" % \
147                (regN, regno))
148        else:
149            print("Bad register parse: ", regtype, regid)
150    elif (regtype == "P"):
151        if (regid in {"t", "u", "v"}):
152            f.write("//    const int %s = insn->regno[%d];\n" % \
153                (regN, regno))
154        else:
155            print("Bad register parse: ", regtype, regid)
156    elif (regtype == "O"):
157        if (regid == "s"):
158            f.write("//    const int %s = insn->regno[%d];\n" % \
159                (regN, regno))
160        else:
161            print("Bad register parse: ", regtype, regid)
162    else:
163        print("Bad register parse: ", regtype, regid)
164
165def analyze_opn(f, tag, regtype, regid, toss, numregs, i):
166    if (hex_common.is_pair(regid)):
167        analyze_opn_old(f, tag, regtype, regid, i)
168    elif (hex_common.is_single(regid)):
169        if hex_common.is_old_val(regtype, regid, tag):
170            analyze_opn_old(f,tag, regtype, regid, i)
171        elif hex_common.is_new_val(regtype, regid, tag):
172            analyze_opn_new(f, tag, regtype, regid, i)
173        else:
174            print("Bad register parse: ", regtype, regid, toss, numregs)
175    else:
176        print("Bad register parse: ", regtype, regid, toss, numregs)
177
178##
179## Generate the code to analyze the instruction
180##     For A2_add: Rd32=add(Rs32,Rt32), { RdV=RsV+RtV;}
181##     We produce:
182##     static void analyze_A2_add(DisasContext *ctx)
183##     {
184##         Insn *insn G_GNUC_UNUSED = ctx->insn;
185##         const int RdN = insn->regno[0];
186##         ctx_log_reg_write(ctx, RdN, false);
187##     //    const int RsN = insn->regno[1];
188##     //    const int RtN = insn->regno[2];
189##     }
190##
191def gen_analyze_func(f, tag, regs, imms):
192    f.write("static void analyze_%s(DisasContext *ctx)\n" %tag)
193    f.write('{\n')
194
195    f.write("    Insn *insn G_GNUC_UNUSED = ctx->insn;\n")
196
197    i=0
198    ## Analyze all the registers
199    for regtype, regid, toss, numregs in regs:
200        analyze_opn(f, tag, regtype, regid, toss, numregs, i)
201        i += 1
202
203    has_generated_helper = (not hex_common.skip_qemu_helper(tag) and
204                            not hex_common.is_idef_parser_enabled(tag))
205    if (has_generated_helper and
206        'A_SCALAR_LOAD' in hex_common.attribdict[tag]):
207        f.write("    ctx->need_pkt_has_store_s1 = true;\n")
208
209    f.write("}\n\n")
210
211def main():
212    hex_common.read_semantics_file(sys.argv[1])
213    hex_common.read_attribs_file(sys.argv[2])
214    hex_common.read_overrides_file(sys.argv[3])
215    hex_common.read_overrides_file(sys.argv[4])
216    ## Whether or not idef-parser is enabled is
217    ## determined by the number of arguments to
218    ## this script:
219    ##
220    ##   5 args. -> not enabled,
221    ##   6 args. -> idef-parser enabled.
222    ##
223    ## The 6:th arg. then holds a list of the successfully
224    ## parsed instructions.
225    is_idef_parser_enabled = len(sys.argv) > 6
226    if is_idef_parser_enabled:
227        hex_common.read_idef_parser_enabled_file(sys.argv[5])
228    hex_common.calculate_attribs()
229    tagregs = hex_common.get_tagregs()
230    tagimms = hex_common.get_tagimms()
231
232    with open(sys.argv[-1], 'w') as f:
233        f.write("#ifndef HEXAGON_TCG_FUNCS_H\n")
234        f.write("#define HEXAGON_TCG_FUNCS_H\n\n")
235
236        for tag in hex_common.tags:
237            gen_analyze_func(f, tag, tagregs[tag], tagimms[tag])
238
239        f.write("#endif    /* HEXAGON_TCG_FUNCS_H */\n")
240
241if __name__ == "__main__":
242    main()
243