2020-05-05 03:34:51 +03:00
|
|
|
#!/usr/bin/env python3
|
2015-08-21 10:04:50 +03:00
|
|
|
# Unicorn Engine
|
|
|
|
# By Dang Hoang Vu, 2013
|
|
|
|
from __future__ import print_function
|
2015-10-14 17:32:31 +03:00
|
|
|
import sys, re, os
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-10-14 17:32:31 +03:00
|
|
|
INCL_DIR = os.path.join('..', 'include', 'unicorn')
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2022-04-30 00:11:34 +03:00
|
|
|
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'm68k.h', 'ppc.h', 'riscv.h', 's390x.h', 'tricore.h', 'unicorn.h' ]
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
template = {
|
|
|
|
'python': {
|
|
|
|
'header': "# For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
|
|
|
|
'footer': "",
|
2015-09-08 05:25:13 +03:00
|
|
|
'line_format': 'UC_%s = %s\n',
|
2015-08-21 10:04:50 +03:00
|
|
|
'out_file': './python/unicorn/%s_const.py',
|
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'arm',
|
|
|
|
'arm64.h': 'arm64',
|
|
|
|
'mips.h': 'mips',
|
|
|
|
'x86.h': 'x86',
|
|
|
|
'sparc.h': 'sparc',
|
|
|
|
'm68k.h': 'm68k',
|
2021-10-03 17:14:44 +03:00
|
|
|
'ppc.h': 'ppc',
|
|
|
|
'riscv.h': 'riscv',
|
2022-01-01 04:24:28 +03:00
|
|
|
's390x.h' : 's390x',
|
2022-04-30 00:11:34 +03:00
|
|
|
'tricore.h' : 'tricore',
|
2015-08-28 17:31:38 +03:00
|
|
|
'unicorn.h': 'unicorn',
|
2015-08-21 10:04:50 +03:00
|
|
|
'comment_open': '#',
|
|
|
|
'comment_close': '',
|
|
|
|
},
|
2016-03-22 14:17:23 +03:00
|
|
|
'ruby': {
|
2017-09-19 03:43:21 +03:00
|
|
|
'header': "# For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.rb]\n\nmodule UnicornEngine\n",
|
2016-03-22 14:17:23 +03:00
|
|
|
'footer': "end",
|
|
|
|
'line_format': '\tUC_%s = %s\n',
|
2017-09-19 03:43:21 +03:00
|
|
|
'out_file': './ruby/unicorn_gem/lib/unicorn_engine/%s_const.rb',
|
2016-03-22 14:17:23 +03:00
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'arm',
|
|
|
|
'arm64.h': 'arm64',
|
|
|
|
'mips.h': 'mips',
|
|
|
|
'x86.h': 'x86',
|
|
|
|
'sparc.h': 'sparc',
|
|
|
|
'm68k.h': 'm68k',
|
2021-10-03 17:14:44 +03:00
|
|
|
'ppc.h': 'ppc',
|
|
|
|
'riscv.h': 'riscv',
|
2022-01-01 04:24:28 +03:00
|
|
|
's390x.h' : 's390x',
|
2022-04-30 00:11:34 +03:00
|
|
|
'tricore.h' : 'tricore',
|
2016-03-22 14:17:23 +03:00
|
|
|
'unicorn.h': 'unicorn',
|
|
|
|
'comment_open': '#',
|
|
|
|
'comment_close': '',
|
|
|
|
},
|
2015-08-28 07:13:50 +03:00
|
|
|
'go': {
|
2015-08-28 17:31:38 +03:00
|
|
|
'header': "package unicorn\n// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.go]\nconst (\n",
|
2015-08-28 07:13:50 +03:00
|
|
|
'footer': ")",
|
2015-08-28 17:31:38 +03:00
|
|
|
'line_format': '\t%s = %s\n',
|
2015-08-28 07:13:50 +03:00
|
|
|
'out_file': './go/unicorn/%s_const.go',
|
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'arm',
|
|
|
|
'arm64.h': 'arm64',
|
|
|
|
'mips.h': 'mips',
|
|
|
|
'x86.h': 'x86',
|
|
|
|
'sparc.h': 'sparc',
|
|
|
|
'm68k.h': 'm68k',
|
2021-10-03 17:14:44 +03:00
|
|
|
'ppc.h': 'ppc',
|
|
|
|
'riscv.h': 'riscv',
|
2022-01-01 04:24:28 +03:00
|
|
|
's390x.h' : 's390x',
|
2022-04-30 00:11:34 +03:00
|
|
|
'tricore.h' : 'tricore',
|
2015-08-29 09:33:38 +03:00
|
|
|
'unicorn.h': 'unicorn',
|
2015-08-29 05:41:13 +03:00
|
|
|
'comment_open': '//',
|
|
|
|
'comment_close': '',
|
|
|
|
},
|
|
|
|
'java': {
|
|
|
|
'header': "// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT\n\npackage unicorn;\n\npublic interface %sConst {\n",
|
|
|
|
'footer': "\n}\n",
|
2023-05-06 00:32:56 +03:00
|
|
|
'line_format': ' public static final int UC_%s = %s;\n',
|
2023-06-30 02:04:51 +03:00
|
|
|
'out_file': './java/src/main/java/unicorn/%sConst.java',
|
2015-08-29 05:41:13 +03:00
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'Arm',
|
|
|
|
'arm64.h': 'Arm64',
|
|
|
|
'mips.h': 'Mips',
|
|
|
|
'x86.h': 'X86',
|
|
|
|
'sparc.h': 'Sparc',
|
|
|
|
'm68k.h': 'M68k',
|
2021-10-03 17:14:44 +03:00
|
|
|
'ppc.h': 'Ppc',
|
|
|
|
'riscv.h': 'Riscv',
|
2022-01-01 04:24:28 +03:00
|
|
|
's390x.h' : 'S390x',
|
2022-04-30 00:11:34 +03:00
|
|
|
'tricore.h' : 'TriCore',
|
2015-08-29 09:29:59 +03:00
|
|
|
'unicorn.h': 'Unicorn',
|
2023-05-06 00:32:56 +03:00
|
|
|
'comment_open': ' //',
|
2015-08-28 07:13:50 +03:00
|
|
|
'comment_close': '',
|
|
|
|
},
|
2015-10-14 17:32:31 +03:00
|
|
|
'dotnet': {
|
2022-11-01 00:56:44 +03:00
|
|
|
'header': "// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT\n\nnamespace UnicornEngine.Const\n\nopen System\n\n[<AutoOpen>]\nmodule %s =\n",
|
2015-10-14 17:32:31 +03:00
|
|
|
'footer': "\n",
|
2016-01-04 13:31:17 +03:00
|
|
|
'line_format': ' let UC_%s = %s\n',
|
2022-11-01 00:56:44 +03:00
|
|
|
'out_file': os.path.join('dotnet', 'UnicornEngine', 'Const', '%s.fs'),
|
2015-10-14 17:32:31 +03:00
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'Arm',
|
|
|
|
'arm64.h': 'Arm64',
|
|
|
|
'mips.h': 'Mips',
|
|
|
|
'x86.h': 'X86',
|
|
|
|
'sparc.h': 'Sparc',
|
|
|
|
'm68k.h': 'M68k',
|
2021-10-03 17:14:44 +03:00
|
|
|
'ppc.h': 'Ppc',
|
|
|
|
'riscv.h': 'Riscv',
|
2022-01-01 04:24:28 +03:00
|
|
|
's390x.h' : 'S390x',
|
2022-04-30 00:11:34 +03:00
|
|
|
'tricore.h' : 'TriCore',
|
2015-10-14 17:32:31 +03:00
|
|
|
'unicorn.h': 'Common',
|
2016-01-04 18:55:20 +03:00
|
|
|
'comment_open': ' //',
|
2015-10-14 17:32:31 +03:00
|
|
|
'comment_close': '',
|
|
|
|
},
|
2018-08-03 15:33:25 +03:00
|
|
|
'pascal': {
|
|
|
|
'header': "// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT\n\nunit %sConst;\n\ninterface\n\nconst",
|
|
|
|
'footer': "\nimplementation\nend.",
|
|
|
|
'line_format': ' UC_%s = %s;\n',
|
|
|
|
'out_file': os.path.join('pascal', 'unicorn', '%sConst.pas'),
|
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'Arm',
|
|
|
|
'arm64.h': 'Arm64',
|
|
|
|
'mips.h': 'Mips',
|
|
|
|
'x86.h': 'X86',
|
|
|
|
'sparc.h': 'Sparc',
|
|
|
|
'm68k.h': 'M68k',
|
2021-10-03 17:14:44 +03:00
|
|
|
'ppc.h': 'Ppc',
|
|
|
|
'riscv.h': 'Riscv',
|
2022-01-01 04:24:28 +03:00
|
|
|
's390x.h' : 'S390x',
|
2022-04-30 00:11:34 +03:00
|
|
|
'tricore.h' : 'TriCore',
|
2018-08-03 15:33:25 +03:00
|
|
|
'unicorn.h': 'Unicorn',
|
|
|
|
'comment_open': '//',
|
|
|
|
'comment_close': '',
|
|
|
|
},
|
2023-03-23 16:09:41 +03:00
|
|
|
'zig': {
|
|
|
|
'header': "// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT\n\npub const %sConst = enum(c_int) {\n",
|
|
|
|
'footer': "\n};\n",
|
|
|
|
'line_format': '\t%s = %s,\n',
|
|
|
|
'out_file': './zig/unicorn/%s_const.zig',
|
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'arm',
|
|
|
|
'arm64.h': 'arm64',
|
|
|
|
'mips.h': 'mips',
|
|
|
|
'x86.h': 'x86',
|
|
|
|
'sparc.h': 'sparc',
|
|
|
|
'm68k.h': 'm68k',
|
|
|
|
'ppc.h': 'ppc',
|
|
|
|
'riscv.h': 'riscv',
|
|
|
|
's390x.h' : 's390x',
|
|
|
|
'tricore.h' : 'tricore',
|
|
|
|
'unicorn.h': 'unicorn',
|
|
|
|
'comment_open': '//',
|
|
|
|
'comment_close': '',
|
|
|
|
},
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# markup for comments to be added to autogen files
|
|
|
|
MARKUP = '//>'
|
|
|
|
|
|
|
|
def gen(lang):
|
|
|
|
global include, INCL_DIR
|
|
|
|
templ = template[lang]
|
|
|
|
for target in include:
|
|
|
|
prefix = templ[target]
|
2023-07-07 06:12:36 +03:00
|
|
|
outfn = templ['out_file'] % prefix
|
|
|
|
outfile = open(outfn + ".tmp", 'wb') # open as binary prevents windows newlines
|
|
|
|
outfile.write((templ['header'] % prefix).encode("utf-8"))
|
2015-08-28 17:31:38 +03:00
|
|
|
if target == 'unicorn.h':
|
|
|
|
prefix = ''
|
2020-05-05 03:34:51 +03:00
|
|
|
with open(os.path.join(INCL_DIR, target)) as f:
|
|
|
|
lines = f.readlines()
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-28 17:31:38 +03:00
|
|
|
previous = {}
|
2015-08-21 10:04:50 +03:00
|
|
|
count = 0
|
2021-10-30 05:45:32 +03:00
|
|
|
skip = 0
|
|
|
|
in_comment = False
|
|
|
|
|
|
|
|
for lno, line in enumerate(lines):
|
|
|
|
if "/*" in line:
|
|
|
|
in_comment = True
|
|
|
|
if "*/" in line:
|
|
|
|
in_comment = False
|
|
|
|
if in_comment:
|
|
|
|
continue
|
|
|
|
if skip > 0:
|
|
|
|
# Due to clang-format, values may come up in the next line
|
|
|
|
skip -= 1
|
|
|
|
continue
|
2015-08-21 10:04:50 +03:00
|
|
|
line = line.strip()
|
|
|
|
|
|
|
|
if line.startswith(MARKUP): # markup for comments
|
2015-10-04 05:55:29 +03:00
|
|
|
outfile.write(("\n%s%s%s\n" %(templ['comment_open'], \
|
|
|
|
line.replace(MARKUP, ''), templ['comment_close'])).encode("utf-8"))
|
2015-08-21 10:04:50 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
if line == '' or line.startswith('//'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
tmp = line.strip().split(',')
|
2021-10-30 05:45:32 +03:00
|
|
|
if len(tmp) >= 2 and tmp[0] != "#define" and not tmp[0].startswith("UC_"):
|
|
|
|
continue
|
2015-08-21 10:04:50 +03:00
|
|
|
for t in tmp:
|
|
|
|
t = t.strip()
|
|
|
|
if not t or t.startswith('//'): continue
|
|
|
|
f = re.split('\s+', t)
|
|
|
|
|
2015-08-28 17:31:38 +03:00
|
|
|
# parse #define UC_TARGET (num)
|
|
|
|
define = False
|
2015-10-03 20:41:19 +03:00
|
|
|
if f[0] == '#define' and len(f) >= 3:
|
2015-08-28 17:31:38 +03:00
|
|
|
define = True
|
|
|
|
f.pop(0)
|
|
|
|
f.insert(1, '=')
|
2021-10-30 05:45:32 +03:00
|
|
|
if f[0].startswith("UC_" + prefix.upper()) or f[0].startswith("UC_CPU"):
|
2015-08-28 17:31:38 +03:00
|
|
|
if len(f) > 1 and f[1] not in ('//', '='):
|
2016-01-23 05:31:16 +03:00
|
|
|
print("WARNING: Unable to convert %s" % f)
|
|
|
|
print(" Line =", line)
|
2015-08-21 10:04:50 +03:00
|
|
|
continue
|
|
|
|
elif len(f) > 1 and f[1] == '=':
|
2021-10-30 05:45:32 +03:00
|
|
|
# Like:
|
|
|
|
# UC_A =
|
|
|
|
# (1 << 2)
|
|
|
|
# #define UC_B \
|
|
|
|
# (UC_A | UC_C)
|
|
|
|
# Let's search the next line
|
|
|
|
if len(f) == 2:
|
|
|
|
if lno == len(lines) - 1:
|
|
|
|
print("WARNING: Unable to convert %s" % f)
|
|
|
|
print(" Line =", line)
|
|
|
|
continue
|
|
|
|
skip += 1
|
|
|
|
next_line = lines[lno + 1]
|
|
|
|
next_line_tmp = next_line.strip().split(",")
|
|
|
|
rhs = next_line_tmp[0]
|
|
|
|
elif f[-1] == "\\":
|
|
|
|
idx = 0
|
|
|
|
rhs = ""
|
|
|
|
while True:
|
|
|
|
idx += 1
|
|
|
|
if lno + idx == len(lines):
|
|
|
|
print("WARNING: Unable to convert %s" % f)
|
|
|
|
print(" Line =", line)
|
|
|
|
continue
|
|
|
|
skip += 1
|
|
|
|
next_line = lines[lno + idx]
|
|
|
|
next_line_f = re.split('\s+', next_line.strip())
|
|
|
|
if next_line_f[-1] == "\\":
|
|
|
|
rhs += "".join(next_line_f[:-1])
|
|
|
|
else:
|
|
|
|
rhs += next_line.strip()
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
rhs = ''.join(f[2:])
|
2015-08-21 10:04:50 +03:00
|
|
|
else:
|
|
|
|
rhs = str(count)
|
|
|
|
|
2021-10-30 05:45:32 +03:00
|
|
|
|
2015-08-28 17:31:38 +03:00
|
|
|
lhs = f[0].strip()
|
2021-10-30 05:45:32 +03:00
|
|
|
#print(f'lhs: {lhs} rhs: {rhs} f:{f}')
|
2015-08-28 17:31:38 +03:00
|
|
|
# evaluate bitshifts in constants e.g. "UC_X86 = 1 << 1"
|
|
|
|
match = re.match(r'(?P<rhs>\s*\d+\s*<<\s*\d+\s*)', rhs)
|
|
|
|
if match:
|
2015-10-03 20:41:19 +03:00
|
|
|
rhs = str(eval(match.group(1)))
|
2015-08-28 17:31:38 +03:00
|
|
|
else:
|
|
|
|
# evaluate references to other constants e.g. "UC_ARM_REG_X = UC_ARM_REG_SP"
|
|
|
|
match = re.match(r'^([^\d]\w+)$', rhs)
|
|
|
|
if match:
|
|
|
|
rhs = previous[match.group(1)]
|
|
|
|
|
2015-10-03 20:41:19 +03:00
|
|
|
if not rhs.isdigit():
|
|
|
|
for k, v in previous.items():
|
|
|
|
rhs = re.sub(r'\b%s\b' % k, v, rhs)
|
|
|
|
rhs = str(eval(rhs))
|
|
|
|
|
2015-09-08 05:25:13 +03:00
|
|
|
lhs_strip = re.sub(r'^UC_', '', lhs)
|
2015-08-28 17:31:38 +03:00
|
|
|
count = int(rhs) + 1
|
|
|
|
if (count == 1):
|
2015-10-04 05:55:29 +03:00
|
|
|
outfile.write(("\n").encode("utf-8"))
|
2015-10-03 20:41:19 +03:00
|
|
|
|
2015-10-04 05:55:29 +03:00
|
|
|
outfile.write((templ['line_format'] % (lhs_strip, rhs)).encode("utf-8"))
|
2015-10-03 20:41:19 +03:00
|
|
|
previous[lhs] = str(rhs)
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-10-04 05:55:29 +03:00
|
|
|
outfile.write((templ['footer']).encode("utf-8"))
|
2015-08-21 10:04:50 +03:00
|
|
|
outfile.close()
|
|
|
|
|
2023-07-07 06:12:36 +03:00
|
|
|
if os.path.isfile(outfn):
|
|
|
|
with open(outfn, "rb") as infile:
|
|
|
|
cur_data = infile.read()
|
|
|
|
with open(outfn + ".tmp", "rb") as infile:
|
|
|
|
new_data = infile.read()
|
|
|
|
if cur_data == new_data:
|
|
|
|
os.unlink(outfn + ".tmp")
|
|
|
|
else:
|
|
|
|
os.unlink(outfn)
|
|
|
|
os.rename(outfn + ".tmp", outfn)
|
|
|
|
else:
|
|
|
|
os.rename(outfn + ".tmp", outfn)
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
def main():
|
2015-08-28 17:31:38 +03:00
|
|
|
lang = sys.argv[1]
|
2020-05-05 03:34:51 +03:00
|
|
|
if lang == "all":
|
|
|
|
for lang in template.keys():
|
|
|
|
print("Generating constants for {}".format(lang))
|
|
|
|
gen(lang)
|
|
|
|
else:
|
|
|
|
if not lang in template:
|
|
|
|
raise RuntimeError("Unsupported binding %s" % lang)
|
|
|
|
gen(lang)
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("Usage:", sys.argv[0], " <python>")
|
2020-05-05 03:34:51 +03:00
|
|
|
print("Supported: {}".format(["all"] + [x for x in template.keys()]))
|
2015-08-21 10:04:50 +03:00
|
|
|
sys.exit(1)
|
|
|
|
main()
|