11cb0ef41Sopenharmony_ci#!/usr/bin/env python
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci#
41cb0ef41Sopenharmony_ci# genv8constants.py output_file libv8_base.a
51cb0ef41Sopenharmony_ci#
61cb0ef41Sopenharmony_ci# Emits v8dbg constants stored in libv8_base.a in a format suitable for the V8
71cb0ef41Sopenharmony_ci# ustack helper.
81cb0ef41Sopenharmony_ci#
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cifrom __future__ import print_function
111cb0ef41Sopenharmony_ciimport re
121cb0ef41Sopenharmony_ciimport subprocess
131cb0ef41Sopenharmony_ciimport sys
141cb0ef41Sopenharmony_ciimport errno
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciif len(sys.argv) != 3:
171cb0ef41Sopenharmony_ci  print("Usage: genv8constants.py outfile libv8_base.a")
181cb0ef41Sopenharmony_ci  sys.exit(2)
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cioutfile = open(sys.argv[1], 'w')
211cb0ef41Sopenharmony_citry:
221cb0ef41Sopenharmony_ci  pipe = subprocess.Popen([ 'objdump', '-z', '-D', sys.argv[2] ],
231cb0ef41Sopenharmony_ci      bufsize=-1, stdout=subprocess.PIPE).stdout
241cb0ef41Sopenharmony_ciexcept OSError as e:
251cb0ef41Sopenharmony_ci  if e.errno == errno.ENOENT:
261cb0ef41Sopenharmony_ci    print('''
271cb0ef41Sopenharmony_ci      Node.js compile error: could not find objdump
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci      Check that GNU binutils are installed and included in PATH
301cb0ef41Sopenharmony_ci      ''')
311cb0ef41Sopenharmony_ci  else:
321cb0ef41Sopenharmony_ci    print('problem running objdump: ', e.strerror)
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  sys.exit()
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cipattern = re.compile('([0-9a-fA-F]{8}|[0-9a-fA-F]{16}) <(.*)>:')
371cb0ef41Sopenharmony_civ8dbg = re.compile('^v8dbg.*$')
381cb0ef41Sopenharmony_cinumpattern = re.compile('^[0-9a-fA-F]{2} $')
391cb0ef41Sopenharmony_cioctets = 4
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cioutfile.write("""
421cb0ef41Sopenharmony_ci/*
431cb0ef41Sopenharmony_ci * File automatically generated by genv8constants. Do not edit.
441cb0ef41Sopenharmony_ci *
451cb0ef41Sopenharmony_ci * The following offsets are dynamically from libv8_base.a.  See src/v8ustack.d
461cb0ef41Sopenharmony_ci * for details on how these values are used.
471cb0ef41Sopenharmony_ci */
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci#ifndef V8_CONSTANTS_H
501cb0ef41Sopenharmony_ci#define V8_CONSTANTS_H
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci""")
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cicurr_sym = None
551cb0ef41Sopenharmony_cicurr_val = 0
561cb0ef41Sopenharmony_cicurr_octet = 0
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cidef out_reset():
591cb0ef41Sopenharmony_ci  global curr_sym, curr_val, curr_octet
601cb0ef41Sopenharmony_ci  curr_sym = None
611cb0ef41Sopenharmony_ci  curr_val = 0
621cb0ef41Sopenharmony_ci  curr_octet = 0
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cidef out_define():
651cb0ef41Sopenharmony_ci  global curr_sym, curr_val, curr_octet, outfile, octets
661cb0ef41Sopenharmony_ci  if curr_sym != None:
671cb0ef41Sopenharmony_ci    wrapped_val = curr_val & 0xffffffff
681cb0ef41Sopenharmony_ci    if curr_val & 0x80000000 != 0:
691cb0ef41Sopenharmony_ci      wrapped_val = 0x100000000 - wrapped_val
701cb0ef41Sopenharmony_ci      outfile.write("#define %s -0x%x\n" % (curr_sym.upper(), wrapped_val))
711cb0ef41Sopenharmony_ci    else:
721cb0ef41Sopenharmony_ci      outfile.write("#define %s 0x%x\n" % (curr_sym.upper(), wrapped_val))
731cb0ef41Sopenharmony_ci  out_reset()
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_cifor line in pipe:
761cb0ef41Sopenharmony_ci  line = line.decode('utf-8')
771cb0ef41Sopenharmony_ci  if curr_sym != None:
781cb0ef41Sopenharmony_ci    #
791cb0ef41Sopenharmony_ci    # This bit of code has nasty knowledge of the objdump text output
801cb0ef41Sopenharmony_ci    # format, but this is the most obvious robust approach.  We could almost
811cb0ef41Sopenharmony_ci    # rely on looking at numbered fields, but some instructions look very
821cb0ef41Sopenharmony_ci    # much like hex numbers (e.g., "adc"), and we don't want to risk picking
831cb0ef41Sopenharmony_ci    # those up by mistake, so we look at character-based columns instead.
841cb0ef41Sopenharmony_ci    #
851cb0ef41Sopenharmony_ci    for i in range(0, 3):
861cb0ef41Sopenharmony_ci      # 6-character margin, 2-characters + 1 space for each field
871cb0ef41Sopenharmony_ci      idx = 6 + i * 3
881cb0ef41Sopenharmony_ci      octetstr = line[idx:idx+3]
891cb0ef41Sopenharmony_ci      if curr_octet > octets:
901cb0ef41Sopenharmony_ci        break
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci      if not numpattern.match(octetstr):
931cb0ef41Sopenharmony_ci        break
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci      curr_val += int('0x%s' % octetstr, 16) << (curr_octet * 8)
961cb0ef41Sopenharmony_ci      curr_octet += 1
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  match = pattern.match(line)
991cb0ef41Sopenharmony_ci  if match is None:
1001cb0ef41Sopenharmony_ci    continue
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  # Print previous symbol
1031cb0ef41Sopenharmony_ci  out_define()
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  v8match = v8dbg.match(match.group(2))
1061cb0ef41Sopenharmony_ci  if v8match != None:
1071cb0ef41Sopenharmony_ci    out_reset()
1081cb0ef41Sopenharmony_ci    curr_sym = match.group(2)
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci# Print last symbol
1111cb0ef41Sopenharmony_ciout_define()
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_cioutfile.write("""
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci#endif /* V8_CONSTANTS_H */
1161cb0ef41Sopenharmony_ci""")
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_cioutfile.close()
119