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