• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3#
4# genv8constants.py output_file libv8_base.a
5#
6# Emits v8dbg constants stored in libv8_base.a in a format suitable for the V8
7# ustack helper.
8#
9
10from __future__ import print_function
11import re
12import subprocess
13import sys
14import errno
15
16if len(sys.argv) != 3:
17  print("Usage: genv8constants.py outfile libv8_base.a")
18  sys.exit(2)
19
20outfile = open(sys.argv[1], 'w')
21try:
22  pipe = subprocess.Popen([ 'objdump', '-z', '-D', sys.argv[2] ],
23      bufsize=-1, stdout=subprocess.PIPE).stdout
24except OSError as e:
25  if e.errno == errno.ENOENT:
26    print('''
27      Node.js compile error: could not find objdump
28
29      Check that GNU binutils are installed and included in PATH
30      ''')
31  else:
32    print('problem running objdump: ', e.strerror)
33
34  sys.exit()
35
36pattern = re.compile('([0-9a-fA-F]{8}|[0-9a-fA-F]{16}) <(.*)>:')
37v8dbg = re.compile('^v8dbg.*$')
38numpattern = re.compile('^[0-9a-fA-F]{2} $')
39octets = 4
40
41outfile.write("""
42/*
43 * File automatically generated by genv8constants. Do not edit.
44 *
45 * The following offsets are dynamically from libv8_base.a.  See src/v8ustack.d
46 * for details on how these values are used.
47 */
48
49#ifndef V8_CONSTANTS_H
50#define V8_CONSTANTS_H
51
52""")
53
54curr_sym = None
55curr_val = 0
56curr_octet = 0
57
58def out_reset():
59  global curr_sym, curr_val, curr_octet
60  curr_sym = None
61  curr_val = 0
62  curr_octet = 0
63
64def out_define():
65  global curr_sym, curr_val, curr_octet, outfile, octets
66  if curr_sym != None:
67    wrapped_val = curr_val & 0xffffffff
68    if curr_val & 0x80000000 != 0:
69      wrapped_val = 0x100000000 - wrapped_val
70      outfile.write("#define %s -0x%x\n" % (curr_sym.upper(), wrapped_val))
71    else:
72      outfile.write("#define %s 0x%x\n" % (curr_sym.upper(), wrapped_val))
73  out_reset()
74
75for line in pipe:
76  line = line.decode('utf-8')
77  if curr_sym != None:
78    #
79    # This bit of code has nasty knowledge of the objdump text output
80    # format, but this is the most obvious robust approach.  We could almost
81    # rely on looking at numbered fields, but some instructions look very
82    # much like hex numbers (e.g., "adc"), and we don't want to risk picking
83    # those up by mistake, so we look at character-based columns instead.
84    #
85    for i in range(0, 3):
86      # 6-character margin, 2-characters + 1 space for each field
87      idx = 6 + i * 3
88      octetstr = line[idx:idx+3]
89      if curr_octet > octets:
90        break
91
92      if not numpattern.match(octetstr):
93        break
94
95      curr_val += int('0x%s' % octetstr, 16) << (curr_octet * 8)
96      curr_octet += 1
97
98  match = pattern.match(line)
99  if match is None:
100    continue
101
102  # Print previous symbol
103  out_define()
104
105  v8match = v8dbg.match(match.group(2))
106  if v8match != None:
107    out_reset()
108    curr_sym = match.group(2)
109
110# Print last symbol
111out_define()
112
113outfile.write("""
114
115#endif /* V8_CONSTANTS_H */
116""")
117
118outfile.close()
119