improve const generator; emit unicorn.h consts
This commit is contained in:
@ -5,7 +5,7 @@ import sys, re
|
||||
|
||||
INCL_DIR = '../include/unicorn/'
|
||||
|
||||
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'm68k.h' ]
|
||||
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'm68k.h', 'unicorn.h' ]
|
||||
|
||||
template = {
|
||||
'python': {
|
||||
@ -20,13 +20,14 @@ template = {
|
||||
'x86.h': 'x86',
|
||||
'sparc.h': 'sparc',
|
||||
'm68k.h': 'm68k',
|
||||
'unicorn.h': 'unicorn',
|
||||
'comment_open': '#',
|
||||
'comment_close': '',
|
||||
},
|
||||
'go': {
|
||||
'header': "package unicorn\n// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.go]\nconst (",
|
||||
'header': "package unicorn\n// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.go]\nconst (\n",
|
||||
'footer': ")",
|
||||
'line_format': '%s = %s\n',
|
||||
'line_format': '\t%s = %s\n',
|
||||
'out_file': './go/unicorn/%s_const.go',
|
||||
# prefixes for constant filenames of all archs - case sensitive
|
||||
'arm.h': 'arm',
|
||||
@ -35,6 +36,7 @@ template = {
|
||||
'x86.h': 'x86',
|
||||
'sparc.h': 'sparc',
|
||||
'm68k.h': 'm68k',
|
||||
'unicorn.h': 'unicorn',
|
||||
'comment_open': '//',
|
||||
'comment_close': '',
|
||||
},
|
||||
@ -50,9 +52,11 @@ def gen(lang):
|
||||
prefix = templ[target]
|
||||
outfile = open(templ['out_file'] %(prefix), 'w')
|
||||
outfile.write(templ['header'] % (prefix))
|
||||
|
||||
if target == 'unicorn.h':
|
||||
prefix = ''
|
||||
lines = open(INCL_DIR + target).readlines()
|
||||
|
||||
previous = {}
|
||||
count = 0
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
@ -65,17 +69,21 @@ def gen(lang):
|
||||
if line == '' or line.startswith('//'):
|
||||
continue
|
||||
|
||||
if not line.startswith("UC_" + prefix.upper()):
|
||||
continue
|
||||
|
||||
tmp = line.strip().split(',')
|
||||
for t in tmp:
|
||||
t = t.strip()
|
||||
if not t or t.startswith('//'): continue
|
||||
f = re.split('\s+', t)
|
||||
|
||||
# parse #define UC_TARGET (num)
|
||||
define = False
|
||||
if f[0] == '#define' and len(f) >= 3 and f[2].isdigit():
|
||||
define = True
|
||||
f.pop(0)
|
||||
f.insert(1, '=')
|
||||
|
||||
if f[0].startswith("UC_" + prefix.upper()):
|
||||
if len(f) > 1 and f[1] not in '//=':
|
||||
if len(f) > 1 and f[1] not in ('//', '='):
|
||||
print("Error: Unable to convert %s" % f)
|
||||
continue
|
||||
elif len(f) > 1 and f[1] == '=':
|
||||
@ -84,29 +92,31 @@ def gen(lang):
|
||||
rhs = str(count)
|
||||
count += 1
|
||||
|
||||
try:
|
||||
count = int(rhs) + 1
|
||||
if (count == 1):
|
||||
outfile.write("\n")
|
||||
except ValueError:
|
||||
if lang == 'ocaml':
|
||||
# ocaml uses lsl for '<<', lor for '|'
|
||||
rhs = rhs.replace('<<', ' lsl ')
|
||||
rhs = rhs.replace('|', ' lor ')
|
||||
# ocaml variable has _ as prefix
|
||||
if rhs[0].isalpha():
|
||||
rhs = '_' + rhs
|
||||
lhs = f[0].strip()
|
||||
# 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:
|
||||
rhs = eval(match.group(1))
|
||||
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)]
|
||||
|
||||
outfile.write(templ['line_format'] %(f[0].strip(), rhs))
|
||||
count = int(rhs) + 1
|
||||
if (count == 1):
|
||||
outfile.write("\n")
|
||||
outfile.write(templ['line_format'] % (lhs, rhs))
|
||||
previous[lhs] = rhs
|
||||
|
||||
outfile.write(templ['footer'])
|
||||
outfile.close()
|
||||
|
||||
def main():
|
||||
try:
|
||||
gen(sys.argv[1])
|
||||
except:
|
||||
raise RuntimeError("Unsupported binding %s" % sys.argv[1])
|
||||
lang = sys.argv[1]
|
||||
if not lang in template:
|
||||
raise RuntimeError("Unsupported binding %s" % lang)
|
||||
gen(sys.argv[1])
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
|
Reference in New Issue
Block a user