2021-02-08 06:46:10 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
##
|
2023-03-20 10:25:32 +01:00
|
|
|
## Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
|
2021-02-08 06:46:10 +01:00
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published by
|
|
|
|
## the Free Software Foundation; either version 2 of the License, or
|
|
|
|
## (at your option) any later version.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import string
|
|
|
|
import hex_common
|
|
|
|
|
2023-03-20 10:25:33 +01:00
|
|
|
|
2021-02-08 06:46:10 +01:00
|
|
|
##
|
|
|
|
## Generate data for printing each instruction (format string + operands)
|
|
|
|
##
|
|
|
|
def regprinter(m):
|
|
|
|
str = m.group(1)
|
2023-03-20 10:25:33 +01:00
|
|
|
str += ":".join(["%d"] * len(m.group(2)))
|
2021-02-08 06:46:10 +01:00
|
|
|
str += m.group(3)
|
2023-03-20 10:25:33 +01:00
|
|
|
if ("S" in m.group(1)) and (len(m.group(2)) == 1):
|
2021-02-08 06:46:10 +01:00
|
|
|
str += "/%s"
|
2023-03-20 10:25:33 +01:00
|
|
|
elif ("C" in m.group(1)) and (len(m.group(2)) == 1):
|
2021-02-08 06:46:10 +01:00
|
|
|
str += "/%s"
|
|
|
|
return str
|
|
|
|
|
2023-03-20 10:25:33 +01:00
|
|
|
|
2021-02-08 06:46:10 +01:00
|
|
|
def spacify(s):
|
|
|
|
# Regular expression that matches any operator that contains '=' character:
|
2023-03-20 10:25:33 +01:00
|
|
|
opswithequal_re = "[-+^&|!<>=]?="
|
2021-02-08 06:46:10 +01:00
|
|
|
# Regular expression that matches any assignment operator.
|
2023-03-20 10:25:33 +01:00
|
|
|
assignment_re = "[-+^&|]?="
|
2021-02-08 06:46:10 +01:00
|
|
|
|
|
|
|
# Out of the operators that contain the = sign, if the operator is also an
|
|
|
|
# assignment, spaces will be added around it, unless it's enclosed within
|
|
|
|
# parentheses, or spaces are already present.
|
|
|
|
|
|
|
|
equals = re.compile(opswithequal_re)
|
|
|
|
assign = re.compile(assignment_re)
|
|
|
|
|
|
|
|
slen = len(s)
|
|
|
|
paren_count = {}
|
|
|
|
i = 0
|
|
|
|
pc = 0
|
|
|
|
while i < slen:
|
|
|
|
c = s[i]
|
2023-03-20 10:25:33 +01:00
|
|
|
if c == "(":
|
2021-02-08 06:46:10 +01:00
|
|
|
pc += 1
|
2023-03-20 10:25:33 +01:00
|
|
|
elif c == ")":
|
2021-02-08 06:46:10 +01:00
|
|
|
pc -= 1
|
|
|
|
paren_count[i] = pc
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
# Iterate over all operators that contain the equal sign. If any
|
|
|
|
# match is also an assignment operator, add spaces around it if
|
|
|
|
# the parenthesis count is 0.
|
|
|
|
pos = 0
|
|
|
|
out = []
|
|
|
|
for m in equals.finditer(s):
|
|
|
|
ms = m.start()
|
|
|
|
me = m.end()
|
|
|
|
# t is the string that matched opswithequal_re.
|
|
|
|
t = m.string[ms:me]
|
|
|
|
out += s[pos:ms]
|
|
|
|
pos = me
|
|
|
|
if paren_count[ms] == 0:
|
|
|
|
# Check if the entire string t is an assignment.
|
|
|
|
am = assign.match(t)
|
2023-03-20 10:25:33 +01:00
|
|
|
if am and len(am.group(0)) == me - ms:
|
2021-02-08 06:46:10 +01:00
|
|
|
# Don't add spaces if they are already there.
|
2023-03-20 10:25:33 +01:00
|
|
|
if ms > 0 and s[ms - 1] != " ":
|
|
|
|
out.append(" ")
|
2021-02-08 06:46:10 +01:00
|
|
|
out += t
|
2023-03-20 10:25:33 +01:00
|
|
|
if me < slen and s[me] != " ":
|
|
|
|
out.append(" ")
|
2021-02-08 06:46:10 +01:00
|
|
|
continue
|
|
|
|
# If this is not an assignment, just append it to the output
|
|
|
|
# string.
|
|
|
|
out += t
|
|
|
|
|
|
|
|
# Append the remaining part of the string.
|
2023-03-20 10:25:33 +01:00
|
|
|
out += s[pos : len(s)]
|
|
|
|
return "".join(out)
|
|
|
|
|
2021-02-08 06:46:10 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
hex_common.read_semantics_file(sys.argv[1])
|
|
|
|
hex_common.read_attribs_file(sys.argv[2])
|
|
|
|
|
2023-03-20 10:25:33 +01:00
|
|
|
immext_casere = re.compile(r"IMMEXT\(([A-Za-z])")
|
2021-02-08 06:46:10 +01:00
|
|
|
|
2023-03-20 10:25:33 +01:00
|
|
|
with open(sys.argv[3], "w") as f:
|
2021-02-08 06:46:10 +01:00
|
|
|
for tag in hex_common.tags:
|
2023-03-20 10:25:33 +01:00
|
|
|
if not hex_common.behdict[tag]:
|
|
|
|
continue
|
2021-02-08 06:46:10 +01:00
|
|
|
extendable_upper_imm = False
|
|
|
|
extendable_lower_imm = False
|
|
|
|
m = immext_casere.search(hex_common.semdict[tag])
|
|
|
|
if m:
|
|
|
|
if m.group(1).isupper():
|
|
|
|
extendable_upper_imm = True
|
|
|
|
else:
|
|
|
|
extendable_lower_imm = True
|
|
|
|
beh = hex_common.behdict[tag]
|
2023-03-20 10:25:33 +01:00
|
|
|
beh = hex_common.regre.sub(regprinter, beh)
|
|
|
|
beh = hex_common.absimmre.sub(r"#%s0x%x", beh)
|
|
|
|
beh = hex_common.relimmre.sub(r"PC+%s%d", beh)
|
2021-02-08 06:46:10 +01:00
|
|
|
beh = spacify(beh)
|
|
|
|
# Print out a literal "%s" at the end, used to match empty string
|
|
|
|
# so C won't complain at us
|
2023-03-20 10:25:33 +01:00
|
|
|
if "A_VECX" in hex_common.attribdict[tag]:
|
2021-02-08 06:46:10 +01:00
|
|
|
macname = "DEF_VECX_PRINTINFO"
|
2023-03-20 10:25:33 +01:00
|
|
|
else:
|
|
|
|
macname = "DEF_PRINTINFO"
|
2023-03-20 10:25:32 +01:00
|
|
|
f.write(f'{macname}({tag},"{beh}%s"')
|
2023-03-20 10:25:33 +01:00
|
|
|
regs_or_imms = hex_common.reg_or_immre.findall(hex_common.behdict[tag])
|
2021-02-08 06:46:10 +01:00
|
|
|
ri = 0
|
|
|
|
seenregs = {}
|
2023-03-20 10:25:33 +01:00
|
|
|
for allregs, a, b, c, d, allimm, immlett, bits, immshift in regs_or_imms:
|
2021-02-08 06:46:10 +01:00
|
|
|
if a:
|
2023-03-20 10:25:33 +01:00
|
|
|
# register
|
2021-02-08 06:46:10 +01:00
|
|
|
if b in seenregs:
|
|
|
|
regno = seenregs[b]
|
|
|
|
else:
|
|
|
|
regno = ri
|
|
|
|
if len(b) == 1:
|
2023-03-20 10:25:33 +01:00
|
|
|
f.write(f", insn->regno[{regno}]")
|
|
|
|
if "S" in a:
|
|
|
|
f.write(f", sreg2str(insn->regno[{regno}])")
|
|
|
|
elif "C" in a:
|
|
|
|
f.write(f", creg2str(insn->regno[{regno}])")
|
2021-02-08 06:46:10 +01:00
|
|
|
elif len(b) == 2:
|
2023-03-20 10:25:33 +01:00
|
|
|
f.write(f", insn->regno[{regno}] + 1" f", insn->regno[{regno}]")
|
2021-02-08 06:46:10 +01:00
|
|
|
else:
|
|
|
|
print("Put some stuff to handle quads here")
|
|
|
|
if b not in seenregs:
|
|
|
|
seenregs[b] = ri
|
|
|
|
ri += 1
|
|
|
|
else:
|
2023-03-20 10:25:33 +01:00
|
|
|
# immediate
|
|
|
|
if immlett.isupper():
|
2021-02-08 06:46:10 +01:00
|
|
|
if extendable_upper_imm:
|
2023-03-20 10:25:33 +01:00
|
|
|
if immlett in "rR":
|
2021-02-08 06:46:10 +01:00
|
|
|
f.write(',insn->extension_valid?"##":""')
|
|
|
|
else:
|
|
|
|
f.write(',insn->extension_valid?"#":""')
|
|
|
|
else:
|
|
|
|
f.write(',""')
|
|
|
|
ii = 1
|
|
|
|
else:
|
|
|
|
if extendable_lower_imm:
|
2023-03-20 10:25:33 +01:00
|
|
|
if immlett in "rR":
|
2021-02-08 06:46:10 +01:00
|
|
|
f.write(',insn->extension_valid?"##":""')
|
|
|
|
else:
|
|
|
|
f.write(',insn->extension_valid?"#":""')
|
|
|
|
else:
|
|
|
|
f.write(',""')
|
|
|
|
ii = 0
|
2023-03-20 10:25:33 +01:00
|
|
|
f.write(f", insn->immed[{ii}]")
|
2021-02-08 06:46:10 +01:00
|
|
|
# append empty string so there is at least one more arg
|
|
|
|
f.write(',"")\n')
|
|
|
|
|
2023-03-20 10:25:33 +01:00
|
|
|
|
2021-02-08 06:46:10 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|