xref: /qemu/target/hexagon/gen_op_regs.py (revision 7653b1ea)
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##
27##     Generate the register and immediate operands for each instruction
28##
29def calculate_regid_reg(tag):
30    def letter_inc(x):
31        return chr(ord(x) + 1)
32
33    ordered_implregs = ["SP", "FP", "LR"]
34    srcdst_lett = "X"
35    src_lett = "S"
36    dst_lett = "D"
37    retstr = ""
38    mapdict = {}
39    for reg in ordered_implregs:
40        reg_rd = 0
41        reg_wr = 0
42        if ("A_IMPLICIT_WRITES_" + reg) in hex_common.attribdict[tag]:
43            reg_wr = 1
44        if reg_rd and reg_wr:
45            retstr += srcdst_lett
46            mapdict[srcdst_lett] = reg
47            srcdst_lett = letter_inc(srcdst_lett)
48        elif reg_rd:
49            retstr += src_lett
50            mapdict[src_lett] = reg
51            src_lett = letter_inc(src_lett)
52        elif reg_wr:
53            retstr += dst_lett
54            mapdict[dst_lett] = reg
55            dst_lett = letter_inc(dst_lett)
56    return retstr, mapdict
57
58
59def calculate_regid_letters(tag):
60    retstr, mapdict = calculate_regid_reg(tag)
61    return retstr
62
63
64def strip_reg_prefix(x):
65    y = x.replace("UREG.", "")
66    y = y.replace("MREG.", "")
67    return y.replace("GREG.", "")
68
69
70def main():
71    hex_common.read_semantics_file(sys.argv[1])
72    hex_common.read_attribs_file(sys.argv[2])
73    hex_common.init_registers()
74    tagregs = hex_common.get_tagregs(full=True)
75    tagimms = hex_common.get_tagimms()
76
77    with open(sys.argv[3], "w") as f:
78        for tag in hex_common.tags:
79            regs = tagregs[tag]
80            rregs = []
81            wregs = []
82            regids = ""
83            for regtype, regid, _, numregs in regs:
84                reg = hex_common.get_register(tag, regtype, regid)
85                if reg.is_read():
86                    if regid[0] not in regids:
87                        regids += regid[0]
88                    rregs.append(regtype + regid + numregs)
89                if reg.is_written():
90                    wregs.append(regtype + regid + numregs)
91                    if regid[0] not in regids:
92                        regids += regid[0]
93            for attrib in hex_common.attribdict[tag]:
94                if hex_common.attribinfo[attrib]["rreg"]:
95                    rregs.append(strip_reg_prefix(attribinfo[attrib]["rreg"]))
96                if hex_common.attribinfo[attrib]["wreg"]:
97                    wregs.append(strip_reg_prefix(attribinfo[attrib]["wreg"]))
98            regids += calculate_regid_letters(tag)
99            f.write(
100                f'REGINFO({tag},"{regids}",\t/*RD:*/\t"{",".join(rregs)}",'
101                f'\t/*WR:*/\t"{",".join(wregs)}")\n'
102            )
103
104        for tag in hex_common.tags:
105            imms = tagimms[tag]
106            f.write(f"IMMINFO({tag}")
107            if not imms:
108                f.write(""",'u',0,0,'U',0,0""")
109            for sign, size, shamt in imms:
110                if sign == "r":
111                    sign = "s"
112                if not shamt:
113                    shamt = "0"
114                f.write(f""",'{sign}',{size},{shamt}""")
115            if len(imms) == 1:
116                if sign.isupper():
117                    myu = "u"
118                else:
119                    myu = "U"
120                f.write(f""",'{myu}',0,0""")
121            f.write(")\n")
122
123
124if __name__ == "__main__":
125    main()
126