Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
RustPython/scripts/generate_opcode_metadata.py at typelock · RustPython/RustPython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
RustPython
/
RustPython
Public
Notifications
You must be signed in to change notification settings
Fork
1.5k
Star
22.3k
Code
Issues
295
Pull requests
104
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
typelock
Breadcrumbs
RustPython
/
scripts
/
generate_opcode_metadata.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
177 lines (136 loc) · 4.78 KB
typelock
Breadcrumbs
RustPython
/
scripts
/
generate_opcode_metadata.py
Copy path
Top
File metadata and controls
Code
Blame
177 lines (136 loc) · 4.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Generate Lib/_opcode_metadata.py for RustPython bytecode.
This file generates opcode metadata that is compatible with CPython 3.13.
"""
import
itertools
import
pathlib
import
re
import
typing
ROOT
=
pathlib
.
Path
(
__file__
).
parents
[
1
]
BYTECODE_FILE
=
(
ROOT
/
"crates"
/
"compiler-core"
/
"src"
/
"bytecode"
/
"instruction.rs"
)
OPCODE_METADATA_FILE
=
ROOT
/
"Lib"
/
"_opcode_metadata.py"
# Opcodes that needs to be first, regardless of their opcode ID.
PRIORITY_OPMAP
=
{
"CACHE"
,
"RESERVED"
,
"RESUME"
,
"INSTRUMENTED_LINE"
,
"ENTER_EXECUTOR"
,
}
def
to_pascal_case
(
s
:
str
)
->
str
:
res
=
re
.
sub
(
r"(?<=[a-z0-9])([A-Z])"
,
r"_\1"
,
s
)
return
re
.
sub
(
r"(\D)(\d+)$"
,
r"\1_\2"
,
res
).
upper
()
class
Opcode
(
typing
.
NamedTuple
):
rust_name
:
str
id
:
int
have_oparg
:
bool
@
property
def
cpython_name
(
self
)
->
str
:
return
to_pascal_case
(
self
.
rust_name
)
@
property
def
is_instrumented
(
self
):
return
self
.
cpython_name
.
startswith
(
"INSTRUMENTED_"
)
@
classmethod
def
from_str
(
cls
,
body
:
str
):
raw_variants
=
re
.
split
(
r"(\d+),"
,
body
.
strip
())
raw_variants
.
remove
(
""
)
for
raw_name
,
raw_id
in
itertools
.
batched
(
raw_variants
,
2
,
strict
=
True
):
have_oparg
=
"Arg<"
in
raw_name
# Hacky but works
name
=
re
.
findall
(
r"\b[A-Z][A-Za-z]*\d*\b(?=\s*[\({=])"
,
raw_name
)[
0
]
yield
cls
(
rust_name
=
name
.
strip
(),
id
=
int
(
raw_id
),
have_oparg
=
have_oparg
)
def
__lt__
(
self
,
other
:
typing
.
Self
)
->
bool
:
sprio
,
oprio
=
(
opcode
.
cpython_name
not
in
PRIORITY_OPMAP
for
opcode
in
(
self
,
other
)
)
return
(
sprio
,
self
.
id
)
<
(
oprio
,
other
.
id
)
def
extract_enum_body
(
contents
:
str
,
enum_name
:
str
)
->
str
:
res
=
re
.
search
(
f"pub enum
{
enum_name
}
"
+
r"\{(.+?)\n\}"
,
contents
,
re
.
DOTALL
)
if
not
res
:
raise
ValueError
(
f"Could not find
{
enum_name
}
enum"
)
return
"
\n
"
.
join
(
line
.
split
(
"//"
)[
0
].
strip
()
# Remove any comment. i.e. "foo // some comment"
for
line
in
res
.
group
(
1
).
splitlines
()
if
not
line
.
strip
().
startswith
(
"//"
)
# Ignore comment lines
)
def
build_deopts
(
contents
:
str
)
->
dict
[
str
,
list
[
str
]]:
raw_body
=
re
.
search
(
r"fn deopt\(self\) -> Option<Self>(.*)"
,
contents
,
re
.
DOTALL
).
group
(
1
)
body
=
"
\n
"
.
join
(
itertools
.
takewhile
(
lambda
l
:
not
l
.
startswith
(
"_ =>"
),
# Take until reaching fallback
filter
(
lambda
l
: (
not
l
.
startswith
(
(
"//"
,
"Some(match"
)
)
# Skip comments or start of match
),
map
(
str
.
strip
,
raw_body
.
splitlines
()),
),
)
).
removeprefix
(
"{"
)
depth
=
0
arms
=
[]
buf
=
[]
for
char
in
body
:
if
char
==
"{"
:
depth
+=
1
elif
char
==
"}"
:
depth
-=
1
if
depth
==
0
and
(
char
in
(
"}"
,
","
)):
arm
=
""
.
join
(
buf
).
strip
()
arms
.
append
(
arm
)
buf
=
[]
else
:
buf
.
append
(
char
)
# last arm
arms
.
append
(
""
.
join
(
buf
))
arms
=
[
arm
for
arm
in
arms
if
arm
]
deopts
=
{}
for
arm
in
arms
:
*
specialized
,
deopt
=
map
(
to_pascal_case
,
re
.
findall
(
r"Self::(\w*)\b"
,
arm
))
deopts
[
deopt
]
=
specialized
return
deopts
contents
=
BYTECODE_FILE
.
read_text
(
encoding
=
"utf-8"
)
deopts
=
build_deopts
(
contents
)
enum_body
=
"
\n
"
.
join
(
extract_enum_body
(
contents
,
enum_name
)
for
enum_name
in
(
"Instruction"
,
"PseudoInstruction"
)
)
opcodes
=
list
(
Opcode
.
from_str
(
enum_body
))
have_oparg
=
min
(
opcode
.
id
for
opcode
in
opcodes
if
opcode
.
have_oparg
)
-
1
min_instrumented
=
min
(
opcode
.
id
for
opcode
in
opcodes
if
opcode
.
is_instrumented
)
# Generate the output file
output
=
"""# This file is generated by scripts/generate_opcode_metadata.py
# for RustPython bytecode format (CPython 3.14 compatible opcode numbers).
# Do not edit!
"""
output
+=
"
\n
_specializations = {
\n
"
for
key
,
lst
in
deopts
.
items
():
output
+=
f' "
{
key
}
": [
\n
'
for
item
in
lst
:
output
+=
f' "
{
item
}
",
\n
'
output
+=
" ],
\n
"
output
+=
"}
\n
"
specialized
=
set
(
itertools
.
chain
.
from_iterable
(
deopts
.
values
()))
output
+=
"
\n
_specialized_opmap = {
\n
"
for
opcode
in
sorted
(
opcodes
,
key
=
lambda
op
:
op
.
cpython_name
):
cpython_name
=
opcode
.
cpython_name
if
cpython_name
not
in
specialized
:
continue
output
+=
f" '
{
cpython_name
}
':
{
opcode
.
id
}
,
\n
"
output
+=
"}
\n
"
output
+=
"
\n
opmap = {
\n
"
for
opcode
in
sorted
(
opcodes
):
cpython_name
=
opcode
.
cpython_name
if
cpython_name
in
specialized
:
continue
output
+=
f" '
{
cpython_name
}
':
{
opcode
.
id
}
,
\n
"
output
+=
"}
\n
"
output
+=
f"""
HAVE_ARGUMENT =
{
have_oparg
}
MIN_INSTRUMENTED_OPCODE =
{
min_instrumented
}
"""
OPCODE_METADATA_FILE
.
write_text
(
output
,
encoding
=
"utf-8"
)
You can’t perform that action at this time.