4 ## Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
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.
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.
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/>.
26 ## Helpers for gen_helper_function
29 f
.write(" uint32_t EA;\n")
31 def gen_helper_return_type(f
,regtype
,regid
,regno
):
32 if regno
> 1 : f
.write(", ")
35 def gen_helper_return_type_pair(f
,regtype
,regid
,regno
):
36 if regno
> 1 : f
.write(", ")
39 def gen_helper_arg(f
,regtype
,regid
,regno
):
40 if regno
> 0 : f
.write(", " )
41 f
.write("int32_t %s%sV" % (regtype
,regid
))
43 def gen_helper_arg_new(f
,regtype
,regid
,regno
):
44 if regno
>= 0 : f
.write(", " )
45 f
.write("int32_t %s%sN" % (regtype
,regid
))
47 def gen_helper_arg_pair(f
,regtype
,regid
,regno
):
48 if regno
>= 0 : f
.write(", ")
49 f
.write("int64_t %s%sV" % (regtype
,regid
))
51 def gen_helper_arg_opn(f
,regtype
,regid
,i
,tag
):
52 if (hex_common
.is_pair(regid
)):
53 gen_helper_arg_pair(f
,regtype
,regid
,i
)
54 elif (hex_common
.is_single(regid
)):
55 if hex_common
.is_old_val(regtype
, regid
, tag
):
56 gen_helper_arg(f
,regtype
,regid
,i
)
57 elif hex_common
.is_new_val(regtype
, regid
, tag
):
58 gen_helper_arg_new(f
,regtype
,regid
,i
)
60 print("Bad register parse: ",regtype
,regid
,toss
,numregs
)
62 print("Bad register parse: ",regtype
,regid
,toss
,numregs
)
64 def gen_helper_arg_imm(f
,immlett
):
65 f
.write(", int32_t %s" % (hex_common
.imm_name(immlett
)))
67 def gen_helper_dest_decl(f
,regtype
,regid
,regno
,subfield
=""):
68 f
.write(" int32_t %s%sV%s = 0;\n" % \
69 (regtype
,regid
,subfield
))
71 def gen_helper_dest_decl_pair(f
,regtype
,regid
,regno
,subfield
=""):
72 f
.write(" int64_t %s%sV%s = 0;\n" % \
73 (regtype
,regid
,subfield
))
75 def gen_helper_dest_decl_opn(f
,regtype
,regid
,i
):
76 if (hex_common
.is_pair(regid
)):
77 gen_helper_dest_decl_pair(f
,regtype
,regid
,i
)
78 elif (hex_common
.is_single(regid
)):
79 gen_helper_dest_decl(f
,regtype
,regid
,i
)
81 print("Bad register parse: ",regtype
,regid
,toss
,numregs
)
83 def gen_helper_return(f
,regtype
,regid
,regno
):
84 f
.write(" return %s%sV;\n" % (regtype
,regid
))
86 def gen_helper_return_pair(f
,regtype
,regid
,regno
):
87 f
.write(" return %s%sV;\n" % (regtype
,regid
))
89 def gen_helper_return_opn(f
, regtype
, regid
, i
):
90 if (hex_common
.is_pair(regid
)):
91 gen_helper_return_pair(f
,regtype
,regid
,i
)
92 elif (hex_common
.is_single(regid
)):
93 gen_helper_return(f
,regtype
,regid
,i
)
95 print("Bad register parse: ",regtype
,regid
,toss
,numregs
)
98 ## Generate the TCG code to call the helper
99 ## For A2_add: Rd32=add(Rs32,Rt32), { RdV=RsV+RtV;}
101 ## int32_t HELPER(A2_add)(CPUHexagonState *env, int32_t RsV, int32_t RtV)
103 ## uint32_t slot __attribute__(unused)) = 4;
106 ## COUNT_HELPER(A2_add);
110 def gen_helper_function(f
, tag
, tagregs
, tagimms
):
116 numscalarreadwrite
= 0
117 for regtype
,regid
,toss
,numregs
in regs
:
118 if (hex_common
.is_written(regid
)):
120 if (hex_common
.is_scalar_reg(regtype
)):
121 numscalarresults
+= 1
122 if (hex_common
.is_readwrite(regid
)):
123 if (hex_common
.is_scalar_reg(regtype
)):
124 numscalarreadwrite
+= 1
126 if (numscalarresults
> 1):
127 ## The helper is bogus when there is more than one result
128 f
.write("void HELPER(%s)(CPUHexagonState *env) { BOGUS_HELPER(%s); }\n"
131 ## The return type of the function is the type of the destination
134 for regtype
,regid
,toss
,numregs
in regs
:
135 if (hex_common
.is_written(regid
)):
136 if (hex_common
.is_pair(regid
)):
137 gen_helper_return_type_pair(f
,regtype
,regid
,i
)
138 elif (hex_common
.is_single(regid
)):
139 gen_helper_return_type(f
,regtype
,regid
,i
)
141 print("Bad register parse: ",regtype
,regid
,toss
,numregs
)
144 if (numscalarresults
== 0):
146 f
.write(" HELPER(%s)(CPUHexagonState *env" % tag
)
150 ## Arguments to the helper function are the source regs and immediates
151 for regtype
,regid
,toss
,numregs
in regs
:
152 if (hex_common
.is_read(regid
)):
153 gen_helper_arg_opn(f
,regtype
,regid
,i
,tag
)
155 for immlett
,bits
,immshift
in imms
:
156 gen_helper_arg_imm(f
,immlett
)
158 if hex_common
.need_slot(tag
):
159 if i
> 0: f
.write(", ")
160 f
.write("uint32_t slot")
162 if hex_common
.need_part1(tag
):
163 if i
> 0: f
.write(", ")
164 f
.write("uint32_t part1")
166 if (not hex_common
.need_slot(tag
)):
167 f
.write(" uint32_t slot __attribute__((unused)) = 4;\n" )
168 if hex_common
.need_ea(tag
): gen_decl_ea(f
)
169 ## Declare the return variable
171 for regtype
,regid
,toss
,numregs
in regs
:
172 if (hex_common
.is_writeonly(regid
)):
173 gen_helper_dest_decl_opn(f
,regtype
,regid
,i
)
176 if 'A_FPOP' in hex_common
.attribdict
[tag
]:
177 f
.write(' arch_fpop_start(env);\n');
179 f
.write(" %s\n" % hex_common
.semdict
[tag
])
181 if 'A_FPOP' in hex_common
.attribdict
[tag
]:
182 f
.write(' arch_fpop_end(env);\n');
184 ## Save/return the return variable
185 for regtype
,regid
,toss
,numregs
in regs
:
186 if (hex_common
.is_written(regid
)):
187 gen_helper_return_opn(f
, regtype
, regid
, i
)
189 ## End of the helper definition
192 hex_common
.read_semantics_file(sys
.argv
[1])
193 hex_common
.read_attribs_file(sys
.argv
[2])
194 hex_common
.read_overrides_file(sys
.argv
[3])
195 hex_common
.calculate_attribs()
196 tagregs
= hex_common
.get_tagregs()
197 tagimms
= hex_common
.get_tagimms()
199 with
open(sys
.argv
[4], 'w') as f
:
200 for tag
in hex_common
.tags
:
201 ## Skip the priv instructions
202 if ( "A_PRIV" in hex_common
.attribdict
[tag
] ) :
204 ## Skip the guest instructions
205 if ( "A_GUEST" in hex_common
.attribdict
[tag
] ) :
207 ## Skip the diag instructions
208 if ( tag
== "Y6_diag" ) :
210 if ( tag
== "Y6_diag0" ) :
212 if ( tag
== "Y6_diag1" ) :
214 if ( hex_common
.skip_qemu_helper(tag
) ):
217 gen_helper_function(f
, tag
, tagregs
, tagimms
)
219 if __name__
== "__main__":