Merge remote-tracking branch 'ry/v0.6' into v0.6-merge
[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
14 if len(sys.argv) != 3:
15   print "usage: objsym.py outfile libv8_base.a"
16   sys.exit(2);
17
18 outfile = file(sys.argv[1], 'w');
19 pipe = subprocess.Popen([ 'objdump', '-z', '-D', sys.argv[2] ],
20     stdout=subprocess.PIPE).stdout;
21 pattern = re.compile('00000000 <(v8dbg_.*)>:');
22 numpattern = re.compile('[0-9a-fA-F]{2}');
23
24 outfile.write("""
25 /*
26  * File automatically generated by genv8constants. Do not edit.
27  *
28  * The following offsets are dynamically from libv8_base.a.  See src/v8ustack.d
29  * for details on how these values are used.
30  */
31
32 #ifndef V8_CONSTANTS_H
33 #define V8_CONSTANTS_H
34
35 #if defined(__i386)
36 """);
37
38 curr_sym = None;
39 curr_val = 0;
40 curr_octet = 0;
41
42 for line in pipe:
43   if curr_sym != None:
44     #
45     # This bit of code has nasty knowledge of the objdump text output
46     # format, but this is the most obvious robust approach.  We could almost
47     # rely on looking at numbered fields, but some instructions look very
48     # much like hex numbers (e.g., "adc"), and we don't want to risk picking
49     # those up by mistake, so we look at character-based columns intead.
50     #
51     for i in range (0, 3):
52       # 6-character margin, 2-characters + 1 space for each field
53       idx = 6 + i * 3;
54       octetstr = line[idx:idx+2]
55       if not numpattern.match(octetstr):
56         break;
57
58       curr_val += int('0x%s' % octetstr, 16) << (curr_octet * 8);
59       curr_octet += 1;
60
61     if curr_octet < 4:
62       continue;
63
64     outfile.write("#define %s 0x%x\n" % (curr_sym.upper(), curr_val));
65     curr_sym = None;
66     curr_val = 0;
67     curr_octet = 0;
68     continue;
69
70   match = pattern.match(line)
71   if match == None:
72     continue;
73
74   curr_sym = match.group(1);
75
76 outfile.write("""
77 #else
78 #error "only i386 is supported for DTrace ustack helper"
79 #endif
80
81 #endif /* V8_CONSTANTS_H */
82 """);