Rizin
unix-like reverse engineering framework and cli tools
const_generator Namespace Reference

Functions

def gen (lang)
 
def main ()
 

Variables

string INCL_DIR = '../include/capstone/'
 
list 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' ]
 
dictionary template
 
string MARKUP = '//>'
 

Function Documentation

◆ gen()

def const_generator.gen (   lang)

Definition at line 79 of file const_generator.py.

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 
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

References encode(), int, isalpha, len, cmd_descs_generate.str, and cmd_descs_generate.strip().

Referenced by main().

◆ main()

def const_generator.main ( void  )

Definition at line 154 of file const_generator.py.

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 

References gen(), and len.

Variable Documentation

◆ INCL_DIR

string const_generator.INCL_DIR = '../include/capstone/'

Definition at line 6 of file const_generator.py.

◆ include

list const_generator.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' ]

Definition at line 8 of file const_generator.py.

◆ MARKUP

string const_generator.MARKUP = '//>'

Definition at line 77 of file const_generator.py.

◆ template

dictionary const_generator.template

Definition at line 10 of file const_generator.py.