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, 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        hex_common.bad_register(regtype, regid)
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 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_pkt_need_commit(tag):
90                def_helper_size += 1
91            if hex_common.need_part1(tag):
92                def_helper_size += 1
93            if hex_common.need_slot(tag):
94                def_helper_size += 1
95            if hex_common.need_PC(tag):
96                def_helper_size += 1
97            if hex_common.helper_needs_next_PC(tag):
98                def_helper_size += 1
99            if hex_common.need_condexec_reg(tag, regs):
100                def_helper_size += 1
101            f.write(f"DEF_HELPER_{def_helper_size}({tag}")
102            ## The return type is void
103            f.write(", void")
104        else:
105            def_helper_size = len(regs) + len(imms) + numscalarreadwrite
106            if hex_common.need_pkt_has_multi_cof(tag):
107                def_helper_size += 1
108            if hex_common.need_pkt_need_commit(tag):
109                def_helper_size += 1
110            if hex_common.need_part1(tag):
111                def_helper_size += 1
112            if hex_common.need_slot(tag):
113                def_helper_size += 1
114            if hex_common.need_PC(tag):
115                def_helper_size += 1
116            if hex_common.need_condexec_reg(tag, regs):
117                def_helper_size += 1
118            if hex_common.helper_needs_next_PC(tag):
119                def_helper_size += 1
120            f.write(f"DEF_HELPER_{def_helper_size}({tag}")
121
122        ## Generate the qemu DEF_HELPER type for each result
123        ## Iterate over this list twice
124        ## - Emit the scalar result
125        ## - Emit the vector result
126        i = 0
127        for regtype, regid in regs:
128            if hex_common.is_written(regid):
129                if not hex_common.is_hvx_reg(regtype):
130                    gen_def_helper_opn(f, tag, regtype, regid, i)
131                i += 1
132
133        ## Put the env between the outputs and inputs
134        f.write(", env")
135        i += 1
136
137        # Second pass
138        for regtype, regid in regs:
139            if hex_common.is_written(regid):
140                if hex_common.is_hvx_reg(regtype):
141                    gen_def_helper_opn(f, tag, regtype, regid, i)
142                    i += 1
143
144        ## For conditional instructions, we pass in the destination register
145        if "A_CONDEXEC" in hex_common.attribdict[tag]:
146            for regtype, regid in regs:
147                if hex_common.is_writeonly(regid) and not hex_common.is_hvx_reg(
148                    regtype
149                ):
150                    gen_def_helper_opn(f, tag, regtype, regid, i)
151                    i += 1
152
153        ## Generate the qemu type for each input operand (regs and immediates)
154        for regtype, regid in regs:
155            if hex_common.is_read(regid):
156                if hex_common.is_hvx_reg(regtype) and hex_common.is_readwrite(regid):
157                    continue
158                gen_def_helper_opn(f, tag, regtype, regid, i)
159                i += 1
160        for immlett, bits, immshift in imms:
161            f.write(", s32")
162
163        ## Add the arguments for the instruction pkt_has_multi_cof,
164        ## pkt_needs_commit, PC, next_PC, slot, and part1 (if needed)
165        if hex_common.need_pkt_has_multi_cof(tag):
166            f.write(", i32")
167        if hex_common.need_pkt_need_commit(tag):
168            f.write(', i32')
169        if hex_common.need_PC(tag):
170            f.write(", i32")
171        if hex_common.helper_needs_next_PC(tag):
172            f.write(", i32")
173        if hex_common.need_slot(tag):
174            f.write(", i32")
175        if hex_common.need_part1(tag):
176            f.write(" , i32")
177        f.write(")\n")
178
179
180def main():
181    hex_common.read_semantics_file(sys.argv[1])
182    hex_common.read_attribs_file(sys.argv[2])
183    hex_common.read_overrides_file(sys.argv[3])
184    hex_common.read_overrides_file(sys.argv[4])
185    ## Whether or not idef-parser is enabled is
186    ## determined by the number of arguments to
187    ## this script:
188    ##
189    ##   5 args. -> not enabled,
190    ##   6 args. -> idef-parser enabled.
191    ##
192    ## The 6:th arg. then holds a list of the successfully
193    ## parsed instructions.
194    is_idef_parser_enabled = len(sys.argv) > 6
195    if is_idef_parser_enabled:
196        hex_common.read_idef_parser_enabled_file(sys.argv[5])
197    hex_common.calculate_attribs()
198    tagregs = hex_common.get_tagregs()
199    tagimms = hex_common.get_tagimms()
200
201    output_file = sys.argv[-1]
202    with open(output_file, "w") as f:
203        for tag in hex_common.tags:
204            ## Skip the priv instructions
205            if "A_PRIV" in hex_common.attribdict[tag]:
206                continue
207            ## Skip the guest instructions
208            if "A_GUEST" in hex_common.attribdict[tag]:
209                continue
210            ## Skip the diag instructions
211            if tag == "Y6_diag":
212                continue
213            if tag == "Y6_diag0":
214                continue
215            if tag == "Y6_diag1":
216                continue
217
218            if hex_common.skip_qemu_helper(tag):
219                continue
220            if hex_common.is_idef_parser_enabled(tag):
221                continue
222
223            gen_helper_prototype(f, tag, tagregs, tagimms)
224
225
226if __name__ == "__main__":
227    main()
228