xref: /qemu/target/hexagon/hex_common.py (revision a4696661)
1793958c9STaylor Simpson#!/usr/bin/env python3
2793958c9STaylor Simpson
3793958c9STaylor Simpson##
476eaa971STaylor Simpson##  Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
5793958c9STaylor Simpson##
6793958c9STaylor Simpson##  This program is free software; you can redistribute it and/or modify
7793958c9STaylor Simpson##  it under the terms of the GNU General Public License as published by
8793958c9STaylor Simpson##  the Free Software Foundation; either version 2 of the License, or
9793958c9STaylor Simpson##  (at your option) any later version.
10793958c9STaylor Simpson##
11793958c9STaylor Simpson##  This program is distributed in the hope that it will be useful,
12793958c9STaylor Simpson##  but WITHOUT ANY WARRANTY; without even the implied warranty of
13793958c9STaylor Simpson##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14793958c9STaylor Simpson##  GNU General Public License for more details.
15793958c9STaylor Simpson##
16793958c9STaylor Simpson##  You should have received a copy of the GNU General Public License
17793958c9STaylor Simpson##  along with this program; if not, see <http://www.gnu.org/licenses/>.
18793958c9STaylor Simpson##
19793958c9STaylor Simpson
20793958c9STaylor Simpsonimport sys
21793958c9STaylor Simpsonimport re
22793958c9STaylor Simpsonimport string
23b4478074STaylor Simpsonimport textwrap
24793958c9STaylor Simpson
25793958c9STaylor Simpsonbehdict = {}  # tag ->behavior
26793958c9STaylor Simpsonsemdict = {}  # tag -> semantics
27793958c9STaylor Simpsonattribdict = {}  # tag -> attributes
28793958c9STaylor Simpsonmacros = {}  # macro -> macro information...
29b4478074STaylor Simpsonregisters = {}  # register -> register functions
30b4478074STaylor Simpsonnew_registers = {}
31793958c9STaylor Simpsontags = []  # list of all tags
32793958c9STaylor Simpsonoverrides = {}  # tags with helper overrides
33e71fdc4fSAlessandro Di Federicoidef_parser_enabled = {}  # tags enabled for idef-parser
34793958c9STaylor Simpson
35793958c9STaylor Simpson# We should do this as a hash for performance,
36793958c9STaylor Simpson# but to keep order let's keep it as a list.
37793958c9STaylor Simpsondef uniquify(seq):
38793958c9STaylor Simpson    seen = set()
39793958c9STaylor Simpson    seen_add = seen.add
40793958c9STaylor Simpson    return [x for x in seq if x not in seen and not seen_add(x)]
41793958c9STaylor Simpson
425bb322e2SMarco Liebel
435bb322e2SMarco Liebelregre = re.compile(r"((?<!DUP)[MNORCPQXSGVZA])([stuvwxyzdefg]+)([.]?[LlHh]?)(\d+S?)")
44793958c9STaylor Simpsonimmre = re.compile(r"[#]([rRsSuUm])(\d+)(?:[:](\d+))?")
455bb322e2SMarco Liebelreg_or_immre = re.compile(
465bb322e2SMarco Liebel    r"(((?<!DUP)[MNRCOPQXSGVZA])([stuvwxyzdefg]+)"
47e41c40d1SPaolo Bonzini    r"([.]?[LlHh]?)(\d+S?))|([#]([rRsSuUm])(\d+)[:]?(\d+)?)"
485bb322e2SMarco Liebel)
49793958c9STaylor Simpsonrelimmre = re.compile(r"[#]([rR])(\d+)(?:[:](\d+))?")
50793958c9STaylor Simpsonabsimmre = re.compile(r"[#]([sSuUm])(\d+)(?:[:](\d+))?")
51793958c9STaylor Simpson
52793958c9STaylor Simpsonfinished_macros = set()
53793958c9STaylor Simpson
545bb322e2SMarco Liebel
55793958c9STaylor Simpsondef expand_macro_attribs(macro, allmac_re):
56793958c9STaylor Simpson    if macro.key not in finished_macros:
57793958c9STaylor Simpson        # Get a list of all things that might be macros
58793958c9STaylor Simpson        l = allmac_re.findall(macro.beh)
59793958c9STaylor Simpson        for submacro in l:
605bb322e2SMarco Liebel            if not submacro:
615bb322e2SMarco Liebel                continue
62793958c9STaylor Simpson            if not macros[submacro]:
63cd6c4edfSMarco Liebel                raise Exception(f"Couldn't find macro: <{l}>")
645bb322e2SMarco Liebel            macro.attribs |= expand_macro_attribs(macros[submacro], allmac_re)
65793958c9STaylor Simpson            finished_macros.add(macro.key)
66793958c9STaylor Simpson    return macro.attribs
67793958c9STaylor Simpson
685bb322e2SMarco Liebel
69793958c9STaylor Simpson# When qemu needs an attribute that isn't in the imported files,
70793958c9STaylor Simpson# we'll add it here.
71793958c9STaylor Simpsondef add_qemu_macro_attrib(name, attrib):
72793958c9STaylor Simpson    macros[name].attribs.add(attrib)
73793958c9STaylor Simpson
745bb322e2SMarco Liebel
755bb322e2SMarco Liebelimmextre = re.compile(r"f(MUST_)?IMMEXT[(]([UuSsRr])")
765bb322e2SMarco Liebel
77613653e5STaylor Simpson
78613653e5STaylor Simpsondef is_cond_jump(tag):
795bb322e2SMarco Liebel    if tag == "J2_rte":
80613653e5STaylor Simpson        return False
815bb322e2SMarco Liebel    if "A_HWLOOP0_END" in attribdict[tag] or "A_HWLOOP1_END" in attribdict[tag]:
82613653e5STaylor Simpson        return False
835bb322e2SMarco Liebel    return re.compile(r"(if.*fBRANCH)|(if.*fJUMPR)").search(semdict[tag]) != None
845bb322e2SMarco Liebel
85613653e5STaylor Simpson
86613653e5STaylor Simpsondef is_cond_call(tag):
87613653e5STaylor Simpson    return re.compile(r"(if.*fCALL)").search(semdict[tag]) != None
88613653e5STaylor Simpson
895bb322e2SMarco Liebel
90793958c9STaylor Simpsondef calculate_attribs():
915bb322e2SMarco Liebel    add_qemu_macro_attrib("fREAD_PC", "A_IMPLICIT_READS_PC")
925bb322e2SMarco Liebel    add_qemu_macro_attrib("fTRAP", "A_IMPLICIT_READS_PC")
935bb322e2SMarco Liebel    add_qemu_macro_attrib("fSET_OVERFLOW", "A_IMPLICIT_WRITES_USR")
945bb322e2SMarco Liebel    add_qemu_macro_attrib("fSET_LPCFG", "A_IMPLICIT_WRITES_USR")
955bb322e2SMarco Liebel    add_qemu_macro_attrib("fLOAD", "A_SCALAR_LOAD")
965bb322e2SMarco Liebel    add_qemu_macro_attrib("fSTORE", "A_SCALAR_STORE")
97b9f0326bSTaylor Simpson    add_qemu_macro_attrib('fLSBNEW0', 'A_IMPLICIT_READS_P0')
98b9f0326bSTaylor Simpson    add_qemu_macro_attrib('fLSBNEW0NOT', 'A_IMPLICIT_READS_P0')
99b9f0326bSTaylor Simpson    add_qemu_macro_attrib('fREAD_P0', 'A_IMPLICIT_READS_P0')
100b9f0326bSTaylor Simpson    add_qemu_macro_attrib('fLSBNEW1', 'A_IMPLICIT_READS_P1')
101b9f0326bSTaylor Simpson    add_qemu_macro_attrib('fLSBNEW1NOT', 'A_IMPLICIT_READS_P1')
102b9f0326bSTaylor Simpson    add_qemu_macro_attrib('fREAD_P3', 'A_IMPLICIT_READS_P3')
103f7be65fbSTaylor Simpson    add_qemu_macro_attrib('fREAD_SP', 'A_IMPLICIT_READS_SP')
104793958c9STaylor Simpson
105793958c9STaylor Simpson    # Recurse down macros, find attributes from sub-macros
106793958c9STaylor Simpson    macroValues = list(macros.values())
107793958c9STaylor Simpson    allmacros_restr = "|".join(set([m.re.pattern for m in macroValues]))
108793958c9STaylor Simpson    allmacros_re = re.compile(allmacros_restr)
109793958c9STaylor Simpson    for macro in macroValues:
110793958c9STaylor Simpson        expand_macro_attribs(macro, allmacros_re)
111793958c9STaylor Simpson    # Append attributes to all instructions
112793958c9STaylor Simpson    for tag in tags:
113793958c9STaylor Simpson        for macname in allmacros_re.findall(semdict[tag]):
1145bb322e2SMarco Liebel            if not macname:
1155bb322e2SMarco Liebel                continue
116793958c9STaylor Simpson            macro = macros[macname]
117793958c9STaylor Simpson            attribdict[tag] |= set(macro.attribs)
118613653e5STaylor Simpson    # Mark conditional jumps and calls
119613653e5STaylor Simpson    #     Not all instructions are properly marked with A_CONDEXEC
120613653e5STaylor Simpson    for tag in tags:
121613653e5STaylor Simpson        if is_cond_jump(tag) or is_cond_call(tag):
1225bb322e2SMarco Liebel            attribdict[tag].add("A_CONDEXEC")
1235bb322e2SMarco Liebel
124793958c9STaylor Simpson
125793958c9STaylor Simpsondef SEMANTICS(tag, beh, sem):
126793958c9STaylor Simpson    # print tag,beh,sem
127793958c9STaylor Simpson    behdict[tag] = beh
128793958c9STaylor Simpson    semdict[tag] = sem
129793958c9STaylor Simpson    attribdict[tag] = set()
130793958c9STaylor Simpson    tags.append(tag)  # dicts have no order, this is for order
131793958c9STaylor Simpson
1325bb322e2SMarco Liebel
133793958c9STaylor Simpsondef ATTRIBUTES(tag, attribstring):
1345bb322e2SMarco Liebel    attribstring = attribstring.replace("ATTRIBS", "").replace("(", "").replace(")", "")
135793958c9STaylor Simpson    if not attribstring:
136793958c9STaylor Simpson        return
137793958c9STaylor Simpson    attribs = attribstring.split(",")
138793958c9STaylor Simpson    for attrib in attribs:
139793958c9STaylor Simpson        attribdict[tag].add(attrib.strip())
140793958c9STaylor Simpson
1415bb322e2SMarco Liebel
142793958c9STaylor Simpsonclass Macro(object):
1435bb322e2SMarco Liebel    __slots__ = ["key", "name", "beh", "attribs", "re"]
1445bb322e2SMarco Liebel
145793958c9STaylor Simpson    def __init__(self, name, beh, attribs):
146793958c9STaylor Simpson        self.key = name
147793958c9STaylor Simpson        self.name = name
148793958c9STaylor Simpson        self.beh = beh
149793958c9STaylor Simpson        self.attribs = set(attribs)
150793958c9STaylor Simpson        self.re = re.compile("\\b" + name + "\\b")
151793958c9STaylor Simpson
1525bb322e2SMarco Liebel
153793958c9STaylor Simpsondef MACROATTRIB(macname, beh, attribstring):
154793958c9STaylor Simpson    attribstring = attribstring.replace("(", "").replace(")", "")
155793958c9STaylor Simpson    if attribstring:
156793958c9STaylor Simpson        attribs = attribstring.split(",")
157793958c9STaylor Simpson    else:
158793958c9STaylor Simpson        attribs = []
159793958c9STaylor Simpson    macros[macname] = Macro(macname, beh, attribs)
160793958c9STaylor Simpson
1613608c241SMatheus Tavares Bernardinodef compute_tag_regs(tag, full):
1623608c241SMatheus Tavares Bernardino    tagregs = regre.findall(behdict[tag])
1633608c241SMatheus Tavares Bernardino    if not full:
1643608c241SMatheus Tavares Bernardino        tagregs = map(lambda reg: reg[:2], tagregs)
1653608c241SMatheus Tavares Bernardino    return uniquify(tagregs)
1665bb322e2SMarco Liebel
167793958c9STaylor Simpsondef compute_tag_immediates(tag):
168793958c9STaylor Simpson    return uniquify(immre.findall(behdict[tag]))
169793958c9STaylor Simpson
1705bb322e2SMarco Liebel
171793958c9STaylor Simpson##
172793958c9STaylor Simpson##  tagregs is the main data structure we'll use
173793958c9STaylor Simpson##  tagregs[tag] will contain the registers used by an instruction
174793958c9STaylor Simpson##  Within each entry, we'll use the regtype and regid fields
175793958c9STaylor Simpson##      regtype can be one of the following
176793958c9STaylor Simpson##          C                control register
177793958c9STaylor Simpson##          N                new register value
178793958c9STaylor Simpson##          P                predicate register
179793958c9STaylor Simpson##          R                GPR register
180793958c9STaylor Simpson##          M                modifier register
181144da357STaylor Simpson##          Q                HVX predicate vector
182144da357STaylor Simpson##          V                HVX vector register
183144da357STaylor Simpson##          O                HVX new vector register
184793958c9STaylor Simpson##      regid can be one of the following
185793958c9STaylor Simpson##          d, e             destination register
186793958c9STaylor Simpson##          dd               destination register pair
187793958c9STaylor Simpson##          s, t, u, v, w    source register
188793958c9STaylor Simpson##          ss, tt, uu, vv   source register pair
189793958c9STaylor Simpson##          x, y             read-write register
190793958c9STaylor Simpson##          xx, yy           read-write register pair
191793958c9STaylor Simpson##
1923608c241SMatheus Tavares Bernardinodef get_tagregs(full=False):
1933608c241SMatheus Tavares Bernardino    compute_func = lambda tag: compute_tag_regs(tag, full)
1943608c241SMatheus Tavares Bernardino    return dict(zip(tags, list(map(compute_func, tags))))
1955bb322e2SMarco Liebel
196793958c9STaylor Simpsondef get_tagimms():
197793958c9STaylor Simpson    return dict(zip(tags, list(map(compute_tag_immediates, tags))))
198793958c9STaylor Simpson
1995bb322e2SMarco Liebel
200850d0622STaylor Simpsondef need_p0(tag):
201850d0622STaylor Simpson    return "A_IMPLICIT_READS_P0" in attribdict[tag]
202850d0622STaylor Simpson
203850d0622STaylor Simpson
204f7be65fbSTaylor Simpsondef need_sp(tag):
205f7be65fbSTaylor Simpson    return "A_IMPLICIT_READS_SP" in attribdict[tag]
206f7be65fbSTaylor Simpson
207f7be65fbSTaylor Simpson
2082f0a771dSTaylor Simpsondef is_hvx_insn(tag):
2092f0a771dSTaylor Simpson    return "A_CVI" in attribdict[tag]
2102f0a771dSTaylor Simpson
2112f0a771dSTaylor Simpson
2122f0a771dSTaylor Simpsondef need_env(tag):
2132f0a771dSTaylor Simpson    return ("A_STORE" in attribdict[tag] or
2142f0a771dSTaylor Simpson            "A_LOAD" in attribdict[tag] or
2152f0a771dSTaylor Simpson            "A_CVI_GATHER" in attribdict[tag] or
2162f0a771dSTaylor Simpson            "A_CVI_SCATTER" in attribdict[tag] or
2172f0a771dSTaylor Simpson            "A_IMPLICIT_WRITES_USR" in attribdict[tag])
2182f0a771dSTaylor Simpson
2192f0a771dSTaylor Simpson
220793958c9STaylor Simpsondef need_slot(tag):
2215bb322e2SMarco Liebel    if (
222e5d0d78dSTaylor Simpson        "A_CVI_SCATTER" not in attribdict[tag]
223e5d0d78dSTaylor Simpson        and "A_CVI_GATHER" not in attribdict[tag]
224e5d0d78dSTaylor Simpson        and ("A_STORE" in attribdict[tag]
225e5d0d78dSTaylor Simpson             or "A_LOAD" in attribdict[tag])
2265bb322e2SMarco Liebel    ):
227793958c9STaylor Simpson        return 1
228793958c9STaylor Simpson    else:
229793958c9STaylor Simpson        return 0
230793958c9STaylor Simpson
2315bb322e2SMarco Liebel
232793958c9STaylor Simpsondef need_part1(tag):
233793958c9STaylor Simpson    return re.compile(r"fPART1").search(semdict[tag])
234793958c9STaylor Simpson
2355bb322e2SMarco Liebel
236793958c9STaylor Simpsondef need_ea(tag):
237793958c9STaylor Simpson    return re.compile(r"\bEA\b").search(semdict[tag])
238793958c9STaylor Simpson
2395bb322e2SMarco Liebel
24040085901STaylor Simpsondef need_PC(tag):
2415bb322e2SMarco Liebel    return "A_IMPLICIT_READS_PC" in attribdict[tag]
2425bb322e2SMarco Liebel
24340085901STaylor Simpson
244b4478074STaylor Simpsondef need_next_PC(tag):
245b4478074STaylor Simpson    return "A_CALL" in attribdict[tag]
246b4478074STaylor Simpson
247b4478074STaylor Simpson
248fb67c2bfSTaylor Simpsondef need_pkt_has_multi_cof(tag):
2495bb322e2SMarco Liebel    return "A_COF" in attribdict[tag]
2505bb322e2SMarco Liebel
251fb67c2bfSTaylor Simpson
252d54c5615STaylor Simpsondef need_pkt_need_commit(tag):
253d54c5615STaylor Simpson    return 'A_IMPLICIT_WRITES_USR' in attribdict[tag]
254d54c5615STaylor Simpson
2555bb322e2SMarco Liebel
256793958c9STaylor Simpsondef skip_qemu_helper(tag):
257793958c9STaylor Simpson    return tag in overrides.keys()
258793958c9STaylor Simpson
2595bb322e2SMarco Liebel
260e71fdc4fSAlessandro Di Federicodef is_idef_parser_enabled(tag):
261e71fdc4fSAlessandro Di Federico    return tag in idef_parser_enabled
262e71fdc4fSAlessandro Di Federico
2635bb322e2SMarco Liebel
264763d2ce7STaylor Simpsondef is_hvx_insn(tag):
265763d2ce7STaylor Simpson    return "A_CVI" in attribdict[tag]
266763d2ce7STaylor Simpson
267763d2ce7STaylor Simpson
268763d2ce7STaylor Simpsondef has_hvx_helper(tag):
269763d2ce7STaylor Simpson    return (is_hvx_insn(tag) and
270763d2ce7STaylor Simpson            not skip_qemu_helper(tag) and
271763d2ce7STaylor Simpson            not is_idef_parser_enabled(tag))
272763d2ce7STaylor Simpson
273763d2ce7STaylor Simpson
274793958c9STaylor Simpsondef imm_name(immlett):
275cd6c4edfSMarco Liebel    return f"{immlett}iV"
276793958c9STaylor Simpson
2775bb322e2SMarco Liebel
278793958c9STaylor Simpsondef read_semantics_file(name):
279793958c9STaylor Simpson    eval_line = ""
2805bb322e2SMarco Liebel    for line in open(name, "rt").readlines():
281793958c9STaylor Simpson        if not line.startswith("#"):
282793958c9STaylor Simpson            eval_line += line
283793958c9STaylor Simpson            if line.endswith("\\\n"):
284793958c9STaylor Simpson                eval_line.rstrip("\\\n")
285793958c9STaylor Simpson            else:
286793958c9STaylor Simpson                eval(eval_line.strip())
287793958c9STaylor Simpson                eval_line = ""
288793958c9STaylor Simpson
2895bb322e2SMarco Liebel
290793958c9STaylor Simpsondef read_overrides_file(name):
291e41c40d1SPaolo Bonzini    overridere = re.compile(r"#define fGEN_TCG_([A-Za-z0-9_]+)\(.*")
2925bb322e2SMarco Liebel    for line in open(name, "rt").readlines():
293793958c9STaylor Simpson        if not overridere.match(line):
294793958c9STaylor Simpson            continue
295793958c9STaylor Simpson        tag = overridere.findall(line)[0]
296793958c9STaylor Simpson        overrides[tag] = True
297e71fdc4fSAlessandro Di Federico
2985bb322e2SMarco Liebel
299e71fdc4fSAlessandro Di Federicodef read_idef_parser_enabled_file(name):
300e71fdc4fSAlessandro Di Federico    global idef_parser_enabled
301e71fdc4fSAlessandro Di Federico    with open(name, "r") as idef_parser_enabled_file:
302e71fdc4fSAlessandro Di Federico        lines = idef_parser_enabled_file.read().strip().split("\n")
303e71fdc4fSAlessandro Di Federico        idef_parser_enabled = set(lines)
304b4478074STaylor Simpson
305b4478074STaylor Simpson
306b4478074STaylor Simpsondef is_predicated(tag):
307b4478074STaylor Simpson    return "A_CONDEXEC" in attribdict[tag]
308b4478074STaylor Simpson
309b4478074STaylor Simpson
310b4478074STaylor Simpsondef code_fmt(txt):
311b4478074STaylor Simpson    return textwrap.indent(textwrap.dedent(txt), "    ")
312b4478074STaylor Simpson
313b4478074STaylor Simpson
314b4478074STaylor Simpsondef hvx_newv(tag):
315b4478074STaylor Simpson    if "A_CVI_NEW" in attribdict[tag]:
316b4478074STaylor Simpson        return "EXT_NEW"
317b4478074STaylor Simpson    elif "A_CVI_TMP" in attribdict[tag] or "A_CVI_TMP_DST" in attribdict[tag]:
318b4478074STaylor Simpson        return "EXT_TMP"
319b4478074STaylor Simpson    else:
320b4478074STaylor Simpson        return "EXT_DFL"
321b4478074STaylor Simpson
322b4478074STaylor Simpsondef vreg_offset_func(tag):
323b4478074STaylor Simpson    if "A_CVI_TMP" in attribdict[tag] or "A_CVI_TMP_DST" in attribdict[tag]:
324b4478074STaylor Simpson        return "ctx_tmp_vreg_off"
325b4478074STaylor Simpson    else:
326b4478074STaylor Simpson        return "ctx_future_vreg_off"
327b4478074STaylor Simpson
328b4478074STaylor Simpsonclass HelperArg:
329b4478074STaylor Simpson    def __init__(self, proto_arg, call_arg, func_arg):
330b4478074STaylor Simpson        self.proto_arg = proto_arg
331b4478074STaylor Simpson        self.call_arg = call_arg
332b4478074STaylor Simpson        self.func_arg = func_arg
333b4478074STaylor Simpson
334b4478074STaylor Simpsonclass Register:
335b4478074STaylor Simpson    def __init__(self, regtype, regid):
336b4478074STaylor Simpson        self.regtype = regtype
337b4478074STaylor Simpson        self.regid = regid
338b4478074STaylor Simpson        self.reg_num = f"{regtype}{regid}N"
339b4478074STaylor Simpson    def decl_reg_num(self, f, regno):
340b4478074STaylor Simpson        f.write(code_fmt(f"""\
341b4478074STaylor Simpson            const int {self.reg_num} = insn->regno[{regno}];
342b4478074STaylor Simpson        """))
343b4478074STaylor Simpson    def idef_arg(self, declared):
344b4478074STaylor Simpson        declared.append(self.reg_tcg())
345b4478074STaylor Simpson    def helper_arg(self):
346b4478074STaylor Simpson        return HelperArg(
347b4478074STaylor Simpson            self.helper_proto_type(),
348b4478074STaylor Simpson            self.reg_tcg(),
349b4478074STaylor Simpson            f"{self.helper_arg_type()} {self.helper_arg_name()}"
350b4478074STaylor Simpson        )
351b4478074STaylor Simpson
352b4478074STaylor Simpson#
353b4478074STaylor Simpson# Every register is either Single or Pair or Hvx
354b4478074STaylor Simpson#
355b4478074STaylor Simpsonclass Scalar:
356b4478074STaylor Simpson    def is_scalar_reg(self):
357b4478074STaylor Simpson        return True
358b4478074STaylor Simpson    def is_hvx_reg(self):
359b4478074STaylor Simpson        return False
360b4478074STaylor Simpson    def helper_arg_name(self):
361b4478074STaylor Simpson        return self.reg_tcg()
362b4478074STaylor Simpson
363b4478074STaylor Simpsonclass Single(Scalar):
364b4478074STaylor Simpson    def helper_proto_type(self):
365b4478074STaylor Simpson        return "s32"
366b4478074STaylor Simpson    def helper_arg_type(self):
367b4478074STaylor Simpson        return "int32_t"
368b4478074STaylor Simpson
369b4478074STaylor Simpsonclass Pair(Scalar):
370b4478074STaylor Simpson    def helper_proto_type(self):
371b4478074STaylor Simpson        return "s64"
372b4478074STaylor Simpson    def helper_arg_type(self):
373b4478074STaylor Simpson        return "int64_t"
374b4478074STaylor Simpson
375b4478074STaylor Simpsonclass Hvx:
376b4478074STaylor Simpson    def is_scalar_reg(self):
377b4478074STaylor Simpson        return False
378b4478074STaylor Simpson    def is_hvx_reg(self):
379b4478074STaylor Simpson        return True
380b4478074STaylor Simpson    def hvx_off(self):
381b4478074STaylor Simpson        return f"{self.reg_tcg()}_off"
382b4478074STaylor Simpson    def helper_proto_type(self):
383b4478074STaylor Simpson        return "ptr"
384b4478074STaylor Simpson    def helper_arg_type(self):
385b4478074STaylor Simpson        return "void *"
386b4478074STaylor Simpson    def helper_arg_name(self):
387b4478074STaylor Simpson        return f"{self.reg_tcg()}_void"
388b4478074STaylor Simpson
389b4478074STaylor Simpson#
390b4478074STaylor Simpson# Every register is either Dest or OldSource or NewSource or ReadWrite
391b4478074STaylor Simpson#
392b4478074STaylor Simpsonclass Dest:
393b4478074STaylor Simpson    def reg_tcg(self):
394b4478074STaylor Simpson        return f"{self.regtype}{self.regid}V"
395b4478074STaylor Simpson    def is_written(self):
396b4478074STaylor Simpson        return True
397b4478074STaylor Simpson    def is_writeonly(self):
398b4478074STaylor Simpson        return True
399b4478074STaylor Simpson    def is_read(self):
400b4478074STaylor Simpson        return False
401b4478074STaylor Simpson    def is_readwrite(self):
402b4478074STaylor Simpson        return False
403b4478074STaylor Simpson
404b4478074STaylor Simpsonclass Source:
405b4478074STaylor Simpson    def is_written(self):
406b4478074STaylor Simpson        return False
407b4478074STaylor Simpson    def is_writeonly(self):
408b4478074STaylor Simpson        return False
409b4478074STaylor Simpson    def is_read(self):
410b4478074STaylor Simpson        return True
411b4478074STaylor Simpson    def is_readwrite(self):
412b4478074STaylor Simpson        return False
413b4478074STaylor Simpson
414b4478074STaylor Simpsonclass OldSource(Source):
415b4478074STaylor Simpson    def reg_tcg(self):
416b4478074STaylor Simpson        return f"{self.regtype}{self.regid}V"
4172720bd1dSTaylor Simpson    def is_old(self):
4182720bd1dSTaylor Simpson        return True
4192720bd1dSTaylor Simpson    def is_new(self):
4202720bd1dSTaylor Simpson        return False
421b4478074STaylor Simpson
422b4478074STaylor Simpsonclass NewSource(Source):
423b4478074STaylor Simpson    def reg_tcg(self):
424b4478074STaylor Simpson        return f"{self.regtype}{self.regid}N"
4252720bd1dSTaylor Simpson    def is_old(self):
4262720bd1dSTaylor Simpson        return False
4272720bd1dSTaylor Simpson    def is_new(self):
4282720bd1dSTaylor Simpson        return True
429b4478074STaylor Simpson
430b4478074STaylor Simpsonclass ReadWrite:
431b4478074STaylor Simpson    def reg_tcg(self):
432b4478074STaylor Simpson        return f"{self.regtype}{self.regid}V"
433b4478074STaylor Simpson    def is_written(self):
434b4478074STaylor Simpson        return True
435b4478074STaylor Simpson    def is_writeonly(self):
436b4478074STaylor Simpson        return False
437b4478074STaylor Simpson    def is_read(self):
438b4478074STaylor Simpson        return True
439b4478074STaylor Simpson    def is_readwrite(self):
440b4478074STaylor Simpson        return True
4412720bd1dSTaylor Simpson    def is_old(self):
4422720bd1dSTaylor Simpson        return True
4432720bd1dSTaylor Simpson    def is_new(self):
4442720bd1dSTaylor Simpson        return False
445b4478074STaylor Simpson
446b4478074STaylor Simpsonclass GprDest(Register, Single, Dest):
447b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
448b4478074STaylor Simpson        self.decl_reg_num(f, regno)
449b4478074STaylor Simpson        f.write(code_fmt(f"""\
450b4478074STaylor Simpson            TCGv {self.reg_tcg()} = get_result_gpr(ctx, {self.reg_num});
451b4478074STaylor Simpson        """))
452b4478074STaylor Simpson    def log_write(self, f, tag):
453b4478074STaylor Simpson        f.write(code_fmt(f"""\
454b4478074STaylor Simpson            gen_log_reg_write(ctx, {self.reg_num}, {self.reg_tcg()});
455b4478074STaylor Simpson        """))
45666fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
45766fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
45866fab981STaylor Simpson        f.write(code_fmt(f"""\
45966fab981STaylor Simpson            ctx_log_reg_write(ctx, {self.reg_num}, {predicated});
46066fab981STaylor Simpson        """))
461b4478074STaylor Simpson
462b4478074STaylor Simpsonclass GprSource(Register, Single, OldSource):
463b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
464b4478074STaylor Simpson        self.decl_reg_num(f, regno)
465b4478074STaylor Simpson        f.write(code_fmt(f"""\
466b4478074STaylor Simpson            TCGv {self.reg_tcg()} = hex_gpr[{self.reg_num}];
467b4478074STaylor Simpson        """))
46866fab981STaylor Simpson    def analyze_read(self, f, regno):
46966fab981STaylor Simpson        f.write(code_fmt(f"""\
47066fab981STaylor Simpson            ctx_log_reg_read(ctx, {self.reg_num});
47166fab981STaylor Simpson        """))
472b4478074STaylor Simpson
473b4478074STaylor Simpsonclass GprNewSource(Register, Single, NewSource):
474b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
475b4478074STaylor Simpson        f.write(code_fmt(f"""\
476b4478074STaylor Simpson            TCGv {self.reg_tcg()} = get_result_gpr(ctx, insn->regno[{regno}]);
477b4478074STaylor Simpson        """))
47866fab981STaylor Simpson    def analyze_read(self, f, regno):
47966fab981STaylor Simpson        f.write(code_fmt(f"""\
48076eaa971STaylor Simpson            ctx_log_reg_read_new(ctx, {self.reg_num});
48166fab981STaylor Simpson        """))
482b4478074STaylor Simpson
483b4478074STaylor Simpsonclass GprReadWrite(Register, Single, ReadWrite):
484b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
485b4478074STaylor Simpson        self.decl_reg_num(f, regno)
486b4478074STaylor Simpson        f.write(code_fmt(f"""\
487b4478074STaylor Simpson            TCGv {self.reg_tcg()} = get_result_gpr(ctx, {self.reg_num});
488b4478074STaylor Simpson        """))
489b4478074STaylor Simpson        ## For read/write registers, we need to get the original value into
490b4478074STaylor Simpson        ## the result TCGv.  For predicated instructions, this is done in
491b4478074STaylor Simpson        ## gen_start_packet.  For un-predicated instructions, we do it here.
492b4478074STaylor Simpson        if not is_predicated(tag):
493b4478074STaylor Simpson            f.write(code_fmt(f"""\
494b4478074STaylor Simpson                tcg_gen_mov_tl({self.reg_tcg()}, hex_gpr[{self.reg_num}]);
495b4478074STaylor Simpson            """))
496b4478074STaylor Simpson    def log_write(self, f, tag):
497b4478074STaylor Simpson        f.write(code_fmt(f"""\
498b4478074STaylor Simpson            gen_log_reg_write(ctx, {self.reg_num}, {self.reg_tcg()});
499b4478074STaylor Simpson        """))
50076eaa971STaylor Simpson    def analyze_read(self, f, regno):
50176eaa971STaylor Simpson        f.write(code_fmt(f"""\
50276eaa971STaylor Simpson            ctx_log_reg_read(ctx, {self.reg_num});
50376eaa971STaylor Simpson        """))
50466fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
50566fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
50666fab981STaylor Simpson        f.write(code_fmt(f"""\
50766fab981STaylor Simpson            ctx_log_reg_write(ctx, {self.reg_num}, {predicated});
50866fab981STaylor Simpson        """))
509b4478074STaylor Simpson
510b4478074STaylor Simpsonclass ControlDest(Register, Single, Dest):
511b4478074STaylor Simpson    def decl_reg_num(self, f, regno):
512b4478074STaylor Simpson        f.write(code_fmt(f"""\
513b4478074STaylor Simpson            const int {self.reg_num} = insn->regno[{regno}]  + HEX_REG_SA0;
514b4478074STaylor Simpson        """))
515b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
516b4478074STaylor Simpson        self.decl_reg_num(f, regno)
517b4478074STaylor Simpson        f.write(code_fmt(f"""\
518b4478074STaylor Simpson            TCGv {self.reg_tcg()} = get_result_gpr(ctx, {self.reg_num});
519b4478074STaylor Simpson        """))
520b4478074STaylor Simpson    def log_write(self, f, tag):
521b4478074STaylor Simpson        f.write(code_fmt(f"""\
522b4478074STaylor Simpson            gen_write_ctrl_reg(ctx, {self.reg_num}, {self.reg_tcg()});
523b4478074STaylor Simpson        """))
52466fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
52566fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
52666fab981STaylor Simpson        f.write(code_fmt(f"""\
52766fab981STaylor Simpson            ctx_log_reg_write(ctx, {self.reg_num}, {predicated});
52866fab981STaylor Simpson        """))
529b4478074STaylor Simpson
530b4478074STaylor Simpsonclass ControlSource(Register, Single, OldSource):
531b4478074STaylor Simpson    def decl_reg_num(self, f, regno):
532b4478074STaylor Simpson        f.write(code_fmt(f"""\
533b4478074STaylor Simpson            const int {self.reg_num} = insn->regno[{regno}]  + HEX_REG_SA0;
534b4478074STaylor Simpson        """))
535b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
536b4478074STaylor Simpson        self.decl_reg_num(f, regno);
537b4478074STaylor Simpson        f.write(code_fmt(f"""\
538b4478074STaylor Simpson            TCGv {self.reg_tcg()} = tcg_temp_new();
539b4478074STaylor Simpson            gen_read_ctrl_reg(ctx, {self.reg_num}, {self.reg_tcg()});
540b4478074STaylor Simpson        """))
54166fab981STaylor Simpson    def analyze_read(self, f, regno):
54266fab981STaylor Simpson        f.write(code_fmt(f"""\
54366fab981STaylor Simpson            ctx_log_reg_read(ctx, {self.reg_num});
54466fab981STaylor Simpson        """))
545b4478074STaylor Simpson
546b4478074STaylor Simpsonclass ModifierSource(Register, Single, OldSource):
547b4478074STaylor Simpson    def decl_reg_num(self, f, regno):
548b4478074STaylor Simpson        f.write(code_fmt(f"""\
549b4478074STaylor Simpson            const int {self.reg_num} = insn->regno[{regno}] + HEX_REG_M0;
550b4478074STaylor Simpson        """))
551b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
552b4478074STaylor Simpson        self.decl_reg_num(f, regno)
553b4478074STaylor Simpson        f.write(code_fmt(f"""\
554b4478074STaylor Simpson            TCGv {self.reg_tcg()} = hex_gpr[{self.reg_num}];
555b4478074STaylor Simpson            TCGv CS G_GNUC_UNUSED =
556b4478074STaylor Simpson                hex_gpr[{self.reg_num} - HEX_REG_M0 + HEX_REG_CS0];
557b4478074STaylor Simpson        """))
558b4478074STaylor Simpson    def idef_arg(self, declared):
559b4478074STaylor Simpson        declared.append(self.reg_tcg())
560b4478074STaylor Simpson        declared.append("CS")
56166fab981STaylor Simpson    def analyze_read(self, f, regno):
56266fab981STaylor Simpson        f.write(code_fmt(f"""\
56366fab981STaylor Simpson            ctx_log_reg_read(ctx, {self.reg_num});
56466fab981STaylor Simpson        """))
565b4478074STaylor Simpson
566b4478074STaylor Simpsonclass PredDest(Register, Single, Dest):
567b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
568b4478074STaylor Simpson        self.decl_reg_num(f, regno)
569b4478074STaylor Simpson        f.write(code_fmt(f"""\
570b4478074STaylor Simpson            TCGv {self.reg_tcg()} = tcg_temp_new();
571b4478074STaylor Simpson        """))
572b4478074STaylor Simpson    def log_write(self, f, tag):
573b4478074STaylor Simpson        f.write(code_fmt(f"""\
574b4478074STaylor Simpson            gen_log_pred_write(ctx, {self.reg_num}, {self.reg_tcg()});
575b4478074STaylor Simpson        """))
57666fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
57766fab981STaylor Simpson        f.write(code_fmt(f"""\
57866fab981STaylor Simpson            ctx_log_pred_write(ctx, {self.reg_num});
57966fab981STaylor Simpson        """))
580b4478074STaylor Simpson
581b4478074STaylor Simpsonclass PredSource(Register, Single, OldSource):
582b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
583b4478074STaylor Simpson        self.decl_reg_num(f, regno)
584b4478074STaylor Simpson        f.write(code_fmt(f"""\
585b4478074STaylor Simpson            TCGv {self.reg_tcg()} = hex_pred[{self.reg_num}];
586b4478074STaylor Simpson        """))
58766fab981STaylor Simpson    def analyze_read(self, f, regno):
58866fab981STaylor Simpson        f.write(code_fmt(f"""\
58966fab981STaylor Simpson            ctx_log_pred_read(ctx, {self.reg_num});
59066fab981STaylor Simpson        """))
591b4478074STaylor Simpson
592b4478074STaylor Simpsonclass PredNewSource(Register, Single, NewSource):
593b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
594b4478074STaylor Simpson        f.write(code_fmt(f"""\
595b4478074STaylor Simpson            TCGv {self.reg_tcg()} = get_result_pred(ctx, insn->regno[{regno}]);
596b4478074STaylor Simpson        """))
59766fab981STaylor Simpson    def analyze_read(self, f, regno):
59866fab981STaylor Simpson        f.write(code_fmt(f"""\
59976eaa971STaylor Simpson            ctx_log_pred_read_new(ctx, {self.reg_num});
60066fab981STaylor Simpson        """))
601b4478074STaylor Simpson
602b4478074STaylor Simpsonclass PredReadWrite(Register, Single, ReadWrite):
603b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
604b4478074STaylor Simpson        self.decl_reg_num(f, regno)
605b4478074STaylor Simpson        f.write(code_fmt(f"""\
606b4478074STaylor Simpson            TCGv {self.reg_tcg()} = tcg_temp_new();
607b4478074STaylor Simpson            tcg_gen_mov_tl({self.reg_tcg()}, hex_pred[{self.reg_num}]);
608b4478074STaylor Simpson        """))
609b4478074STaylor Simpson    def log_write(self, f, tag):
610b4478074STaylor Simpson        f.write(code_fmt(f"""\
611b4478074STaylor Simpson            gen_log_pred_write(ctx, {self.reg_num}, {self.reg_tcg()});
612b4478074STaylor Simpson        """))
61376eaa971STaylor Simpson    def analyze_read(self, f, regno):
61476eaa971STaylor Simpson        f.write(code_fmt(f"""\
61576eaa971STaylor Simpson            ctx_log_pred_read(ctx, {self.reg_num});
61676eaa971STaylor Simpson        """))
61766fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
61866fab981STaylor Simpson        f.write(code_fmt(f"""\
61966fab981STaylor Simpson            ctx_log_pred_write(ctx, {self.reg_num});
62066fab981STaylor Simpson        """))
621b4478074STaylor Simpson
622b4478074STaylor Simpsonclass PairDest(Register, Pair, Dest):
623b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
624b4478074STaylor Simpson        self.decl_reg_num(f, regno)
625b4478074STaylor Simpson        f.write(code_fmt(f"""\
626b4478074STaylor Simpson            TCGv_i64 {self.reg_tcg()} =
627b4478074STaylor Simpson                get_result_gpr_pair(ctx, {self.reg_num});
628b4478074STaylor Simpson        """))
629b4478074STaylor Simpson    def log_write(self, f, tag):
630b4478074STaylor Simpson        f.write(code_fmt(f"""\
631b4478074STaylor Simpson            gen_log_reg_write_pair(ctx, {self.reg_num}, {self.reg_tcg()});
632b4478074STaylor Simpson        """))
63366fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
63466fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
63566fab981STaylor Simpson        f.write(code_fmt(f"""\
63666fab981STaylor Simpson            ctx_log_reg_write_pair(ctx, {self.reg_num}, {predicated});
63766fab981STaylor Simpson        """))
638b4478074STaylor Simpson
639b4478074STaylor Simpsonclass PairSource(Register, Pair, OldSource):
640b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
641b4478074STaylor Simpson        self.decl_reg_num(f, regno)
642b4478074STaylor Simpson        f.write(code_fmt(f"""\
643b4478074STaylor Simpson            TCGv_i64 {self.reg_tcg()} = tcg_temp_new_i64();
644b4478074STaylor Simpson            tcg_gen_concat_i32_i64({self.reg_tcg()},
645b4478074STaylor Simpson                                    hex_gpr[{self.reg_num}],
646b4478074STaylor Simpson                                    hex_gpr[{self.reg_num} + 1]);
647b4478074STaylor Simpson        """))
64866fab981STaylor Simpson    def analyze_read(self, f, regno):
64966fab981STaylor Simpson        f.write(code_fmt(f"""\
65066fab981STaylor Simpson            ctx_log_reg_read_pair(ctx, {self.reg_num});
65166fab981STaylor Simpson        """))
652b4478074STaylor Simpson
653b4478074STaylor Simpsonclass PairReadWrite(Register, Pair, ReadWrite):
654b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
655b4478074STaylor Simpson        self.decl_reg_num(f, regno)
656b4478074STaylor Simpson        f.write(code_fmt(f"""\
657b4478074STaylor Simpson            TCGv_i64 {self.reg_tcg()} =
658b4478074STaylor Simpson                get_result_gpr_pair(ctx, {self.reg_num});
659b4478074STaylor Simpson            tcg_gen_concat_i32_i64({self.reg_tcg()},
660b4478074STaylor Simpson                                   hex_gpr[{self.reg_num}],
661b4478074STaylor Simpson                                   hex_gpr[{self.reg_num} + 1]);
662b4478074STaylor Simpson        """))
663b4478074STaylor Simpson    def log_write(self, f, tag):
664b4478074STaylor Simpson        f.write(code_fmt(f"""\
665b4478074STaylor Simpson            gen_log_reg_write_pair(ctx, {self.reg_num}, {self.reg_tcg()});
666b4478074STaylor Simpson        """))
66776eaa971STaylor Simpson    def analyze_read(self, f, regno):
66876eaa971STaylor Simpson        f.write(code_fmt(f"""\
66976eaa971STaylor Simpson            ctx_log_reg_read_pair(ctx, {self.reg_num});
67076eaa971STaylor Simpson        """))
67166fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
67266fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
67366fab981STaylor Simpson        f.write(code_fmt(f"""\
67466fab981STaylor Simpson            ctx_log_reg_write_pair(ctx, {self.reg_num}, {predicated});
67566fab981STaylor Simpson        """))
676b4478074STaylor Simpson
677b4478074STaylor Simpsonclass ControlPairDest(Register, Pair, Dest):
678b4478074STaylor Simpson    def decl_reg_num(self, f, regno):
679b4478074STaylor Simpson        f.write(code_fmt(f"""\
680b4478074STaylor Simpson            const int {self.reg_num} = insn->regno[{regno}] + HEX_REG_SA0;
681b4478074STaylor Simpson        """))
682b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
683b4478074STaylor Simpson        self.decl_reg_num(f, regno)
684b4478074STaylor Simpson        f.write(code_fmt(f"""\
685b4478074STaylor Simpson            TCGv_i64 {self.reg_tcg()} =
686b4478074STaylor Simpson                get_result_gpr_pair(ctx, {self.reg_num});
687b4478074STaylor Simpson        """))
688b4478074STaylor Simpson    def log_write(self, f, tag):
689b4478074STaylor Simpson        f.write(code_fmt(f"""\
690b4478074STaylor Simpson            gen_write_ctrl_reg_pair(ctx, {self.reg_num}, {self.reg_tcg()});
691b4478074STaylor Simpson        """))
69266fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
69366fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
69466fab981STaylor Simpson        f.write(code_fmt(f"""\
69566fab981STaylor Simpson            ctx_log_reg_write_pair(ctx, {self.reg_num}, {predicated});
69666fab981STaylor Simpson        """))
697b4478074STaylor Simpson
698b4478074STaylor Simpsonclass ControlPairSource(Register, Pair, OldSource):
699b4478074STaylor Simpson    def decl_reg_num(self, f, regno):
700b4478074STaylor Simpson        f.write(code_fmt(f"""\
701b4478074STaylor Simpson            const int {self.reg_num} = insn->regno[{regno}] + HEX_REG_SA0;
702b4478074STaylor Simpson        """))
703b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
704b4478074STaylor Simpson        self.decl_reg_num(f, regno)
705b4478074STaylor Simpson        f.write(code_fmt(f"""\
706b4478074STaylor Simpson            TCGv_i64 {self.reg_tcg()} = tcg_temp_new_i64();
707b4478074STaylor Simpson            gen_read_ctrl_reg_pair(ctx, {self.reg_num}, {self.reg_tcg()});
708b4478074STaylor Simpson        """))
70966fab981STaylor Simpson    def analyze_read(self, f, regno):
71066fab981STaylor Simpson        f.write(code_fmt(f"""\
71166fab981STaylor Simpson            ctx_log_reg_read_pair(ctx, {self.reg_num});
71266fab981STaylor Simpson        """))
713b4478074STaylor Simpson
714b4478074STaylor Simpsonclass VRegDest(Register, Hvx, Dest):
715b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
716b4478074STaylor Simpson        self.decl_reg_num(f, regno)
717b4478074STaylor Simpson        f.write(code_fmt(f"""\
718b4478074STaylor Simpson            const intptr_t {self.hvx_off()} =
719b4478074STaylor Simpson                {vreg_offset_func(tag)}(ctx, {self.reg_num}, 1, true);
720b4478074STaylor Simpson        """))
721b4478074STaylor Simpson        if not skip_qemu_helper(tag):
722b4478074STaylor Simpson            f.write(code_fmt(f"""\
723b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
724b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
725b4478074STaylor Simpson            """))
726b4478074STaylor Simpson    def log_write(self, f, tag):
727b4478074STaylor Simpson        pass
728a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
729a3295f54STaylor Simpson        f.write(code_fmt(f"""\
730a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMVector *)({self.helper_arg_name()}) */
731a3295f54STaylor Simpson        """))
73266fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
73366fab981STaylor Simpson        newv = hvx_newv(tag)
73466fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
73566fab981STaylor Simpson        f.write(code_fmt(f"""\
736763d2ce7STaylor Simpson            ctx_log_vreg_write(ctx, {self.reg_num}, {newv}, {predicated},
737763d2ce7STaylor Simpson                               insn_has_hvx_helper);
73866fab981STaylor Simpson        """))
739b4478074STaylor Simpson
740b4478074STaylor Simpsonclass VRegSource(Register, Hvx, OldSource):
741b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
742b4478074STaylor Simpson        self.decl_reg_num(f, regno)
743b4478074STaylor Simpson        f.write(code_fmt(f"""\
744b4478074STaylor Simpson            const intptr_t {self.hvx_off()} = vreg_src_off(ctx, {self.reg_num});
745b4478074STaylor Simpson        """))
746b4478074STaylor Simpson        if not skip_qemu_helper(tag):
747b4478074STaylor Simpson            f.write(code_fmt(f"""\
748b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
749b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
750b4478074STaylor Simpson            """))
751a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
752a3295f54STaylor Simpson        f.write(code_fmt(f"""\
753a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMVector *)({self.helper_arg_name()}) */
754a3295f54STaylor Simpson        """))
75566fab981STaylor Simpson    def analyze_read(self, f, regno):
75666fab981STaylor Simpson        f.write(code_fmt(f"""\
757763d2ce7STaylor Simpson            ctx_log_vreg_read(ctx, {self.reg_num}, insn_has_hvx_helper);
75866fab981STaylor Simpson        """))
759b4478074STaylor Simpson
760b4478074STaylor Simpsonclass VRegNewSource(Register, Hvx, NewSource):
761b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
762b4478074STaylor Simpson        self.decl_reg_num(f, regno)
763b4478074STaylor Simpson        if skip_qemu_helper(tag):
764b4478074STaylor Simpson            f.write(code_fmt(f"""\
765b4478074STaylor Simpson                const intptr_t {self.hvx_off()} =
766b4478074STaylor Simpson                    ctx_future_vreg_off(ctx, {self.reg_num}, 1, true);
767b4478074STaylor Simpson            """))
768a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
769a3295f54STaylor Simpson        f.write(code_fmt(f"""\
770a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMVector *)({self.helper_arg_name()}) */
771a3295f54STaylor Simpson        """))
77266fab981STaylor Simpson    def analyze_read(self, f, regno):
77366fab981STaylor Simpson        f.write(code_fmt(f"""\
774763d2ce7STaylor Simpson            ctx_log_vreg_read_new(ctx, {self.reg_num}, insn_has_hvx_helper);
77566fab981STaylor Simpson        """))
776b4478074STaylor Simpson
777b4478074STaylor Simpsonclass VRegReadWrite(Register, Hvx, ReadWrite):
778b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
779b4478074STaylor Simpson        self.decl_reg_num(f, regno)
780b4478074STaylor Simpson        f.write(code_fmt(f"""\
781b4478074STaylor Simpson            const intptr_t {self.hvx_off()} =
782b4478074STaylor Simpson                {vreg_offset_func(tag)}(ctx, {self.reg_num}, 1, true);
783b4478074STaylor Simpson            tcg_gen_gvec_mov(MO_64, {self.hvx_off()},
784b4478074STaylor Simpson                             vreg_src_off(ctx, {self.reg_num}),
785b4478074STaylor Simpson                             sizeof(MMVector), sizeof(MMVector));
786b4478074STaylor Simpson        """))
787b4478074STaylor Simpson        if not skip_qemu_helper(tag):
788b4478074STaylor Simpson            f.write(code_fmt(f"""\
789b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
790b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
791b4478074STaylor Simpson            """))
792b4478074STaylor Simpson    def log_write(self, f, tag):
793b4478074STaylor Simpson        pass
794a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
795a3295f54STaylor Simpson        f.write(code_fmt(f"""\
796a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMVector *)({self.helper_arg_name()}) */
797a3295f54STaylor Simpson        """))
79876eaa971STaylor Simpson    def analyze_read(self, f, regno):
79976eaa971STaylor Simpson        f.write(code_fmt(f"""\
800763d2ce7STaylor Simpson            ctx_log_vreg_read(ctx, {self.reg_num}, insn_has_hvx_helper);
80176eaa971STaylor Simpson        """))
80266fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
80366fab981STaylor Simpson        newv = hvx_newv(tag)
80466fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
80566fab981STaylor Simpson        f.write(code_fmt(f"""\
806763d2ce7STaylor Simpson            ctx_log_vreg_write(ctx, {self.reg_num}, {newv}, {predicated},
807763d2ce7STaylor Simpson                               insn_has_hvx_helper);
80866fab981STaylor Simpson        """))
809b4478074STaylor Simpson
810b4478074STaylor Simpsonclass VRegTmp(Register, Hvx, ReadWrite):
811b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
812b4478074STaylor Simpson        self.decl_reg_num(f, regno)
813b4478074STaylor Simpson        f.write(code_fmt(f"""\
814b4478074STaylor Simpson            const intptr_t {self.hvx_off()} = offsetof(CPUHexagonState, vtmp);
815b4478074STaylor Simpson        """))
816b4478074STaylor Simpson        if not skip_qemu_helper(tag):
817b4478074STaylor Simpson            f.write(code_fmt(f"""\
818b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
819b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
820b4478074STaylor Simpson                tcg_gen_gvec_mov(MO_64, {self.hvx_off()},
821b4478074STaylor Simpson                                 vreg_src_off(ctx, {self.reg_num}),
822b4478074STaylor Simpson                                 sizeof(MMVector), sizeof(MMVector));
823b4478074STaylor Simpson            """))
824b4478074STaylor Simpson    def log_write(self, f, tag):
825b4478074STaylor Simpson        f.write(code_fmt(f"""\
826b4478074STaylor Simpson            gen_log_vreg_write(ctx, {self.hvx_off()}, {self.reg_num},
827b4478074STaylor Simpson                               {hvx_newv(tag)});
828b4478074STaylor Simpson        """))
829a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
830a3295f54STaylor Simpson        f.write(code_fmt(f"""\
831a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMVector *)({self.helper_arg_name()}) */
832a3295f54STaylor Simpson        """))
83376eaa971STaylor Simpson    def analyze_read(self, f, regno):
83476eaa971STaylor Simpson        f.write(code_fmt(f"""\
835763d2ce7STaylor Simpson            ctx_log_vreg_read(ctx, {self.reg_num}, insn_has_hvx_helper);
83676eaa971STaylor Simpson        """))
83766fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
83866fab981STaylor Simpson        newv = hvx_newv(tag)
83966fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
84066fab981STaylor Simpson        f.write(code_fmt(f"""\
841763d2ce7STaylor Simpson            ctx_log_vreg_write(ctx, {self.reg_num}, {newv}, {predicated},
842763d2ce7STaylor Simpson                               insn_has_hvx_helper);
84366fab981STaylor Simpson        """))
844b4478074STaylor Simpson
845b4478074STaylor Simpsonclass VRegPairDest(Register, Hvx, Dest):
846b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
847b4478074STaylor Simpson        self.decl_reg_num(f, regno)
848b4478074STaylor Simpson        f.write(code_fmt(f"""\
849b4478074STaylor Simpson            const intptr_t {self.hvx_off()} =
850b4478074STaylor Simpson                {vreg_offset_func(tag)}(ctx, {self.reg_num}, 2, true);
851b4478074STaylor Simpson        """))
852b4478074STaylor Simpson        if not skip_qemu_helper(tag):
853b4478074STaylor Simpson            f.write(code_fmt(f"""\
854b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
855b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
856b4478074STaylor Simpson            """))
857b4478074STaylor Simpson    def log_write(self, f, tag):
858b4478074STaylor Simpson        pass
859a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
860a3295f54STaylor Simpson        f.write(code_fmt(f"""\
861a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMVectorPair *)({self.helper_arg_name()}) */
862a3295f54STaylor Simpson        """))
86366fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
86466fab981STaylor Simpson        newv = hvx_newv(tag)
86566fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
86666fab981STaylor Simpson        f.write(code_fmt(f"""\
867763d2ce7STaylor Simpson            ctx_log_vreg_write_pair(ctx, {self.reg_num}, {newv}, {predicated},
868763d2ce7STaylor Simpson                                    insn_has_hvx_helper);
86966fab981STaylor Simpson        """))
870b4478074STaylor Simpson
871b4478074STaylor Simpsonclass VRegPairSource(Register, Hvx, OldSource):
872b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
873b4478074STaylor Simpson        self.decl_reg_num(f, regno)
874b4478074STaylor Simpson        f.write(code_fmt(f"""\
875b4478074STaylor Simpson            const intptr_t {self.hvx_off()} =
876b4478074STaylor Simpson                offsetof(CPUHexagonState, {self.reg_tcg()});
877b4478074STaylor Simpson            tcg_gen_gvec_mov(MO_64, {self.hvx_off()},
878b4478074STaylor Simpson                             vreg_src_off(ctx, {self.reg_num}),
879b4478074STaylor Simpson                             sizeof(MMVector), sizeof(MMVector));
880b4478074STaylor Simpson            tcg_gen_gvec_mov(MO_64, {self.hvx_off()} + sizeof(MMVector),
881b4478074STaylor Simpson                             vreg_src_off(ctx, {self.reg_num} ^ 1),
882b4478074STaylor Simpson                             sizeof(MMVector), sizeof(MMVector));
883b4478074STaylor Simpson        """))
884b4478074STaylor Simpson        if not skip_qemu_helper(tag):
885b4478074STaylor Simpson            f.write(code_fmt(f"""\
886b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
887b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
888b4478074STaylor Simpson            """))
889a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
890a3295f54STaylor Simpson        f.write(code_fmt(f"""\
891a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMVectorPair *)({self.helper_arg_name()}) */
892a3295f54STaylor Simpson        """))
89366fab981STaylor Simpson    def analyze_read(self, f, regno):
89466fab981STaylor Simpson        f.write(code_fmt(f"""\
895763d2ce7STaylor Simpson            ctx_log_vreg_read_pair(ctx, {self.reg_num}, insn_has_hvx_helper);
89666fab981STaylor Simpson        """))
897b4478074STaylor Simpson
898b4478074STaylor Simpsonclass VRegPairReadWrite(Register, Hvx, ReadWrite):
899b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
900b4478074STaylor Simpson        self.decl_reg_num(f, regno)
901b4478074STaylor Simpson        f.write(code_fmt(f"""\
902b4478074STaylor Simpson            const intptr_t {self.hvx_off()} =
903b4478074STaylor Simpson                offsetof(CPUHexagonState, {self.reg_tcg()});
904b4478074STaylor Simpson            tcg_gen_gvec_mov(MO_64, {self.hvx_off()},
905b4478074STaylor Simpson                             vreg_src_off(ctx, {self.reg_num}),
906b4478074STaylor Simpson                             sizeof(MMVector), sizeof(MMVector));
907b4478074STaylor Simpson            tcg_gen_gvec_mov(MO_64, {self.hvx_off()} + sizeof(MMVector),
908b4478074STaylor Simpson                             vreg_src_off(ctx, {self.reg_num} ^ 1),
909b4478074STaylor Simpson                             sizeof(MMVector), sizeof(MMVector));
910b4478074STaylor Simpson        """))
911b4478074STaylor Simpson        if not skip_qemu_helper(tag):
912b4478074STaylor Simpson            f.write(code_fmt(f"""\
913b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
914b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
915b4478074STaylor Simpson            """))
916b4478074STaylor Simpson    def log_write(self, f, tag):
917b4478074STaylor Simpson        f.write(code_fmt(f"""\
918b4478074STaylor Simpson            gen_log_vreg_write_pair(ctx, {self.hvx_off()}, {self.reg_num},
919b4478074STaylor Simpson                                    {hvx_newv(tag)});
920b4478074STaylor Simpson        """))
921a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
922a3295f54STaylor Simpson        f.write(code_fmt(f"""\
923a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMVectorPair *)({self.helper_arg_name()}) */
924a3295f54STaylor Simpson        """))
92576eaa971STaylor Simpson    def analyze_read(self, f, regno):
92676eaa971STaylor Simpson        f.write(code_fmt(f"""\
927763d2ce7STaylor Simpson            ctx_log_vreg_read_pair(ctx, {self.reg_num}, insn_has_hvx_helper);
92876eaa971STaylor Simpson        """))
92966fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
93066fab981STaylor Simpson        newv = hvx_newv(tag)
93166fab981STaylor Simpson        predicated = "true" if is_predicated(tag) else "false"
93266fab981STaylor Simpson        f.write(code_fmt(f"""\
933763d2ce7STaylor Simpson            ctx_log_vreg_write_pair(ctx, {self.reg_num}, {newv}, {predicated},
934763d2ce7STaylor Simpson                                    insn_has_hvx_helper);
93566fab981STaylor Simpson        """))
936b4478074STaylor Simpson
937b4478074STaylor Simpsonclass QRegDest(Register, Hvx, Dest):
938b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
939b4478074STaylor Simpson        self.decl_reg_num(f, regno)
940b4478074STaylor Simpson        f.write(code_fmt(f"""\
941b4478074STaylor Simpson            const intptr_t {self.hvx_off()} =
942b4478074STaylor Simpson                get_result_qreg(ctx, {self.reg_num});
943b4478074STaylor Simpson        """))
944b4478074STaylor Simpson        if not skip_qemu_helper(tag):
945b4478074STaylor Simpson            f.write(code_fmt(f"""\
946b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
947b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
948b4478074STaylor Simpson            """))
949b4478074STaylor Simpson    def log_write(self, f, tag):
950b4478074STaylor Simpson        pass
951a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
952a3295f54STaylor Simpson        f.write(code_fmt(f"""\
953a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMQReg *)({self.helper_arg_name()}) */
954a3295f54STaylor Simpson        """))
95566fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
95666fab981STaylor Simpson        f.write(code_fmt(f"""\
957763d2ce7STaylor Simpson            ctx_log_qreg_write(ctx, {self.reg_num}, insn_has_hvx_helper);
95866fab981STaylor Simpson        """))
959b4478074STaylor Simpson
960b4478074STaylor Simpsonclass QRegSource(Register, Hvx, OldSource):
961b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
962b4478074STaylor Simpson        self.decl_reg_num(f, regno)
963b4478074STaylor Simpson        f.write(code_fmt(f"""\
964b4478074STaylor Simpson            const intptr_t {self.hvx_off()} =
965b4478074STaylor Simpson                offsetof(CPUHexagonState, QRegs[{self.reg_num}]);
966b4478074STaylor Simpson        """))
967b4478074STaylor Simpson        if not skip_qemu_helper(tag):
968b4478074STaylor Simpson            f.write(code_fmt(f"""\
969b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
970b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
971b4478074STaylor Simpson            """))
972a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
973a3295f54STaylor Simpson        f.write(code_fmt(f"""\
974a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMQReg *)({self.helper_arg_name()}) */
975a3295f54STaylor Simpson        """))
97666fab981STaylor Simpson    def analyze_read(self, f, regno):
97766fab981STaylor Simpson        f.write(code_fmt(f"""\
978763d2ce7STaylor Simpson            ctx_log_qreg_read(ctx, {self.reg_num}, insn_has_hvx_helper);
97966fab981STaylor Simpson        """))
980b4478074STaylor Simpson
981b4478074STaylor Simpsonclass QRegReadWrite(Register, Hvx, ReadWrite):
982b4478074STaylor Simpson    def decl_tcg(self, f, tag, regno):
983b4478074STaylor Simpson        self.decl_reg_num(f, regno)
984b4478074STaylor Simpson        f.write(code_fmt(f"""\
985b4478074STaylor Simpson            const intptr_t {self.hvx_off()} =
986b4478074STaylor Simpson                get_result_qreg(ctx, {self.reg_num});
987b4478074STaylor Simpson            tcg_gen_gvec_mov(MO_64, {self.hvx_off()},
988b4478074STaylor Simpson                             offsetof(CPUHexagonState, QRegs[{self.reg_num}]),
989b4478074STaylor Simpson                             sizeof(MMQReg), sizeof(MMQReg));
990b4478074STaylor Simpson        """))
991b4478074STaylor Simpson        if not skip_qemu_helper(tag):
992b4478074STaylor Simpson            f.write(code_fmt(f"""\
993b4478074STaylor Simpson                TCGv_ptr {self.reg_tcg()} = tcg_temp_new_ptr();
994b4478074STaylor Simpson                tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off()});
995b4478074STaylor Simpson            """))
996b4478074STaylor Simpson    def log_write(self, f, tag):
997b4478074STaylor Simpson        pass
998a3295f54STaylor Simpson    def helper_hvx_desc(self, f):
999a3295f54STaylor Simpson        f.write(code_fmt(f"""\
1000a3295f54STaylor Simpson            /* {self.reg_tcg()} is *(MMQReg *)({self.helper_arg_name()}) */
1001a3295f54STaylor Simpson        """))
100276eaa971STaylor Simpson    def analyze_read(self, f, regno):
100376eaa971STaylor Simpson        f.write(code_fmt(f"""\
1004763d2ce7STaylor Simpson            ctx_log_qreg_read(ctx, {self.reg_num}, insn_has_hvx_helper);
100576eaa971STaylor Simpson        """))
100666fab981STaylor Simpson    def analyze_write(self, f, tag, regno):
100766fab981STaylor Simpson        f.write(code_fmt(f"""\
1008763d2ce7STaylor Simpson            ctx_log_qreg_write(ctx, {self.reg_num}, insn_has_hvx_helper);
100966fab981STaylor Simpson        """))
1010b4478074STaylor Simpson
1011b4478074STaylor Simpsondef init_registers():
1012b4478074STaylor Simpson    regs = {
1013b4478074STaylor Simpson        GprDest("R", "d"),
1014b4478074STaylor Simpson        GprDest("R", "e"),
1015b4478074STaylor Simpson        GprSource("R", "s"),
1016b4478074STaylor Simpson        GprSource("R", "t"),
1017b4478074STaylor Simpson        GprSource("R", "u"),
1018b4478074STaylor Simpson        GprSource("R", "v"),
1019b4478074STaylor Simpson        GprReadWrite("R", "x"),
1020b4478074STaylor Simpson        GprReadWrite("R", "y"),
1021b4478074STaylor Simpson        ControlDest("C", "d"),
1022b4478074STaylor Simpson        ControlSource("C", "s"),
1023b4478074STaylor Simpson        ModifierSource("M", "u"),
1024b4478074STaylor Simpson        PredDest("P", "d"),
1025b4478074STaylor Simpson        PredDest("P", "e"),
1026b4478074STaylor Simpson        PredSource("P", "s"),
1027b4478074STaylor Simpson        PredSource("P", "t"),
1028b4478074STaylor Simpson        PredSource("P", "u"),
1029b4478074STaylor Simpson        PredSource("P", "v"),
1030b4478074STaylor Simpson        PredReadWrite("P", "x"),
1031b4478074STaylor Simpson        PairDest("R", "dd"),
1032b4478074STaylor Simpson        PairDest("R", "ee"),
1033b4478074STaylor Simpson        PairSource("R", "ss"),
1034b4478074STaylor Simpson        PairSource("R", "tt"),
1035b4478074STaylor Simpson        PairReadWrite("R", "xx"),
1036b4478074STaylor Simpson        PairReadWrite("R", "yy"),
1037b4478074STaylor Simpson        ControlPairDest("C", "dd"),
1038b4478074STaylor Simpson        ControlPairSource("C", "ss"),
1039b4478074STaylor Simpson        VRegDest("V", "d"),
1040b4478074STaylor Simpson        VRegSource("V", "s"),
1041b4478074STaylor Simpson        VRegSource("V", "u"),
1042b4478074STaylor Simpson        VRegSource("V", "v"),
1043b4478074STaylor Simpson        VRegSource("V", "w"),
1044b4478074STaylor Simpson        VRegReadWrite("V", "x"),
1045b4478074STaylor Simpson        VRegTmp("V", "y"),
1046b4478074STaylor Simpson        VRegPairDest("V", "dd"),
1047b4478074STaylor Simpson        VRegPairSource("V", "uu"),
1048b4478074STaylor Simpson        VRegPairSource("V", "vv"),
1049b4478074STaylor Simpson        VRegPairReadWrite("V", "xx"),
1050b4478074STaylor Simpson        QRegDest("Q", "d"),
1051b4478074STaylor Simpson        QRegDest("Q", "e"),
1052b4478074STaylor Simpson        QRegSource("Q", "s"),
1053b4478074STaylor Simpson        QRegSource("Q", "t"),
1054b4478074STaylor Simpson        QRegSource("Q", "u"),
1055b4478074STaylor Simpson        QRegSource("Q", "v"),
1056b4478074STaylor Simpson        QRegReadWrite("Q", "x"),
1057b4478074STaylor Simpson    }
1058b4478074STaylor Simpson    for reg in regs:
1059b4478074STaylor Simpson        registers[f"{reg.regtype}{reg.regid}"] = reg
1060b4478074STaylor Simpson
1061b4478074STaylor Simpson    new_regs = {
1062b4478074STaylor Simpson        GprNewSource("N", "s"),
1063b4478074STaylor Simpson        GprNewSource("N", "t"),
1064b4478074STaylor Simpson        PredNewSource("P", "t"),
1065b4478074STaylor Simpson        PredNewSource("P", "u"),
1066b4478074STaylor Simpson        PredNewSource("P", "v"),
1067b4478074STaylor Simpson        VRegNewSource("O", "s"),
1068b4478074STaylor Simpson    }
1069b4478074STaylor Simpson    for reg in new_regs:
1070b4478074STaylor Simpson        new_registers[f"{reg.regtype}{reg.regid}"] = reg
1071b4478074STaylor Simpson
1072b4478074STaylor Simpsondef get_register(tag, regtype, regid):
1073b4478074STaylor Simpson    if f"{regtype}{regid}V" in semdict[tag]:
1074b4478074STaylor Simpson        return registers[f"{regtype}{regid}"]
1075b4478074STaylor Simpson    else:
1076b4478074STaylor Simpson        return new_registers[f"{regtype}{regid}"]
1077b4478074STaylor Simpson
1078b4478074STaylor Simpsondef helper_ret_type(tag, regs):
1079b4478074STaylor Simpson    ## If there is a scalar result, it is the return type
1080b4478074STaylor Simpson    return_type = HelperArg( "void", "void", "void")
1081b4478074STaylor Simpson    numscalarresults = 0
1082b4478074STaylor Simpson    for regtype, regid in regs:
1083b4478074STaylor Simpson        reg = get_register(tag, regtype, regid)
1084b4478074STaylor Simpson        if reg.is_written() and reg.is_scalar_reg():
1085b4478074STaylor Simpson            return_type = HelperArg(
1086b4478074STaylor Simpson                reg.helper_proto_type(),
1087b4478074STaylor Simpson                reg.reg_tcg(),
1088b4478074STaylor Simpson                reg.helper_arg_type()
1089b4478074STaylor Simpson            )
1090b4478074STaylor Simpson    if numscalarresults > 1:
1091b4478074STaylor Simpson        raise Exception("numscalarresults > 1")
1092b4478074STaylor Simpson    return return_type
1093b4478074STaylor Simpson
1094b4478074STaylor Simpsondef helper_args(tag, regs, imms):
1095b4478074STaylor Simpson    args = []
1096b4478074STaylor Simpson
1097b4478074STaylor Simpson    ## First argument is the CPU state
10982f0a771dSTaylor Simpson    if need_env(tag):
1099b4478074STaylor Simpson        args.append(HelperArg(
1100b4478074STaylor Simpson            "env",
1101b4478074STaylor Simpson            "tcg_env",
1102b4478074STaylor Simpson            "CPUHexagonState *env"
1103b4478074STaylor Simpson        ))
1104b4478074STaylor Simpson
1105b4478074STaylor Simpson    ## For predicated instructions, we pass in the destination register
1106b4478074STaylor Simpson    if is_predicated(tag):
1107b4478074STaylor Simpson        for regtype, regid in regs:
1108b4478074STaylor Simpson            reg = get_register(tag, regtype, regid)
1109b4478074STaylor Simpson            if reg.is_writeonly() and not reg.is_hvx_reg():
1110b4478074STaylor Simpson                args.append(reg.helper_arg())
1111b4478074STaylor Simpson
1112b4478074STaylor Simpson    ## Pass the HVX destination registers
1113b4478074STaylor Simpson    for regtype, regid in regs:
1114b4478074STaylor Simpson        reg = get_register(tag, regtype, regid)
1115b4478074STaylor Simpson        if reg.is_written() and reg.is_hvx_reg():
1116b4478074STaylor Simpson            args.append(reg.helper_arg())
1117b4478074STaylor Simpson
1118b4478074STaylor Simpson    ## Pass the source registers
1119b4478074STaylor Simpson    for regtype, regid in regs:
1120b4478074STaylor Simpson        reg = get_register(tag, regtype, regid)
1121b4478074STaylor Simpson        if reg.is_read() and not (reg.is_hvx_reg() and reg.is_readwrite()):
1122b4478074STaylor Simpson            args.append(reg.helper_arg())
1123b4478074STaylor Simpson
1124b4478074STaylor Simpson    ## Pass the immediates
1125b4478074STaylor Simpson    for immlett, bits, immshift in imms:
1126b4478074STaylor Simpson        args.append(HelperArg(
1127b4478074STaylor Simpson            "s32",
1128b4478074STaylor Simpson            f"tcg_constant_tl({imm_name(immlett)})",
1129b4478074STaylor Simpson            f"int32_t {imm_name(immlett)}"
1130b4478074STaylor Simpson        ))
1131b4478074STaylor Simpson
1132b4478074STaylor Simpson    ## Other stuff the helper might need
1133b4478074STaylor Simpson    if need_pkt_has_multi_cof(tag):
1134b4478074STaylor Simpson        args.append(HelperArg(
1135b4478074STaylor Simpson            "i32",
1136b4478074STaylor Simpson            "tcg_constant_tl(ctx->pkt->pkt_has_multi_cof)",
1137b4478074STaylor Simpson            "uint32_t pkt_has_multi_cof"
1138b4478074STaylor Simpson        ))
1139b4478074STaylor Simpson    if need_pkt_need_commit(tag):
1140b4478074STaylor Simpson        args.append(HelperArg(
1141b4478074STaylor Simpson            "i32",
1142b4478074STaylor Simpson            "tcg_constant_tl(ctx->need_commit)",
1143b4478074STaylor Simpson            "uint32_t pkt_need_commit"
1144b4478074STaylor Simpson        ))
1145b4478074STaylor Simpson    if need_PC(tag):
1146b4478074STaylor Simpson        args.append(HelperArg(
1147b4478074STaylor Simpson            "i32",
1148b4478074STaylor Simpson            "tcg_constant_tl(ctx->pkt->pc)",
1149b4478074STaylor Simpson            "target_ulong PC"
1150b4478074STaylor Simpson        ))
1151b4478074STaylor Simpson    if need_next_PC(tag):
1152b4478074STaylor Simpson        args.append(HelperArg(
1153b4478074STaylor Simpson            "i32",
1154b4478074STaylor Simpson            "tcg_constant_tl(ctx->next_PC)",
1155b4478074STaylor Simpson            "target_ulong next_PC"
1156b4478074STaylor Simpson        ))
1157850d0622STaylor Simpson    if need_p0(tag):
1158850d0622STaylor Simpson        args.append(HelperArg(
1159850d0622STaylor Simpson            "i32",
1160850d0622STaylor Simpson            "hex_pred[0]",
1161850d0622STaylor Simpson            "uint32_t P0"
1162850d0622STaylor Simpson        ))
1163f7be65fbSTaylor Simpson    if need_sp(tag):
1164f7be65fbSTaylor Simpson        args.append(HelperArg(
1165f7be65fbSTaylor Simpson            "i32",
1166f7be65fbSTaylor Simpson            "hex_gpr[HEX_REG_SP]",
1167f7be65fbSTaylor Simpson            "uint32_t SP"
1168f7be65fbSTaylor Simpson        ))
1169b4478074STaylor Simpson    if need_slot(tag):
1170b4478074STaylor Simpson        args.append(HelperArg(
1171b4478074STaylor Simpson            "i32",
1172b4478074STaylor Simpson            "gen_slotval(ctx)",
1173b4478074STaylor Simpson            "uint32_t slotval"
1174b4478074STaylor Simpson        ))
1175b4478074STaylor Simpson    if need_part1(tag):
1176b4478074STaylor Simpson        args.append(HelperArg(
1177b4478074STaylor Simpson            "i32",
1178b4478074STaylor Simpson            "tcg_constant_tl(insn->part1)"
1179b4478074STaylor Simpson            "uint32_t part1"
1180b4478074STaylor Simpson        ))
1181b4478074STaylor Simpson    return args
1182a4696661STaylor Simpson
1183a4696661STaylor Simpson
1184a4696661STaylor Simpsondef read_common_files():
1185a4696661STaylor Simpson    read_semantics_file(sys.argv[1])
1186a4696661STaylor Simpson    read_overrides_file(sys.argv[2])
1187a4696661STaylor Simpson    read_overrides_file(sys.argv[3])
1188a4696661STaylor Simpson    ## Whether or not idef-parser is enabled is
1189a4696661STaylor Simpson    ## determined by the number of arguments to
1190a4696661STaylor Simpson    ## this script:
1191a4696661STaylor Simpson    ##
1192a4696661STaylor Simpson    ##   4 args. -> not enabled,
1193a4696661STaylor Simpson    ##   5 args. -> idef-parser enabled.
1194a4696661STaylor Simpson    ##
1195a4696661STaylor Simpson    ## The 5:th arg. then holds a list of the successfully
1196a4696661STaylor Simpson    ## parsed instructions.
1197a4696661STaylor Simpson    is_idef_parser_enabled = len(sys.argv) > 5
1198a4696661STaylor Simpson    if is_idef_parser_enabled:
1199a4696661STaylor Simpson        read_idef_parser_enabled_file(sys.argv[4])
1200a4696661STaylor Simpson    calculate_attribs()
1201a4696661STaylor Simpson    init_registers()
1202a4696661STaylor Simpson    return is_idef_parser_enabled
1203