unicorn_const_generator.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python3
  2. # Unicorn Engine
  3. # By Dang Hoang Vu, 2013
  4. # Modified for Ryujinx from: https://github.com/unicorn-engine/unicorn/blob/6c1cbef6ac505d355033aef1176b684d02e1eb3a/bindings/const_generator.py
  5. from __future__ import print_function
  6. import sys, re, os
  7. include = [ 'arm.h', 'arm64.h', 'unicorn.h' ]
  8. split_common = [ 'ARCH', 'MODE', 'ERR', 'MEM', 'TCG', 'HOOK', 'PROT' ]
  9. template = {
  10. 'dotnet': {
  11. 'header': "// Constants for Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT\n\n// ReSharper disable InconsistentNaming\nnamespace Ryujinx.Tests.Unicorn.Native.Const\n{\n public enum %s\n {\n",
  12. 'footer': " }\n}\n",
  13. 'line_format': ' %s = %s,\n',
  14. 'out_file': os.path.join(os.path.dirname(__file__), 'Native', 'Const', '%s.cs'),
  15. # prefixes for constant filenames of all archs - case sensitive
  16. 'arm.h': 'Arm',
  17. 'arm64.h': 'Arm64',
  18. 'unicorn.h': 'Common',
  19. # prefixes for filenames of split_common values - case sensitive
  20. 'ARCH': 'Arch',
  21. 'MODE': 'Mode',
  22. 'ERR': 'Error',
  23. 'MEM': 'Memory',
  24. 'TCG': 'TCG',
  25. 'HOOK': 'Hook',
  26. 'PROT': 'Permission',
  27. 'comment_open': ' //',
  28. 'comment_close': '',
  29. }
  30. }
  31. # markup for comments to be added to autogen files
  32. MARKUP = '//>'
  33. def gen(unicorn_repo_path):
  34. global include
  35. include_dir = os.path.join(unicorn_repo_path, 'include', 'unicorn')
  36. templ = template["dotnet"]
  37. for target in include:
  38. prefix = templ[target]
  39. outfile = open(templ['out_file'] %(prefix), 'wb') # open as binary prevents windows newlines
  40. outfile.write((templ['header'] % (prefix)).encode("utf-8"))
  41. if target == 'unicorn.h':
  42. prefix = ''
  43. for cat in split_common:
  44. with open(templ['out_file'] %(templ[cat]), 'wb') as file:
  45. file.write((templ['header'] %(templ[cat])).encode("utf-8"))
  46. with open(os.path.join(include_dir, target)) as f:
  47. lines = f.readlines()
  48. previous = {}
  49. count = 0
  50. skip = 0
  51. in_comment = False
  52. for lno, line in enumerate(lines):
  53. if "/*" in line:
  54. in_comment = True
  55. if "*/" in line:
  56. in_comment = False
  57. if in_comment:
  58. continue
  59. if skip > 0:
  60. # Due to clang-format, values may come up in the next line
  61. skip -= 1
  62. continue
  63. line = line.strip()
  64. if line.startswith(MARKUP): # markup for comments
  65. outfile.write(("\n%s%s%s\n" %(templ['comment_open'], \
  66. line.replace(MARKUP, ''), templ['comment_close'])).encode("utf-8"))
  67. continue
  68. if line == '' or line.startswith('//'):
  69. continue
  70. tmp = line.strip().split(',')
  71. if len(tmp) >= 2 and tmp[0] != "#define" and not tmp[0].startswith("UC_"):
  72. continue
  73. for t in tmp:
  74. t = t.strip()
  75. if not t or t.startswith('//'): continue
  76. f = re.split('\s+', t)
  77. # parse #define UC_TARGET (num)
  78. define = False
  79. if f[0] == '#define' and len(f) >= 3:
  80. define = True
  81. f.pop(0)
  82. f.insert(1, '=')
  83. if f[0].startswith("UC_" + prefix.upper()) or f[0].startswith("UC_CPU"):
  84. if len(f) > 1 and f[1] not in ('//', '='):
  85. print("WARNING: Unable to convert %s" % f)
  86. print(" Line =", line)
  87. continue
  88. elif len(f) > 1 and f[1] == '=':
  89. # Like:
  90. # UC_A =
  91. # (1 << 2)
  92. # #define UC_B \
  93. # (UC_A | UC_C)
  94. # Let's search the next line
  95. if len(f) == 2:
  96. if lno == len(lines) - 1:
  97. print("WARNING: Unable to convert %s" % f)
  98. print(" Line =", line)
  99. continue
  100. skip += 1
  101. next_line = lines[lno + 1]
  102. next_line_tmp = next_line.strip().split(",")
  103. rhs = next_line_tmp[0]
  104. elif f[-1] == "\\":
  105. idx = 0
  106. rhs = ""
  107. while True:
  108. idx += 1
  109. if lno + idx == len(lines):
  110. print("WARNING: Unable to convert %s" % f)
  111. print(" Line =", line)
  112. continue
  113. skip += 1
  114. next_line = lines[lno + idx]
  115. next_line_f = re.split('\s+', next_line.strip())
  116. if next_line_f[-1] == "\\":
  117. rhs += "".join(next_line_f[:-1])
  118. else:
  119. rhs += next_line.strip()
  120. break
  121. else:
  122. rhs = ''.join(f[2:])
  123. else:
  124. rhs = str(count)
  125. lhs = f[0].strip()
  126. #print(f'lhs: {lhs} rhs: {rhs} f:{f}')
  127. # evaluate bitshifts in constants e.g. "UC_X86 = 1 << 1"
  128. match = re.match(r'(?P<rhs>\s*\d+\s*<<\s*\d+\s*)', rhs)
  129. if match:
  130. rhs = str(eval(match.group(1)))
  131. else:
  132. # evaluate references to other constants e.g. "UC_ARM_REG_X = UC_ARM_REG_SP"
  133. match = re.match(r'^([^\d]\w+)$', rhs)
  134. if match:
  135. rhs = previous[match.group(1)]
  136. if not rhs.isdigit():
  137. for k, v in previous.items():
  138. rhs = re.sub(r'\b%s\b' % k, v, rhs)
  139. rhs = str(eval(rhs))
  140. lhs_strip = re.sub(r'^UC_', '', lhs)
  141. count = int(rhs) + 1
  142. if target == "unicorn.h":
  143. matched_cat = False
  144. for cat in split_common:
  145. if lhs_strip.startswith(f"{cat}_"):
  146. with open(templ['out_file'] %(templ[cat]), 'ab') as cat_file:
  147. cat_lhs_strip = lhs_strip
  148. if not lhs_strip.lstrip(f"{cat}_").isnumeric():
  149. cat_lhs_strip = lhs_strip.replace(f"{cat}_", "", 1)
  150. cat_file.write(
  151. (templ['line_format'] % (cat_lhs_strip, rhs)).encode("utf-8"))
  152. matched_cat = True
  153. break
  154. if matched_cat:
  155. previous[lhs] = str(rhs)
  156. continue
  157. if (count == 1):
  158. outfile.write(("\n").encode("utf-8"))
  159. if lhs_strip.startswith(f"{prefix.upper()}_") and not lhs_strip.replace(f"{prefix.upper()}_", "", 1).isnumeric():
  160. lhs_strip = lhs_strip.replace(f"{prefix.upper()}_", "", 1)
  161. outfile.write((templ['line_format'] % (lhs_strip, rhs)).encode("utf-8"))
  162. previous[lhs] = str(rhs)
  163. outfile.write((templ['footer']).encode("utf-8"))
  164. outfile.close()
  165. if target == "unicorn.h":
  166. for cat in split_common:
  167. with open(templ['out_file'] %(templ[cat]), 'ab') as cat_file:
  168. cat_file.write(templ['footer'].encode('utf-8'))
  169. if __name__ == "__main__":
  170. if len(sys.argv) < 2:
  171. print("Usage:", sys.argv[0], " <path to unicorn repo>")
  172. sys.exit(1)
  173. unicorn_repo_path = sys.argv[1]
  174. if os.path.isdir(unicorn_repo_path):
  175. print("Generating constants for dotnet")
  176. gen(unicorn_repo_path)
  177. else:
  178. print("Couldn't find unicorn repo at:", unicorn_repo_path)