xref: /qemu/target/hexagon/gen_helper_protos.py (revision 8afc43ea)
1#!/usr/bin/env python3
2
3##
4##  Copyright(c) 2019-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_helper_prototype
27##
28def_helper_types = {
29    "N": "s32",
30    "O": "s32",
31    "P": "s32",
32    "M": "s32",
33    "C": "s32",
34    "R": "s32",
35    "V": "ptr",
36    "Q": "ptr",
37}
38
39def_helper_types_pair = {
40    "R": "s64",
41    "C": "s64",
42    "S": "s64",
43    "G": "s64",
44    "V": "ptr",
45    "Q": "ptr",
46}
47
48
49def gen_def_helper_opn(f, tag, regtype, regid, toss, numregs, i):
50    if hex_common.is_pair(regid):
51        f.write(f", {def_helper_types_pair[regtype]}")
52    elif hex_common.is_single(regid):
53        f.write(f", {def_helper_types[regtype]}")
54    else:
55        print("Bad register parse: ", regtype, regid, toss, numregs)
56
57
58##
59## Generate the DEF_HELPER prototype for an instruction
60##     For A2_add: Rd32=add(Rs32,Rt32)
61##     We produce:
62##         DEF_HELPER_3(A2_add, s32, env, s32, s32)
63##
64def gen_helper_prototype(f, tag, tagregs, tagimms):
65    regs = tagregs[tag]
66    imms = tagimms[tag]
67
68    numresults = 0
69    numscalarresults = 0
70    numscalarreadwrite = 0
71    for regtype, regid, toss, numregs in regs:
72        if hex_common.is_written(regid):
73            numresults += 1
74            if hex_common.is_scalar_reg(regtype):
75                numscalarresults += 1
76        if hex_common.is_readwrite(regid):
77            if hex_common.is_scalar_reg(regtype):
78                numscalarreadwrite += 1
79
80    if numscalarresults > 1:
81        ## The helper is bogus when there is more than one result
82        f.write(f"DEF_HELPER_1({tag}, void, env)\n")
83    else:
84        ## Figure out how many arguments the helper will take
85        if numscalarresults == 0:
86            def_helper_size = len(regs) + len(imms) + numscalarreadwrite + 1
87            if hex_common.need_pkt_has_multi_cof(tag):
88                def_helper_size += 1
89            if hex_common.need_part1(tag):
90                def_helper_size += 1
91            if hex_common.need_slot(tag):
92                def_helper_size += 1
93            if hex_common.need_PC(tag):
94                def_helper_size += 1
95            if hex_common.helper_needs_next_PC(tag):
96                def_helper_size += 1
97            if hex_common.need_condexec_reg(tag, regs):
98                def_helper_size += 1
99            f.write(f"DEF_HELPER_{def_helper_size}({tag}")
100            ## The return type is void
101            f.write(", void")
102        else:
103            def_helper_size = len(regs) + len(imms) + numscalarreadwrite
104            if hex_common.need_pkt_has_multi_cof(tag):
105                def_helper_size += 1
106            if hex_common.need_part1(tag):
107                def_helper_size += 1
108            if hex_common.need_slot(tag):
109                def_helper_size += 1
110            if hex_common.need_PC(tag):
111                def_helper_size += 1
112            if hex_common.need_condexec_reg(tag, regs):
113                def_helper_size += 1
114            if hex_common.helper_needs_next_PC(tag):
115                def_helper_size += 1
116            f.write(f"DEF_HELPER_{def_helper_size}({tag}")
117
118        ## Generate the qemu DEF_HELPER type for each result
119        ## Iterate over this list twice
120        ## - Emit the scalar result
121        ## - Emit the vector result
122        i = 0
123        for regtype, regid, toss, numregs in regs:
124            if hex_common.is_written(regid):
125                if not hex_common.is_hvx_reg(regtype):
126                    gen_def_helper_opn(f, tag, regtype, regid, toss, numregs, i)
127                i += 1
128
129        ## Put the env between the outputs and inputs
130        f.write(", env")
131        i += 1
132
133        # Second pass
134        for regtype, regid, toss, numregs in regs:
135            if hex_common.is_written(regid):
136                if hex_common.is_hvx_reg(regtype):
137                    gen_def_helper_opn(f, tag, regtype, regid, toss, numregs, i)
138                    i += 1
139
140        ## For conditional instructions, we pass in the destination register
141        if "A_CONDEXEC" in hex_common.attribdict[tag]:
142            for regtype, regid, toss, numregs in regs:
143                if hex_common.is_writeonly(regid) and not hex_common.is_hvx_reg(
144                    regtype
145                ):
146                    gen_def_helper_opn(f, tag, regtype, regid, toss, numregs, i)
147                    i += 1
148
149        ## Generate the qemu type for each input operand (regs and immediates)
150        for regtype, regid, toss, numregs in regs:
151            if hex_common.is_read(regid):
152                if hex_common.is_hvx_reg(regtype) and hex_common.is_readwrite(regid):
153                    continue
154                gen_def_helper_opn(f, tag, regtype, regid, toss, numregs, i)
155                i += 1
156        for immlett, bits, immshift in imms:
157            f.write(", s32")
158
159        ## Add the arguments for the instruction pkt_has_multi_cof, slot and
160        ## part1 (if needed)
161        if hex_common.need_pkt_has_multi_cof(tag):
162            f.write(", i32")
163        if hex_common.need_PC(tag):
164            f.write(", i32")
165        if hex_common.helper_needs_next_PC(tag):
166            f.write(", i32")
167        if hex_common.need_slot(tag):
168            f.write(", i32")
169        if hex_common.need_part1(tag):
170            f.write(" , i32")
171        f.write(")\n")
172
173
174def main():
175    hex_common.read_semantics_file(sys.argv[1])
176    hex_common.read_attribs_file(sys.argv[2])
177    hex_common.read_overrides_file(sys.argv[3])
178    hex_common.read_overrides_file(sys.argv[4])
179    ## Whether or not idef-parser is enabled is
180    ## determined by the number of arguments to
181    ## this script:
182    ##
183    ##   5 args. -> not enabled,
184    ##   6 args. -> idef-parser enabled.
185    ##
186    ## The 6:th arg. then holds a list of the successfully
187    ## parsed instructions.
188    is_idef_parser_enabled = len(sys.argv) > 6
189    if is_idef_parser_enabled:
190        hex_common.read_idef_parser_enabled_file(sys.argv[5])
191    hex_common.calculate_attribs()
192    tagregs = hex_common.get_tagregs()
193    tagimms = hex_common.get_tagimms()
194
195    output_file = sys.argv[-1]
196    with open(output_file, "w") as f:
197        for tag in hex_common.tags:
198            ## Skip the priv instructions
199            if "A_PRIV" in hex_common.attribdict[tag]:
200                continue
201            ## Skip the guest instructions
202            if "A_GUEST" in hex_common.attribdict[tag]:
203                continue
204            ## Skip the diag instructions
205            if tag == "Y6_diag":
206                continue
207            if tag == "Y6_diag0":
208                continue
209            if tag == "Y6_diag1":
210                continue
211
212            if hex_common.skip_qemu_helper(tag):
213                continue
214            if hex_common.is_idef_parser_enabled(tag):
215                continue
216
217            gen_helper_prototype(f, tag, tagregs, tagimms)
218
219
220if __name__ == "__main__":
221    main()
222