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