]> git.proxmox.com Git - mirror_qemu.git/blob - target/hexagon/hex_common.py
target/s390x/cpu topology: activate CPU topology
[mirror_qemu.git] / target / hexagon / hex_common.py
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
20 import sys
21 import re
22 import string
23
24 behdict = {} # tag ->behavior
25 semdict = {} # tag -> semantics
26 attribdict = {} # tag -> attributes
27 macros = {} # macro -> macro information...
28 attribinfo = {} # Register information and misc
29 tags = [] # list of all tags
30 overrides = {} # tags with helper overrides
31 idef_parser_enabled = {} # tags enabled for idef-parser
32
33 def bad_register(regtype, regid):
34 raise Exception(f"Bad register parse: regtype '{regtype}' regid '{regid}'")
35
36 # We should do this as a hash for performance,
37 # but to keep order let's keep it as a list.
38 def uniquify(seq):
39 seen = set()
40 seen_add = seen.add
41 return [x for x in seq if x not in seen and not seen_add(x)]
42
43
44 regre = re.compile(r"((?<!DUP)[MNORCPQXSGVZA])([stuvwxyzdefg]+)([.]?[LlHh]?)(\d+S?)")
45 immre = re.compile(r"[#]([rRsSuUm])(\d+)(?:[:](\d+))?")
46 reg_or_immre = re.compile(
47 r"(((?<!DUP)[MNRCOPQXSGVZA])([stuvwxyzdefg]+)"
48 r"([.]?[LlHh]?)(\d+S?))|([#]([rRsSuUm])(\d+)[:]?(\d+)?)"
49 )
50 relimmre = re.compile(r"[#]([rR])(\d+)(?:[:](\d+))?")
51 absimmre = re.compile(r"[#]([sSuUm])(\d+)(?:[:](\d+))?")
52
53 finished_macros = set()
54
55
56 def expand_macro_attribs(macro, allmac_re):
57 if macro.key not in finished_macros:
58 # Get a list of all things that might be macros
59 l = allmac_re.findall(macro.beh)
60 for submacro in l:
61 if not submacro:
62 continue
63 if not macros[submacro]:
64 raise Exception(f"Couldn't find macro: <{l}>")
65 macro.attribs |= expand_macro_attribs(macros[submacro], allmac_re)
66 finished_macros.add(macro.key)
67 return macro.attribs
68
69
70 # When qemu needs an attribute that isn't in the imported files,
71 # we'll add it here.
72 def add_qemu_macro_attrib(name, attrib):
73 macros[name].attribs.add(attrib)
74
75
76 immextre = re.compile(r"f(MUST_)?IMMEXT[(]([UuSsRr])")
77
78
79 def is_cond_jump(tag):
80 if tag == "J2_rte":
81 return False
82 if "A_HWLOOP0_END" in attribdict[tag] or "A_HWLOOP1_END" in attribdict[tag]:
83 return False
84 return re.compile(r"(if.*fBRANCH)|(if.*fJUMPR)").search(semdict[tag]) != None
85
86
87 def is_cond_call(tag):
88 return re.compile(r"(if.*fCALL)").search(semdict[tag]) != None
89
90
91 def calculate_attribs():
92 add_qemu_macro_attrib("fREAD_PC", "A_IMPLICIT_READS_PC")
93 add_qemu_macro_attrib("fTRAP", "A_IMPLICIT_READS_PC")
94 add_qemu_macro_attrib("fWRITE_P0", "A_WRITES_PRED_REG")
95 add_qemu_macro_attrib("fWRITE_P1", "A_WRITES_PRED_REG")
96 add_qemu_macro_attrib("fWRITE_P2", "A_WRITES_PRED_REG")
97 add_qemu_macro_attrib("fWRITE_P3", "A_WRITES_PRED_REG")
98 add_qemu_macro_attrib("fSET_OVERFLOW", "A_IMPLICIT_WRITES_USR")
99 add_qemu_macro_attrib("fSET_LPCFG", "A_IMPLICIT_WRITES_USR")
100 add_qemu_macro_attrib("fLOAD", "A_SCALAR_LOAD")
101 add_qemu_macro_attrib("fSTORE", "A_SCALAR_STORE")
102 add_qemu_macro_attrib('fLSBNEW0', 'A_IMPLICIT_READS_P0')
103 add_qemu_macro_attrib('fLSBNEW0NOT', 'A_IMPLICIT_READS_P0')
104 add_qemu_macro_attrib('fREAD_P0', 'A_IMPLICIT_READS_P0')
105 add_qemu_macro_attrib('fLSBNEW1', 'A_IMPLICIT_READS_P1')
106 add_qemu_macro_attrib('fLSBNEW1NOT', 'A_IMPLICIT_READS_P1')
107 add_qemu_macro_attrib('fREAD_P3', 'A_IMPLICIT_READS_P3')
108
109 # Recurse down macros, find attributes from sub-macros
110 macroValues = list(macros.values())
111 allmacros_restr = "|".join(set([m.re.pattern for m in macroValues]))
112 allmacros_re = re.compile(allmacros_restr)
113 for macro in macroValues:
114 expand_macro_attribs(macro, allmacros_re)
115 # Append attributes to all instructions
116 for tag in tags:
117 for macname in allmacros_re.findall(semdict[tag]):
118 if not macname:
119 continue
120 macro = macros[macname]
121 attribdict[tag] |= set(macro.attribs)
122 # Figure out which instructions write predicate registers
123 tagregs = get_tagregs()
124 for tag in tags:
125 regs = tagregs[tag]
126 for regtype, regid in regs:
127 if regtype == "P" and is_written(regid):
128 attribdict[tag].add("A_WRITES_PRED_REG")
129 # Mark conditional jumps and calls
130 # Not all instructions are properly marked with A_CONDEXEC
131 for tag in tags:
132 if is_cond_jump(tag) or is_cond_call(tag):
133 attribdict[tag].add("A_CONDEXEC")
134
135
136 def SEMANTICS(tag, beh, sem):
137 # print tag,beh,sem
138 behdict[tag] = beh
139 semdict[tag] = sem
140 attribdict[tag] = set()
141 tags.append(tag) # dicts have no order, this is for order
142
143
144 def ATTRIBUTES(tag, attribstring):
145 attribstring = attribstring.replace("ATTRIBS", "").replace("(", "").replace(")", "")
146 if not attribstring:
147 return
148 attribs = attribstring.split(",")
149 for attrib in attribs:
150 attribdict[tag].add(attrib.strip())
151
152
153 class Macro(object):
154 __slots__ = ["key", "name", "beh", "attribs", "re"]
155
156 def __init__(self, name, beh, attribs):
157 self.key = name
158 self.name = name
159 self.beh = beh
160 self.attribs = set(attribs)
161 self.re = re.compile("\\b" + name + "\\b")
162
163
164 def MACROATTRIB(macname, beh, attribstring):
165 attribstring = attribstring.replace("(", "").replace(")", "")
166 if attribstring:
167 attribs = attribstring.split(",")
168 else:
169 attribs = []
170 macros[macname] = Macro(macname, beh, attribs)
171
172 def compute_tag_regs(tag, full):
173 tagregs = regre.findall(behdict[tag])
174 if not full:
175 tagregs = map(lambda reg: reg[:2], tagregs)
176 return uniquify(tagregs)
177
178 def compute_tag_immediates(tag):
179 return uniquify(immre.findall(behdict[tag]))
180
181
182 ##
183 ## tagregs is the main data structure we'll use
184 ## tagregs[tag] will contain the registers used by an instruction
185 ## Within each entry, we'll use the regtype and regid fields
186 ## regtype can be one of the following
187 ## C control register
188 ## N new register value
189 ## P predicate register
190 ## R GPR register
191 ## M modifier register
192 ## Q HVX predicate vector
193 ## V HVX vector register
194 ## O HVX new vector register
195 ## regid can be one of the following
196 ## d, e destination register
197 ## dd destination register pair
198 ## s, t, u, v, w source register
199 ## ss, tt, uu, vv source register pair
200 ## x, y read-write register
201 ## xx, yy read-write register pair
202 ##
203 def get_tagregs(full=False):
204 compute_func = lambda tag: compute_tag_regs(tag, full)
205 return dict(zip(tags, list(map(compute_func, tags))))
206
207 def get_tagimms():
208 return dict(zip(tags, list(map(compute_tag_immediates, tags))))
209
210
211 def is_pair(regid):
212 return len(regid) == 2
213
214
215 def is_single(regid):
216 return len(regid) == 1
217
218
219 def is_written(regid):
220 return regid[0] in "dexy"
221
222
223 def is_writeonly(regid):
224 return regid[0] in "de"
225
226
227 def is_read(regid):
228 return regid[0] in "stuvwxy"
229
230
231 def is_readwrite(regid):
232 return regid[0] in "xy"
233
234
235 def is_scalar_reg(regtype):
236 return regtype in "RPC"
237
238
239 def is_hvx_reg(regtype):
240 return regtype in "VQ"
241
242
243 def is_old_val(regtype, regid, tag):
244 return regtype + regid + "V" in semdict[tag]
245
246
247 def is_new_val(regtype, regid, tag):
248 return regtype + regid + "N" in semdict[tag]
249
250
251 def need_slot(tag):
252 if (
253 "A_CVI_SCATTER" not in attribdict[tag]
254 and "A_CVI_GATHER" not in attribdict[tag]
255 and ("A_STORE" in attribdict[tag]
256 or "A_LOAD" in attribdict[tag])
257 ):
258 return 1
259 else:
260 return 0
261
262
263 def need_part1(tag):
264 return re.compile(r"fPART1").search(semdict[tag])
265
266
267 def need_ea(tag):
268 return re.compile(r"\bEA\b").search(semdict[tag])
269
270
271 def need_PC(tag):
272 return "A_IMPLICIT_READS_PC" in attribdict[tag]
273
274
275 def helper_needs_next_PC(tag):
276 return "A_CALL" in attribdict[tag]
277
278
279 def need_pkt_has_multi_cof(tag):
280 return "A_COF" in attribdict[tag]
281
282
283 def need_pkt_need_commit(tag):
284 return 'A_IMPLICIT_WRITES_USR' in attribdict[tag]
285
286 def need_condexec_reg(tag, regs):
287 if "A_CONDEXEC" in attribdict[tag]:
288 for regtype, regid in regs:
289 if is_writeonly(regid) and not is_hvx_reg(regtype):
290 return True
291 return False
292
293
294 def skip_qemu_helper(tag):
295 return tag in overrides.keys()
296
297
298 def is_tmp_result(tag):
299 return "A_CVI_TMP" in attribdict[tag] or "A_CVI_TMP_DST" in attribdict[tag]
300
301
302 def is_new_result(tag):
303 return "A_CVI_NEW" in attribdict[tag]
304
305
306 def is_idef_parser_enabled(tag):
307 return tag in idef_parser_enabled
308
309
310 def imm_name(immlett):
311 return f"{immlett}iV"
312
313
314 def read_semantics_file(name):
315 eval_line = ""
316 for line in open(name, "rt").readlines():
317 if not line.startswith("#"):
318 eval_line += line
319 if line.endswith("\\\n"):
320 eval_line.rstrip("\\\n")
321 else:
322 eval(eval_line.strip())
323 eval_line = ""
324
325
326 def read_attribs_file(name):
327 attribre = re.compile(
328 r"DEF_ATTRIB\(([A-Za-z0-9_]+), ([^,]*), "
329 + r'"([A-Za-z0-9_\.]*)", "([A-Za-z0-9_\.]*)"\)'
330 )
331 for line in open(name, "rt").readlines():
332 if not attribre.match(line):
333 continue
334 (attrib_base, descr, rreg, wreg) = attribre.findall(line)[0]
335 attrib_base = "A_" + attrib_base
336 attribinfo[attrib_base] = {"rreg": rreg, "wreg": wreg, "descr": descr}
337
338
339 def read_overrides_file(name):
340 overridere = re.compile(r"#define fGEN_TCG_([A-Za-z0-9_]+)\(.*")
341 for line in open(name, "rt").readlines():
342 if not overridere.match(line):
343 continue
344 tag = overridere.findall(line)[0]
345 overrides[tag] = True
346
347
348 def read_idef_parser_enabled_file(name):
349 global idef_parser_enabled
350 with open(name, "r") as idef_parser_enabled_file:
351 lines = idef_parser_enabled_file.read().strip().split("\n")
352 idef_parser_enabled = set(lines)