blob: dce1b852a7b6fc00b7e27904ad007bbbce9f1b56 [file] [log] [blame]
Taylor Simpson793958c2021-02-07 23:46:10 -06001#!/usr/bin/env python3
2
3##
Taylor Simpsone28b77a2023-03-06 18:58:26 -08004## Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
Taylor Simpson793958c2021-02-07 23:46:10 -06005##
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 2 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, see <http://www.gnu.org/licenses/>.
18##
19
20import sys
21import re
22import string
23
Marco Liebel5bb322e2023-03-20 02:25:33 -070024behdict = {} # tag ->behavior
25semdict = {} # tag -> semantics
26attribdict = {} # tag -> attributes
27macros = {} # macro -> macro information...
28attribinfo = {} # Register information and misc
29tags = [] # list of all tags
30overrides = {} # tags with helper overrides
31idef_parser_enabled = {} # tags enabled for idef-parser
32
Matheus Tavares Bernardino3608c242023-05-24 11:41:47 -030033def bad_register(regtype, regid):
34 raise Exception(f"Bad register parse: regtype '{regtype}' regid '{regid}'")
Taylor Simpson793958c2021-02-07 23:46:10 -060035
36# We should do this as a hash for performance,
37# but to keep order let's keep it as a list.
38def 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
Marco Liebel5bb322e2023-03-20 02:25:33 -070043
44regre = re.compile(r"((?<!DUP)[MNORCPQXSGVZA])([stuvwxyzdefg]+)([.]?[LlHh]?)(\d+S?)")
Taylor Simpson793958c2021-02-07 23:46:10 -060045immre = re.compile(r"[#]([rRsSuUm])(\d+)(?:[:](\d+))?")
Marco Liebel5bb322e2023-03-20 02:25:33 -070046reg_or_immre = re.compile(
47 r"(((?<!DUP)[MNRCOPQXSGVZA])([stuvwxyzdefg]+)"
48 + "([.]?[LlHh]?)(\d+S?))|([#]([rRsSuUm])(\d+)[:]?(\d+)?)"
49)
Taylor Simpson793958c2021-02-07 23:46:10 -060050relimmre = re.compile(r"[#]([rR])(\d+)(?:[:](\d+))?")
51absimmre = re.compile(r"[#]([sSuUm])(\d+)(?:[:](\d+))?")
52
53finished_macros = set()
54
Marco Liebel5bb322e2023-03-20 02:25:33 -070055
56def expand_macro_attribs(macro, allmac_re):
Taylor Simpson793958c2021-02-07 23:46:10 -060057 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:
Marco Liebel5bb322e2023-03-20 02:25:33 -070061 if not submacro:
62 continue
Taylor Simpson793958c2021-02-07 23:46:10 -060063 if not macros[submacro]:
Marco Liebelcd6c4ed2023-03-20 02:25:32 -070064 raise Exception(f"Couldn't find macro: <{l}>")
Marco Liebel5bb322e2023-03-20 02:25:33 -070065 macro.attribs |= expand_macro_attribs(macros[submacro], allmac_re)
Taylor Simpson793958c2021-02-07 23:46:10 -060066 finished_macros.add(macro.key)
67 return macro.attribs
68
Marco Liebel5bb322e2023-03-20 02:25:33 -070069
Taylor Simpson793958c2021-02-07 23:46:10 -060070# When qemu needs an attribute that isn't in the imported files,
71# we'll add it here.
72def add_qemu_macro_attrib(name, attrib):
73 macros[name].attribs.add(attrib)
74
Marco Liebel5bb322e2023-03-20 02:25:33 -070075
76immextre = re.compile(r"f(MUST_)?IMMEXT[(]([UuSsRr])")
77
Taylor Simpson613653e2022-11-08 08:29:01 -080078
79def is_cond_jump(tag):
Marco Liebel5bb322e2023-03-20 02:25:33 -070080 if tag == "J2_rte":
Taylor Simpson613653e2022-11-08 08:29:01 -080081 return False
Marco Liebel5bb322e2023-03-20 02:25:33 -070082 if "A_HWLOOP0_END" in attribdict[tag] or "A_HWLOOP1_END" in attribdict[tag]:
Taylor Simpson613653e2022-11-08 08:29:01 -080083 return False
Marco Liebel5bb322e2023-03-20 02:25:33 -070084 return re.compile(r"(if.*fBRANCH)|(if.*fJUMPR)").search(semdict[tag]) != None
85
Taylor Simpson613653e2022-11-08 08:29:01 -080086
87def is_cond_call(tag):
88 return re.compile(r"(if.*fCALL)").search(semdict[tag]) != None
89
Marco Liebel5bb322e2023-03-20 02:25:33 -070090
Taylor Simpson793958c2021-02-07 23:46:10 -060091def calculate_attribs():
Marco Liebel5bb322e2023-03-20 02:25:33 -070092 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")
Taylor Simpsonb9f03262023-04-27 16:00:01 -0700102 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')
Taylor Simpson793958c2021-02-07 23:46:10 -0600108
109 # Recurse down macros, find attributes from sub-macros
110 macroValues = list(macros.values())
Marco Liebel5bb322e2023-03-20 02:25:33 -0700111 allmacros_restr = "|".join(set([m.re.pattern for m in macroValues]))
Taylor Simpson793958c2021-02-07 23:46:10 -0600112 allmacros_re = re.compile(allmacros_restr)
113 for macro in macroValues:
Marco Liebel5bb322e2023-03-20 02:25:33 -0700114 expand_macro_attribs(macro, allmacros_re)
Taylor Simpson793958c2021-02-07 23:46:10 -0600115 # Append attributes to all instructions
116 for tag in tags:
117 for macname in allmacros_re.findall(semdict[tag]):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700118 if not macname:
119 continue
Taylor Simpson793958c2021-02-07 23:46:10 -0600120 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]
Matheus Tavares Bernardino3608c242023-05-24 11:41:47 -0300126 for regtype, regid in regs:
Taylor Simpson793958c2021-02-07 23:46:10 -0600127 if regtype == "P" and is_written(regid):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700128 attribdict[tag].add("A_WRITES_PRED_REG")
Taylor Simpson613653e2022-11-08 08:29:01 -0800129 # 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):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700133 attribdict[tag].add("A_CONDEXEC")
134
Taylor Simpson793958c2021-02-07 23:46:10 -0600135
136def SEMANTICS(tag, beh, sem):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700137 # print tag,beh,sem
Taylor Simpson793958c2021-02-07 23:46:10 -0600138 behdict[tag] = beh
139 semdict[tag] = sem
140 attribdict[tag] = set()
Marco Liebel5bb322e2023-03-20 02:25:33 -0700141 tags.append(tag) # dicts have no order, this is for order
142
Taylor Simpson793958c2021-02-07 23:46:10 -0600143
144def ATTRIBUTES(tag, attribstring):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700145 attribstring = attribstring.replace("ATTRIBS", "").replace("(", "").replace(")", "")
Taylor Simpson793958c2021-02-07 23:46:10 -0600146 if not attribstring:
147 return
148 attribs = attribstring.split(",")
149 for attrib in attribs:
150 attribdict[tag].add(attrib.strip())
151
Marco Liebel5bb322e2023-03-20 02:25:33 -0700152
Taylor Simpson793958c2021-02-07 23:46:10 -0600153class Macro(object):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700154 __slots__ = ["key", "name", "beh", "attribs", "re"]
155
Taylor Simpson793958c2021-02-07 23:46:10 -0600156 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
Marco Liebel5bb322e2023-03-20 02:25:33 -0700163
164def MACROATTRIB(macname, beh, attribstring):
165 attribstring = attribstring.replace("(", "").replace(")", "")
Taylor Simpson793958c2021-02-07 23:46:10 -0600166 if attribstring:
167 attribs = attribstring.split(",")
168 else:
169 attribs = []
Marco Liebel5bb322e2023-03-20 02:25:33 -0700170 macros[macname] = Macro(macname, beh, attribs)
171
Matheus Tavares Bernardino3608c242023-05-24 11:41:47 -0300172def 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)
Marco Liebel5bb322e2023-03-20 02:25:33 -0700177
Taylor Simpson793958c2021-02-07 23:46:10 -0600178def compute_tag_immediates(tag):
179 return uniquify(immre.findall(behdict[tag]))
180
Marco Liebel5bb322e2023-03-20 02:25:33 -0700181
Taylor Simpson793958c2021-02-07 23:46:10 -0600182##
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
Taylor Simpson144da352021-05-18 16:45:18 -0500192## Q HVX predicate vector
193## V HVX vector register
194## O HVX new vector register
Taylor Simpson793958c2021-02-07 23:46:10 -0600195## 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##
Matheus Tavares Bernardino3608c242023-05-24 11:41:47 -0300203def get_tagregs(full=False):
204 compute_func = lambda tag: compute_tag_regs(tag, full)
205 return dict(zip(tags, list(map(compute_func, tags))))
Marco Liebel5bb322e2023-03-20 02:25:33 -0700206
Taylor Simpson793958c2021-02-07 23:46:10 -0600207def get_tagimms():
208 return dict(zip(tags, list(map(compute_tag_immediates, tags))))
209
Marco Liebel5bb322e2023-03-20 02:25:33 -0700210
Taylor Simpson793958c2021-02-07 23:46:10 -0600211def is_pair(regid):
212 return len(regid) == 2
213
Marco Liebel5bb322e2023-03-20 02:25:33 -0700214
Taylor Simpson793958c2021-02-07 23:46:10 -0600215def is_single(regid):
216 return len(regid) == 1
217
Marco Liebel5bb322e2023-03-20 02:25:33 -0700218
Taylor Simpson793958c2021-02-07 23:46:10 -0600219def is_written(regid):
220 return regid[0] in "dexy"
221
Marco Liebel5bb322e2023-03-20 02:25:33 -0700222
Taylor Simpson793958c2021-02-07 23:46:10 -0600223def is_writeonly(regid):
224 return regid[0] in "de"
225
Marco Liebel5bb322e2023-03-20 02:25:33 -0700226
Taylor Simpson793958c2021-02-07 23:46:10 -0600227def is_read(regid):
228 return regid[0] in "stuvwxy"
229
Marco Liebel5bb322e2023-03-20 02:25:33 -0700230
Taylor Simpson793958c2021-02-07 23:46:10 -0600231def is_readwrite(regid):
232 return regid[0] in "xy"
233
Marco Liebel5bb322e2023-03-20 02:25:33 -0700234
Taylor Simpson793958c2021-02-07 23:46:10 -0600235def is_scalar_reg(regtype):
236 return regtype in "RPC"
237
Marco Liebel5bb322e2023-03-20 02:25:33 -0700238
Taylor Simpson144da352021-05-18 16:45:18 -0500239def is_hvx_reg(regtype):
240 return regtype in "VQ"
241
Marco Liebel5bb322e2023-03-20 02:25:33 -0700242
Taylor Simpson793958c2021-02-07 23:46:10 -0600243def is_old_val(regtype, regid, tag):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700244 return regtype + regid + "V" in semdict[tag]
245
Taylor Simpson793958c2021-02-07 23:46:10 -0600246
247def is_new_val(regtype, regid, tag):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700248 return regtype + regid + "N" in semdict[tag]
249
Taylor Simpson793958c2021-02-07 23:46:10 -0600250
251def need_slot(tag):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700252 if (
Taylor Simpsone5d0d782023-04-27 16:00:11 -0700253 "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])
Marco Liebel5bb322e2023-03-20 02:25:33 -0700257 ):
Taylor Simpson793958c2021-02-07 23:46:10 -0600258 return 1
259 else:
260 return 0
261
Marco Liebel5bb322e2023-03-20 02:25:33 -0700262
Taylor Simpson793958c2021-02-07 23:46:10 -0600263def need_part1(tag):
264 return re.compile(r"fPART1").search(semdict[tag])
265
Marco Liebel5bb322e2023-03-20 02:25:33 -0700266
Taylor Simpson793958c2021-02-07 23:46:10 -0600267def need_ea(tag):
268 return re.compile(r"\bEA\b").search(semdict[tag])
269
Marco Liebel5bb322e2023-03-20 02:25:33 -0700270
Taylor Simpson40085902022-11-08 08:29:00 -0800271def need_PC(tag):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700272 return "A_IMPLICIT_READS_PC" in attribdict[tag]
273
Taylor Simpson40085902022-11-08 08:29:00 -0800274
Taylor Simpson613653e2022-11-08 08:29:01 -0800275def helper_needs_next_PC(tag):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700276 return "A_CALL" in attribdict[tag]
277
Taylor Simpson613653e2022-11-08 08:29:01 -0800278
Taylor Simpsonfb67c2b2022-11-08 08:28:59 -0800279def need_pkt_has_multi_cof(tag):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700280 return "A_COF" in attribdict[tag]
281
Taylor Simpsonfb67c2b2022-11-08 08:28:59 -0800282
Taylor Simpsond54c5612023-04-27 16:00:02 -0700283def need_pkt_need_commit(tag):
284 return 'A_IMPLICIT_WRITES_USR' in attribdict[tag]
285
Taylor Simpsone28b77a2023-03-06 18:58:26 -0800286def need_condexec_reg(tag, regs):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700287 if "A_CONDEXEC" in attribdict[tag]:
Matheus Tavares Bernardino3608c242023-05-24 11:41:47 -0300288 for regtype, regid in regs:
Taylor Simpsone28b77a2023-03-06 18:58:26 -0800289 if is_writeonly(regid) and not is_hvx_reg(regtype):
290 return True
291 return False
292
Marco Liebel5bb322e2023-03-20 02:25:33 -0700293
Taylor Simpson793958c2021-02-07 23:46:10 -0600294def skip_qemu_helper(tag):
295 return tag in overrides.keys()
296
Marco Liebel5bb322e2023-03-20 02:25:33 -0700297
Taylor Simpson144da352021-05-18 16:45:18 -0500298def is_tmp_result(tag):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700299 return "A_CVI_TMP" in attribdict[tag] or "A_CVI_TMP_DST" in attribdict[tag]
300
Taylor Simpson144da352021-05-18 16:45:18 -0500301
302def is_new_result(tag):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700303 return "A_CVI_NEW" in attribdict[tag]
304
Taylor Simpson144da352021-05-18 16:45:18 -0500305
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +0200306def is_idef_parser_enabled(tag):
307 return tag in idef_parser_enabled
308
Marco Liebel5bb322e2023-03-20 02:25:33 -0700309
Taylor Simpson793958c2021-02-07 23:46:10 -0600310def imm_name(immlett):
Marco Liebelcd6c4ed2023-03-20 02:25:32 -0700311 return f"{immlett}iV"
Taylor Simpson793958c2021-02-07 23:46:10 -0600312
Marco Liebel5bb322e2023-03-20 02:25:33 -0700313
Taylor Simpson793958c2021-02-07 23:46:10 -0600314def read_semantics_file(name):
315 eval_line = ""
Marco Liebel5bb322e2023-03-20 02:25:33 -0700316 for line in open(name, "rt").readlines():
Taylor Simpson793958c2021-02-07 23:46:10 -0600317 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
Marco Liebel5bb322e2023-03-20 02:25:33 -0700325
Taylor Simpson793958c2021-02-07 23:46:10 -0600326def read_attribs_file(name):
Marco Liebel5bb322e2023-03-20 02:25:33 -0700327 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():
Taylor Simpson793958c2021-02-07 23:46:10 -0600332 if not attribre.match(line):
333 continue
Marco Liebel5bb322e2023-03-20 02:25:33 -0700334 (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
Taylor Simpson793958c2021-02-07 23:46:10 -0600338
339def read_overrides_file(name):
340 overridere = re.compile("#define fGEN_TCG_([A-Za-z0-9_]+)\(.*")
Marco Liebel5bb322e2023-03-20 02:25:33 -0700341 for line in open(name, "rt").readlines():
Taylor Simpson793958c2021-02-07 23:46:10 -0600342 if not overridere.match(line):
343 continue
344 tag = overridere.findall(line)[0]
345 overrides[tag] = True
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +0200346
Marco Liebel5bb322e2023-03-20 02:25:33 -0700347
Alessandro Di Federicoe71fdc42022-09-23 19:38:30 +0200348def 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)