Rizin
unix-like reverse engineering framework and cli tools
const_generator.py
Go to the documentation of this file.
1 # Capstone Disassembler Engine
2 # By Dang Hoang Vu, 2013
3 from __future__ import print_function
4 import sys, re
5 
6 INCL_DIR = '../include/capstone/'
7 
8 include = [ 'arm.h', 'arm64.h', 'm68k.h', 'mips.h', 'x86.h', 'ppc.h', 'sparc.h', 'systemz.h', 'xcore.h', 'tms320c64x.h', 'm680x.h', 'evm.h' ]
9 
10 template = {
11  'java': {
12  'header': "// For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT\npackage capstone;\n\npublic class %s_const {\n",
13  'footer': "}",
14  'line_format': '\tpublic static final int %s = %s;\n',
15  'out_file': './java/capstone/%s_const.java',
16  # prefixes for constant filenames of all archs - case sensitive
17  'arm.h': 'Arm',
18  'arm64.h': 'Arm64',
19  'm68k.h': 'M68k',
20  'mips.h': 'Mips',
21  'x86.h': 'X86',
22  'ppc.h': 'Ppc',
23  'sparc.h': 'Sparc',
24  'systemz.h': 'Sysz',
25  'xcore.h': 'Xcore',
26  'tms320c64x.h': 'TMS320C64x',
27  'm680x.h': 'M680x',
28  'evm.h': 'Evm',
29  'comment_open': '\t//',
30  'comment_close': '',
31  },
32  'python': {
33  'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
34  'footer': "",
35  'line_format': '%s = %s\n',
36  'out_file': './python/capstone/%s_const.py',
37  # prefixes for constant filenames of all archs - case sensitive
38  'arm.h': 'arm',
39  'arm64.h': 'arm64',
40  'm68k.h': 'm68k',
41  'mips.h': 'mips',
42  'x86.h': 'x86',
43  'ppc.h': 'ppc',
44  'sparc.h': 'sparc',
45  'systemz.h': 'sysz',
46  'xcore.h': 'xcore',
47  'tms320c64x.h': 'tms320c64x',
48  'm680x.h': 'm680x',
49  'evm.h': 'evm',
50  'comment_open': '#',
51  'comment_close': '',
52  },
53  'ocaml': {
54  'header': "(* For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.ml] *)\n",
55  'footer': "",
56  'line_format': 'let _%s = %s;;\n',
57  'out_file': './ocaml/%s_const.ml',
58  # prefixes for constant filenames of all archs - case sensitive
59  'arm.h': 'arm',
60  'arm64.h': 'arm64',
61  'mips.h': 'mips',
62  'm68k.h': 'm68k',
63  'x86.h': 'x86',
64  'ppc.h': 'ppc',
65  'sparc.h': 'sparc',
66  'systemz.h': 'sysz',
67  'xcore.h': 'xcore',
68  'tms320c64x.h': 'tms320c64x',
69  'm680x.h': 'm680x',
70  'evm.h': 'evm',
71  'comment_open': '(*',
72  'comment_close': ' *)',
73  },
74 }
75 
76 # markup for comments to be added to autogen files
77 MARKUP = '//>'
78 
79 def gen(lang):
80  global include, INCL_DIR
81  print('Generating bindings for', lang)
82  templ = template[lang]
83  print('Generating bindings for', lang)
84  for target in include:
85  prefix = templ[target]
86  outfile = open(templ['out_file'] %(prefix), 'wb') # open as binary prevents windows newlines
87  outfile.write((templ['header'] % (prefix)).encode("utf-8"))
88 
89  lines = open(INCL_DIR + target).readlines()
90 
91  count = 0
92  for line in lines:
93  line = line.strip()
94 
95  if line.startswith(MARKUP): # markup for comments
96  outfile.write(("\n%s%s%s\n" %(templ['comment_open'], \
97  line.replace(MARKUP, ''), \
98  templ['comment_close']) ).encode("utf-8"))
99  continue
100 
101  if line == '' or line.startswith('//'):
102  continue
103 
104  if line.startswith('#define '):
105  line = line[8:] #cut off define
106  xline = re.split('\s+', line, 1) #split to at most 2 express
107  if len(xline) != 2:
108  continue
109  if '(' in xline[0] or ')' in xline[0]: #does it look like a function
110  continue
111  xline.insert(1, '=') # insert an = so the expression below can parse it
112  line = ' '.join(xline)
113 
114  if not line.startswith(prefix.upper()):
115  continue
116 
117  tmp = line.strip().split(',')
118  for t in tmp:
119  t = t.strip()
120  if not t or t.startswith('//'): continue
121  # hacky: remove type cast (uint64_t)
122  t = t.replace('(uint64_t)', '')
123  t = re.sub(r'\‍((\d+)ULL << (\d+)\‍)', r'\1 << \2', t) # (1ULL<<1) to 1 << 1
124  f = re.split('\s+', t)
125 
126  if f[0].startswith(prefix.upper()):
127  if len(f) > 1 and f[1] not in ('//', '///<', '='):
128  print("Error: Unable to convert %s" % f)
129  continue
130  elif len(f) > 1 and f[1] == '=':
131  rhs = ''.join(f[2:])
132  else:
133  rhs = str(count)
134  count += 1
135 
136  try:
137  count = int(rhs) + 1
138  if (count == 1):
139  outfile.write(("\n").encode("utf-8"))
140  except ValueError:
141  if lang == 'ocaml':
142  # ocaml uses lsl for '<<', lor for '|'
143  rhs = rhs.replace('<<', ' lsl ')
144  rhs = rhs.replace('|', ' lor ')
145  # ocaml variable has _ as prefix
146  if rhs[0].isalpha():
147  rhs = '_' + rhs
148 
149  outfile.write((templ['line_format'] %(f[0].strip(), rhs)).encode("utf-8"))
150 
151  outfile.write((templ['footer']).encode("utf-8"))
152  outfile.close()
153 
154 def main():
155  try:
156  if sys.argv[1] == 'all':
157  for key in template.keys():
158  gen(key)
159  else:
160  gen(sys.argv[1])
161  except:
162  raise RuntimeError("Unsupported binding %s" % sys.argv[1])
163 
164 if __name__ == "__main__":
165  if len(sys.argv) < 2:
166  print("Usage:", sys.argv[0], " <bindings: java|python|ocaml|all>")
167  sys.exit(1)
168  main()
size_t len
Definition: 6502dis.c:15
static void encode(size_t size, lzma_action action)
Definition: full_flush.c:25
#define isalpha(c)
Definition: safe-ctype.h:125
static int
Definition: sfsocketcall.h:114