add composite const generator (fix #161)

This commit is contained in:
Ryan Hileman 2015-10-03 10:41:19 -07:00
parent b7c4031ed2
commit 7e7c6512f5
4 changed files with 27 additions and 4 deletions

View File

@ -93,7 +93,7 @@ def gen(lang):
# parse #define UC_TARGET (num)
define = False
if f[0] == '#define' and len(f) >= 3 and f[2].isdigit():
if f[0] == '#define' and len(f) >= 3:
define = True
f.pop(0)
f.insert(1, '=')
@ -106,25 +106,30 @@ def gen(lang):
rhs = ''.join(f[2:])
else:
rhs = str(count)
count += 1
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))
rhs = str(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)]
if not rhs.isdigit():
for k, v in previous.items():
rhs = re.sub(r'\b%s\b' % k, v, rhs)
rhs = str(eval(rhs))
lhs_strip = re.sub(r'^UC_', '', lhs)
count = int(rhs) + 1
if (count == 1):
outfile.write("\n")
outfile.write(templ['line_format'] % (lhs_strip, rhs))
previous[lhs] = rhs
previous[lhs] = str(rhs)
outfile.write(templ['footer'])
outfile.close()

View File

@ -74,6 +74,12 @@ const (
HOOK_MEM_READ = 1024
HOOK_MEM_WRITE = 2048
HOOK_MEM_FETCH = 4096
HOOK_MEM_UNMAPPED = 112
HOOK_MEM_PROT = 896
HOOK_MEM_READ_INVALID = 144
HOOK_MEM_WRITE_INVALID = 288
HOOK_MEM_FETCH_INVALID = 576
HOOK_MEM_INVALID = 1008
PROT_NONE = 0
PROT_READ = 1

View File

@ -76,6 +76,12 @@ public interface UnicornConst {
public static final int UC_HOOK_MEM_READ = 1024;
public static final int UC_HOOK_MEM_WRITE = 2048;
public static final int UC_HOOK_MEM_FETCH = 4096;
public static final int UC_HOOK_MEM_UNMAPPED = 112;
public static final int UC_HOOK_MEM_PROT = 896;
public static final int UC_HOOK_MEM_READ_INVALID = 144;
public static final int UC_HOOK_MEM_WRITE_INVALID = 288;
public static final int UC_HOOK_MEM_FETCH_INVALID = 576;
public static final int UC_HOOK_MEM_INVALID = 1008;
public static final int UC_PROT_NONE = 0;
public static final int UC_PROT_READ = 1;

View File

@ -72,6 +72,12 @@ UC_HOOK_MEM_FETCH_PROT = 512
UC_HOOK_MEM_READ = 1024
UC_HOOK_MEM_WRITE = 2048
UC_HOOK_MEM_FETCH = 4096
UC_HOOK_MEM_UNMAPPED = 112
UC_HOOK_MEM_PROT = 896
UC_HOOK_MEM_READ_INVALID = 144
UC_HOOK_MEM_WRITE_INVALID = 288
UC_HOOK_MEM_FETCH_INVALID = 576
UC_HOOK_MEM_INVALID = 1008
UC_PROT_NONE = 0
UC_PROT_READ = 1