-
Notifications
You must be signed in to change notification settings - Fork 54
/
make_opcode_doc.py
executable file
·147 lines (114 loc) · 4.57 KB
/
make_opcode_doc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python -B
""" Usage: make_opcode_doc.py PATH_TO_MOZILLA_CENTRAL
This script generates SpiderMonkey bytecode documentation
from js/src/vm/Opcodes.h.
Output is written to stdout and should be pasted into the following
MDN page:
https://developer.mozilla.org/en-US/docs/SpiderMonkey/Internals/Bytecode
"""
from __future__ import print_function
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
import jsopcode
from xml.sax.saxutils import escape
SOURCE_BASE = 'http://dxr.mozilla.org/mozilla-central/source'
def override(value, override_value):
if override_value != '':
return override_value
return value
def format_flags(flags):
flags = filter(lambda x: x != 'JOF_BYTE', flags)
if len(flags) == 0:
return ''
flags = map(lambda x: x.replace('JOF_', ''), flags)
return ' ({flags})'.format(flags=', '.join(flags))
def print_opcode(opcode):
names_template = '{name} [-{nuses}, +{ndefs}]{flags}'
opcodes = sorted([opcode] + opcode.group,
key=lambda opcode: opcode.name)
names = map(lambda code: names_template.format(name=escape(code.name),
nuses=override(code.nuses,
opcode.nuses_override),
ndefs=override(code.ndefs,
opcode.ndefs_override),
flags=format_flags(code.flags)),
opcodes)
if len(opcodes) == 1:
values = ['{value} (0x{value:02x})'.format(value=opcode.value)]
else:
values_template = '{name}: {value} (0x{value:02x})'
values = map(lambda code: values_template.format(name=escape(code.name),
value=code.value),
opcodes)
print("""<dt id="{id}">{names}</dt>
<dd>
<table class="standard-table">
<tbody>
<tr><th>Value</th><td><code>{values}</code></td></tr>
<tr><th>Operands</th><td><code>{operands}</code></td></tr>
<tr><th>Length</th><td><code>{length}</code></td></tr>
<tr><th>Stack Uses</th><td><code>{stack_uses}</code></td></tr>
<tr><th>Stack Defs</th><td><code>{stack_defs}</code></td></tr>
</tbody>
</table>
{desc}
</dd>
""".format(id=opcodes[0].name,
names='<br>'.join(names),
values='<br>'.join(values),
operands=escape(opcode.operands) or " ",
length=escape(override(opcode.length,
opcode.length_override)),
stack_uses=escape(opcode.stack_uses) or " ",
stack_defs=escape(opcode.stack_defs) or " ",
desc=opcode.desc)) # desc is already escaped
id_cache = dict()
id_count = dict()
def make_element_id(category, type=''):
key = '{}:{}'.format(category, type)
if key in id_cache:
return id_cache[key]
if type == '':
id = category.replace(' ', '_')
else:
id = type.replace(' ', '_')
if id in id_count:
id_count[id] += 1
id = '{}_{}'.format(id, id_count[id])
else:
id_count[id] = 1
id_cache[key] = id
return id
def print_doc(index):
print("""<div>{{{{SpiderMonkeySidebar("Internals")}}}}</div>
<h2 id="Bytecode_Listing">Bytecode Listing</h2>
<p>This document is automatically generated from
<a href="{source_base}/js/src/vm/Opcodes.h">Opcodes.h</a> by
<a href="{source_base}/js/src/vm/make_opcode_doc.py">make_opcode_doc.py</a>.</p>
""".format(source_base=SOURCE_BASE))
for (category_name, types) in index:
print('<h3 id="{id}">{name}</h3>'.format(name=category_name,
id=make_element_id(category_name)))
for (type_name, opcodes) in types:
if type_name:
print('<h4 id="{id}">{name}</h4>'.format(
name=type_name,
id=make_element_id(category_name, type_name)))
print('<dl>')
for opcode_ in sorted(opcodes,
key=lambda opcode: opcode.sort_key):
print_opcode(opcode_)
print('</dl>')
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: make_opcode_doc.py PATH_TO_MOZILLA_CENTRAL",
file=sys.stderr)
sys.exit(1)
dir = sys.argv[1]
try:
index, _ = jsopcode.get_opcodes(dir)
except Exception as e:
print("Error: {}".format(e.args[0]), file=sys.stderr)
sys.exit(1)
print_doc(index)