xref: /qemu/target/hexagon/gen_helper_protos.py (revision 793958c9)
1*793958c9STaylor Simpson#!/usr/bin/env python3
2*793958c9STaylor Simpson
3*793958c9STaylor Simpson##
4*793958c9STaylor Simpson##  Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
5*793958c9STaylor Simpson##
6*793958c9STaylor Simpson##  This program is free software; you can redistribute it and/or modify
7*793958c9STaylor Simpson##  it under the terms of the GNU General Public License as published by
8*793958c9STaylor Simpson##  the Free Software Foundation; either version 2 of the License, or
9*793958c9STaylor Simpson##  (at your option) any later version.
10*793958c9STaylor Simpson##
11*793958c9STaylor Simpson##  This program is distributed in the hope that it will be useful,
12*793958c9STaylor Simpson##  but WITHOUT ANY WARRANTY; without even the implied warranty of
13*793958c9STaylor Simpson##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*793958c9STaylor Simpson##  GNU General Public License for more details.
15*793958c9STaylor Simpson##
16*793958c9STaylor Simpson##  You should have received a copy of the GNU General Public License
17*793958c9STaylor Simpson##  along with this program; if not, see <http://www.gnu.org/licenses/>.
18*793958c9STaylor Simpson##
19*793958c9STaylor Simpson
20*793958c9STaylor Simpsonimport sys
21*793958c9STaylor Simpsonimport re
22*793958c9STaylor Simpsonimport string
23*793958c9STaylor Simpsonimport hex_common
24*793958c9STaylor Simpson
25*793958c9STaylor Simpson##
26*793958c9STaylor Simpson## Helpers for gen_helper_prototype
27*793958c9STaylor Simpson##
28*793958c9STaylor Simpsondef_helper_types = {
29*793958c9STaylor Simpson    'N' : 's32',
30*793958c9STaylor Simpson    'O' : 's32',
31*793958c9STaylor Simpson    'P' : 's32',
32*793958c9STaylor Simpson    'M' : 's32',
33*793958c9STaylor Simpson    'C' : 's32',
34*793958c9STaylor Simpson    'R' : 's32',
35*793958c9STaylor Simpson    'V' : 'ptr',
36*793958c9STaylor Simpson    'Q' : 'ptr'
37*793958c9STaylor Simpson}
38*793958c9STaylor Simpson
39*793958c9STaylor Simpsondef_helper_types_pair = {
40*793958c9STaylor Simpson    'R' : 's64',
41*793958c9STaylor Simpson    'C' : 's64',
42*793958c9STaylor Simpson    'S' : 's64',
43*793958c9STaylor Simpson    'G' : 's64',
44*793958c9STaylor Simpson    'V' : 'ptr',
45*793958c9STaylor Simpson    'Q' : 'ptr'
46*793958c9STaylor Simpson}
47*793958c9STaylor Simpson
48*793958c9STaylor Simpsondef gen_def_helper_opn(f, tag, regtype, regid, toss, numregs, i):
49*793958c9STaylor Simpson    if (hex_common.is_pair(regid)):
50*793958c9STaylor Simpson        f.write(", %s" % (def_helper_types_pair[regtype]))
51*793958c9STaylor Simpson    elif (hex_common.is_single(regid)):
52*793958c9STaylor Simpson        f.write(", %s" % (def_helper_types[regtype]))
53*793958c9STaylor Simpson    else:
54*793958c9STaylor Simpson        print("Bad register parse: ",regtype,regid,toss,numregs)
55*793958c9STaylor Simpson
56*793958c9STaylor Simpson##
57*793958c9STaylor Simpson## Generate the DEF_HELPER prototype for an instruction
58*793958c9STaylor Simpson##     For A2_add: Rd32=add(Rs32,Rt32)
59*793958c9STaylor Simpson##     We produce:
60*793958c9STaylor Simpson##         DEF_HELPER_3(A2_add, s32, env, s32, s32)
61*793958c9STaylor Simpson##
62*793958c9STaylor Simpsondef gen_helper_prototype(f, tag, tagregs, tagimms):
63*793958c9STaylor Simpson    regs = tagregs[tag]
64*793958c9STaylor Simpson    imms = tagimms[tag]
65*793958c9STaylor Simpson
66*793958c9STaylor Simpson    numresults = 0
67*793958c9STaylor Simpson    numscalarresults = 0
68*793958c9STaylor Simpson    numscalarreadwrite = 0
69*793958c9STaylor Simpson    for regtype,regid,toss,numregs in regs:
70*793958c9STaylor Simpson        if (hex_common.is_written(regid)):
71*793958c9STaylor Simpson            numresults += 1
72*793958c9STaylor Simpson            if (hex_common.is_scalar_reg(regtype)):
73*793958c9STaylor Simpson                numscalarresults += 1
74*793958c9STaylor Simpson        if (hex_common.is_readwrite(regid)):
75*793958c9STaylor Simpson            if (hex_common.is_scalar_reg(regtype)):
76*793958c9STaylor Simpson                numscalarreadwrite += 1
77*793958c9STaylor Simpson
78*793958c9STaylor Simpson    if (numscalarresults > 1):
79*793958c9STaylor Simpson        ## The helper is bogus when there is more than one result
80*793958c9STaylor Simpson        f.write('DEF_HELPER_1(%s, void, env)\n' % tag)
81*793958c9STaylor Simpson    else:
82*793958c9STaylor Simpson        ## Figure out how many arguments the helper will take
83*793958c9STaylor Simpson        if (numscalarresults == 0):
84*793958c9STaylor Simpson            def_helper_size = len(regs)+len(imms)+numscalarreadwrite+1
85*793958c9STaylor Simpson            if hex_common.need_part1(tag): def_helper_size += 1
86*793958c9STaylor Simpson            if hex_common.need_slot(tag): def_helper_size += 1
87*793958c9STaylor Simpson            f.write('DEF_HELPER_%s(%s' % (def_helper_size, tag))
88*793958c9STaylor Simpson            ## The return type is void
89*793958c9STaylor Simpson            f.write(', void' )
90*793958c9STaylor Simpson        else:
91*793958c9STaylor Simpson            def_helper_size = len(regs)+len(imms)+numscalarreadwrite
92*793958c9STaylor Simpson            if hex_common.need_part1(tag): def_helper_size += 1
93*793958c9STaylor Simpson            if hex_common.need_slot(tag): def_helper_size += 1
94*793958c9STaylor Simpson            f.write('DEF_HELPER_%s(%s' % (def_helper_size, tag))
95*793958c9STaylor Simpson
96*793958c9STaylor Simpson        ## Generate the qemu DEF_HELPER type for each result
97*793958c9STaylor Simpson        i=0
98*793958c9STaylor Simpson        for regtype,regid,toss,numregs in regs:
99*793958c9STaylor Simpson            if (hex_common.is_written(regid)):
100*793958c9STaylor Simpson                gen_def_helper_opn(f, tag, regtype, regid, toss, numregs, i)
101*793958c9STaylor Simpson                i += 1
102*793958c9STaylor Simpson
103*793958c9STaylor Simpson        ## Put the env between the outputs and inputs
104*793958c9STaylor Simpson        f.write(', env' )
105*793958c9STaylor Simpson        i += 1
106*793958c9STaylor Simpson
107*793958c9STaylor Simpson        ## Generate the qemu type for each input operand (regs and immediates)
108*793958c9STaylor Simpson        for regtype,regid,toss,numregs in regs:
109*793958c9STaylor Simpson            if (hex_common.is_read(regid)):
110*793958c9STaylor Simpson                gen_def_helper_opn(f, tag, regtype, regid, toss, numregs, i)
111*793958c9STaylor Simpson                i += 1
112*793958c9STaylor Simpson        for immlett,bits,immshift in imms:
113*793958c9STaylor Simpson            f.write(", s32")
114*793958c9STaylor Simpson
115*793958c9STaylor Simpson        ## Add the arguments for the instruction slot and part1 (if needed)
116*793958c9STaylor Simpson        if hex_common.need_slot(tag): f.write(', i32' )
117*793958c9STaylor Simpson        if hex_common.need_part1(tag): f.write(' , i32' )
118*793958c9STaylor Simpson        f.write(')\n')
119*793958c9STaylor Simpson
120*793958c9STaylor Simpsondef main():
121*793958c9STaylor Simpson    hex_common.read_semantics_file(sys.argv[1])
122*793958c9STaylor Simpson    hex_common.read_attribs_file(sys.argv[2])
123*793958c9STaylor Simpson    hex_common.read_overrides_file(sys.argv[3])
124*793958c9STaylor Simpson    hex_common.calculate_attribs()
125*793958c9STaylor Simpson    tagregs = hex_common.get_tagregs()
126*793958c9STaylor Simpson    tagimms = hex_common.get_tagimms()
127*793958c9STaylor Simpson
128*793958c9STaylor Simpson    with open(sys.argv[4], 'w') as f:
129*793958c9STaylor Simpson        for tag in hex_common.tags:
130*793958c9STaylor Simpson            ## Skip the priv instructions
131*793958c9STaylor Simpson            if ( "A_PRIV" in hex_common.attribdict[tag] ) :
132*793958c9STaylor Simpson                continue
133*793958c9STaylor Simpson            ## Skip the guest instructions
134*793958c9STaylor Simpson            if ( "A_GUEST" in hex_common.attribdict[tag] ) :
135*793958c9STaylor Simpson                continue
136*793958c9STaylor Simpson            ## Skip the diag instructions
137*793958c9STaylor Simpson            if ( tag == "Y6_diag" ) :
138*793958c9STaylor Simpson                continue
139*793958c9STaylor Simpson            if ( tag == "Y6_diag0" ) :
140*793958c9STaylor Simpson                continue
141*793958c9STaylor Simpson            if ( tag == "Y6_diag1" ) :
142*793958c9STaylor Simpson                continue
143*793958c9STaylor Simpson
144*793958c9STaylor Simpson            if ( hex_common.skip_qemu_helper(tag) ):
145*793958c9STaylor Simpson                continue
146*793958c9STaylor Simpson
147*793958c9STaylor Simpson            gen_helper_prototype(f, tag, tagregs, tagimms)
148*793958c9STaylor Simpson
149*793958c9STaylor Simpsonif __name__ == "__main__":
150*793958c9STaylor Simpson    main()
151