Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-105481: Generate the opcode lists in dis from data extracted from bytecodes.c #106758

Merged
merged 30 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
36a7d56
gh-105481: add haslocal to _opcode.py. Generate most oplists in opcod…
iritkatriel Jul 14, 2023
75019ca
added HAS_FREE flag. Removed HAS_FREE from the HAS_LOCAL list
iritkatriel Jul 14, 2023
494680a
add hasjump, soft deprecate hasjrel and hasjabs. Fix build bug
iritkatriel Jul 15, 2023
24786fb
generate hasexc from the C macros instead of defining it again
iritkatriel Jul 15, 2023
563b431
typo
iritkatriel Jul 15, 2023
227756f
Merge remote-tracking branch 'upstream/main' into opcode_py
iritkatriel Jul 17, 2023
1b13b96
ascii quotes
iritkatriel Jul 17, 2023
0f5ae32
make clinic
iritkatriel Jul 17, 2023
289fad1
remove oplists
iritkatriel Jul 17, 2023
598ba2a
import less from opcode in build script. _opcode.has_* don't exist pr…
iritkatriel Jul 17, 2023
d2d355e
hascompare in old versions as well
iritkatriel Jul 17, 2023
27eb2cc
remove redundant init
iritkatriel Jul 17, 2023
998024a
📜🤖 Added by blurb_it.
blurb-it[bot] Jul 17, 2023
ffa4ee3
Merge branch 'main' into opcode_py
iritkatriel Jul 17, 2023
ce76a60
remove unnecessary comment
iritkatriel Jul 17, 2023
4194a12
make the tests more precise
iritkatriel Jul 17, 2023
6b8c46b
sort the lists
iritkatriel Jul 17, 2023
e15f7d5
address some of the code review comments
iritkatriel Jul 18, 2023
21fe0a4
remove hard-coded lists from the tests
iritkatriel Jul 18, 2023
75795db
remove opcode metadata file from generated files list so diff are vis…
iritkatriel Jul 18, 2023
1a0a2b2
import _opcode directly into dis, rather than via opcode
iritkatriel Jul 18, 2023
f530469
add stack effect to dis.__all__
iritkatriel Jul 18, 2023
71ffe34
Merge branch 'main' into opcode_py
iritkatriel Jul 18, 2023
d525c53
import _specializations, _specialized_instructions directly from _opc…
iritkatriel Jul 18, 2023
5e1c4a4
update Tools/scripts/summarize_stats.py to use _opcode_metadata inste…
iritkatriel Jul 18, 2023
6077263
Revert "update Tools/scripts/summarize_stats.py to use _opcode_metada…
iritkatriel Jul 18, 2023
98ba11f
Revert "import _specializations, _specialized_instructions directly f…
iritkatriel Jul 18, 2023
cc8e5e1
Revert "add stack effect to dis.__all__"
iritkatriel Jul 18, 2023
31dbfec
Revert "import _opcode directly into dis, rather than via opcode"
iritkatriel Jul 18, 2023
1abf6ca
Merge branch 'main' into opcode_py
iritkatriel Jul 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 21 additions & 7 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
adaptive bytecode can be shown by passing ``adaptive=True``.


Example: Given the function :func:`myfunc`::

Check warning on line 46 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:func reference target not found: myfunc

def myfunc(alist):
return len(alist)

the following command can be used to display the disassembly of

Check warning on line 51 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:func reference target not found: myfunc
:func:`myfunc`:

.. doctest::
Expand Down Expand Up @@ -851,7 +851,7 @@

.. opcode:: LOAD_BUILD_CLASS

Pushes :func:`builtins.__build_class__` onto the stack. It is later called

Check warning on line 854 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:func reference target not found: builtins.__build_class__
to construct a class.


Expand Down Expand Up @@ -908,14 +908,14 @@

.. opcode:: STORE_NAME (namei)

Implements ``name = STACK.pop()``. *namei* is the index of *name* in the attribute

Check warning on line 911 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: co_names
:attr:`co_names` of the code object. The compiler tries to use
:opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.


.. opcode:: DELETE_NAME (namei)

Implements ``del name``, where *namei* is the index into :attr:`co_names`

Check warning on line 918 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: co_names
attribute of the code object.


Expand Down Expand Up @@ -954,7 +954,7 @@
value = STACK.pop()
obj.name = value

where *namei* is the index of name in :attr:`co_names`.

Check warning on line 957 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: co_names

.. opcode:: DELETE_ATTR (namei)

Expand All @@ -963,7 +963,7 @@
obj = STACK.pop()
del obj.name

where *namei* is the index of name into :attr:`co_names`.

Check warning on line 966 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: co_names


.. opcode:: STORE_GLOBAL (namei)
Expand Down Expand Up @@ -1803,15 +1803,12 @@
Sequence of bytecodes that access an attribute by name.


.. data:: hasjrel

Sequence of bytecodes that have a relative jump target.
.. data:: hasjump

Sequence of bytecodes that have a jump target. All jumps
are relative.

.. data:: hasjabs

Sequence of bytecodes that have an absolute jump target.

.. versionadded:: 3.13

.. data:: haslocal

Expand All @@ -1827,3 +1824,20 @@
Sequence of bytecodes that set an exception handler.

.. versionadded:: 3.12


.. data:: hasjrel

Sequence of bytecodes that have a relative jump target.

.. deprecated:: 3.13
All jumps are now relative. Use :data:`hasjump`.


.. data:: hasjabs

Sequence of bytecodes that have an absolute jump target.

.. deprecated:: 3.13
All jumps are now relative. This list is empty.

4 changes: 4 additions & 0 deletions Include/cpython/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ PyAPI_FUNC(int) PyUnstable_OpcodeHasArg(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasConst(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasName(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasJump(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasFree(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasLocal(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasExc(int opcode);

36 changes: 20 additions & 16 deletions Include/internal/pycore_opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,10 +952,14 @@ enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC00, INSTR_FMT
#define HAS_CONST_FLAG (2)
#define HAS_NAME_FLAG (4)
#define HAS_JUMP_FLAG (8)
#define HAS_FREE_FLAG (16)
#define HAS_LOCAL_FLAG (32)
#define OPCODE_HAS_ARG(OP) (_PyOpcode_opcode_metadata[OP].flags & (HAS_ARG_FLAG))
#define OPCODE_HAS_CONST(OP) (_PyOpcode_opcode_metadata[OP].flags & (HAS_CONST_FLAG))
#define OPCODE_HAS_NAME(OP) (_PyOpcode_opcode_metadata[OP].flags & (HAS_NAME_FLAG))
#define OPCODE_HAS_JUMP(OP) (_PyOpcode_opcode_metadata[OP].flags & (HAS_JUMP_FLAG))
#define OPCODE_HAS_FREE(OP) (_PyOpcode_opcode_metadata[OP].flags & (HAS_FREE_FLAG))
#define OPCODE_HAS_LOCAL(OP) (_PyOpcode_opcode_metadata[OP].flags & (HAS_LOCAL_FLAG))

struct opcode_metadata {
bool valid_entry;
Expand Down Expand Up @@ -992,16 +996,16 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[OPCODE_METADATA_SIZE] = {
[NOP] = { true, INSTR_FMT_IX, 0 },
[RESUME] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[INSTRUMENTED_RESUME] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[LOAD_CLOSURE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[LOAD_FAST_CHECK] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[LOAD_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[LOAD_FAST_AND_CLEAR] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[LOAD_FAST_LOAD_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[LOAD_CLOSURE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[LOAD_FAST_CHECK] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[LOAD_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[LOAD_FAST_AND_CLEAR] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[LOAD_FAST_LOAD_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[LOAD_CONST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_CONST_FLAG },
[STORE_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[STORE_FAST_MAYBE_NULL] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[STORE_FAST_LOAD_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[STORE_FAST_STORE_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[STORE_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[STORE_FAST_MAYBE_NULL] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[STORE_FAST_LOAD_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[STORE_FAST_STORE_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[POP_TOP] = { true, INSTR_FMT_IX, 0 },
[PUSH_NULL] = { true, INSTR_FMT_IX, 0 },
[END_FOR] = { true, INSTR_FMT_IB, 0 },
Expand All @@ -1025,7 +1029,7 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[OPCODE_METADATA_SIZE] = {
[BINARY_OP_ADD_FLOAT] = { true, INSTR_FMT_IBC, 0 },
[BINARY_OP_SUBTRACT_FLOAT] = { true, INSTR_FMT_IBC, 0 },
[BINARY_OP_ADD_UNICODE] = { true, INSTR_FMT_IBC, 0 },
[BINARY_OP_INPLACE_ADD_UNICODE] = { true, INSTR_FMT_IB, 0 },
[BINARY_OP_INPLACE_ADD_UNICODE] = { true, INSTR_FMT_IB, HAS_LOCAL_FLAG },
[BINARY_SUBSCR] = { true, INSTR_FMT_IXC, 0 },
[BINARY_SLICE] = { true, INSTR_FMT_IX, 0 },
[STORE_SLICE] = { true, INSTR_FMT_IX, 0 },
Expand Down Expand Up @@ -1077,12 +1081,12 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[OPCODE_METADATA_SIZE] = {
[LOAD_GLOBAL] = { true, INSTR_FMT_IBC000, HAS_ARG_FLAG | HAS_NAME_FLAG },
[LOAD_GLOBAL_MODULE] = { true, INSTR_FMT_IBC000, HAS_ARG_FLAG },
[LOAD_GLOBAL_BUILTIN] = { true, INSTR_FMT_IBC000, HAS_ARG_FLAG },
[DELETE_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[MAKE_CELL] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[DELETE_DEREF] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[LOAD_FROM_DICT_OR_DEREF] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[LOAD_DEREF] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[STORE_DEREF] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[DELETE_FAST] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_LOCAL_FLAG },
[MAKE_CELL] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_FREE_FLAG },
[DELETE_DEREF] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_FREE_FLAG },
[LOAD_FROM_DICT_OR_DEREF] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_FREE_FLAG },
[LOAD_DEREF] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_FREE_FLAG },
[STORE_DEREF] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_FREE_FLAG },
[COPY_FREE_VARS] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[BUILD_STRING] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
[BUILD_TUPLE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG },
Expand Down
120 changes: 43 additions & 77 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
operate on bytecodes (e.g. peephole optimizers).
"""

__all__ = ["cmp_op", "hasarg", "hasconst", "hasname", "hasjrel", "hasjabs",
"haslocal", "hascompare", "hasfree", "hasexc", "opname", "opmap",
"stack_effect", "HAVE_ARGUMENT", "EXTENDED_ARG"]
__all__ = ["cmp_op", "opname", "opmap", "stack_effect", "hascompare",
"HAVE_ARGUMENT", "EXTENDED_ARG"]
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved

import _opcode
from _opcode import stack_effect

import sys
Expand All @@ -17,25 +17,12 @@

cmp_op = ('<', '<=', '==', '!=', '>', '>=')

hasarg = []
hasconst = []
hasname = []
hasjrel = []
hasjabs = []
haslocal = []
hascompare = []
hasfree = []
hasexc = []


ENABLE_SPECIALIZATION = True

def is_pseudo(op):
return op >= MIN_PSEUDO_OPCODE and op <= MAX_PSEUDO_OPCODE

oplists = [hasarg, hasconst, hasname, hasjrel, hasjabs,
haslocal, hascompare, hasfree, hasexc]

opmap = {}

## pseudo opcodes (used in the compiler) mapped to the values
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -45,27 +32,9 @@ def is_pseudo(op):
def def_op(name, op):
opmap[name] = op

def name_op(name, op):
def_op(name, op)
hasname.append(op)

def jrel_op(name, op):
def_op(name, op)
hasjrel.append(op)

def jabs_op(name, op):
def_op(name, op)
hasjabs.append(op)

def pseudo_op(name, op, real_ops):
def_op(name, op)
_pseudo_ops[name] = real_ops
# add the pseudo opcode to the lists its targets are in
for oplist in oplists:
res = [opmap[rop] in oplist for rop in real_ops]
if any(res):
assert all(res)
oplist.append(op)


# Instruction opcodes for compiled code
Expand Down Expand Up @@ -137,74 +106,61 @@ def pseudo_op(name, op, real_ops):

HAVE_ARGUMENT = 90 # real opcodes from here have an argument:

name_op('STORE_NAME', 90) # Index in name list
name_op('DELETE_NAME', 91) # ""
def_op('STORE_NAME', 90) # Index in name list
def_op('DELETE_NAME', 91) # ""
def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
jrel_op('FOR_ITER', 93)
def_op('FOR_ITER', 93)
def_op('UNPACK_EX', 94)
name_op('STORE_ATTR', 95) # Index in name list
name_op('DELETE_ATTR', 96) # ""
name_op('STORE_GLOBAL', 97) # ""
name_op('DELETE_GLOBAL', 98) # ""
def_op('STORE_ATTR', 95) # Index in name list
def_op('DELETE_ATTR', 96) # ""
def_op('STORE_GLOBAL', 97) # ""
def_op('DELETE_GLOBAL', 98) # ""
def_op('SWAP', 99)
def_op('LOAD_CONST', 100) # Index in const list
hasconst.append(100)
name_op('LOAD_NAME', 101) # Index in name list
def_op('LOAD_NAME', 101) # Index in name list
def_op('BUILD_TUPLE', 102) # Number of tuple items
def_op('BUILD_LIST', 103) # Number of list items
def_op('BUILD_SET', 104) # Number of set items
def_op('BUILD_MAP', 105) # Number of dict entries
name_op('LOAD_ATTR', 106) # Index in name list
def_op('LOAD_ATTR', 106) # Index in name list
def_op('COMPARE_OP', 107) # Comparison operator
hascompare.append(107)
name_op('IMPORT_NAME', 108) # Index in name list
name_op('IMPORT_FROM', 109) # Index in name list
jrel_op('JUMP_FORWARD', 110) # Number of words to skip

jrel_op('POP_JUMP_IF_FALSE', 114)
jrel_op('POP_JUMP_IF_TRUE', 115)
name_op('LOAD_GLOBAL', 116) # Index in name list
def_op('IMPORT_NAME', 108) # Index in name list
def_op('IMPORT_FROM', 109) # Index in name list
def_op('JUMP_FORWARD', 110) # Number of words to skip

def_op('POP_JUMP_IF_FALSE', 114)
def_op('POP_JUMP_IF_TRUE', 115)
def_op('LOAD_GLOBAL', 116) # Index in name list
def_op('IS_OP', 117)
def_op('CONTAINS_OP', 118)
def_op('RERAISE', 119)
def_op('COPY', 120)
def_op('RETURN_CONST', 121)
hasconst.append(121)
def_op('BINARY_OP', 122)
jrel_op('SEND', 123) # Number of words to skip
def_op('SEND', 123) # Number of words to skip
def_op('LOAD_FAST', 124) # Local variable number, no null check
haslocal.append(124)
def_op('STORE_FAST', 125) # Local variable number
haslocal.append(125)
def_op('DELETE_FAST', 126) # Local variable number
haslocal.append(126)
def_op('LOAD_FAST_CHECK', 127) # Local variable number
haslocal.append(127)
jrel_op('POP_JUMP_IF_NOT_NONE', 128)
jrel_op('POP_JUMP_IF_NONE', 129)
def_op('POP_JUMP_IF_NOT_NONE', 128)
def_op('POP_JUMP_IF_NONE', 129)
def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
def_op('GET_AWAITABLE', 131)
def_op('BUILD_SLICE', 133) # Number of items
jrel_op('JUMP_BACKWARD_NO_INTERRUPT', 134) # Number of words to skip (backwards)
def_op('JUMP_BACKWARD_NO_INTERRUPT', 134) # Number of words to skip (backwards)
def_op('MAKE_CELL', 135)
hasfree.append(135)
def_op('LOAD_DEREF', 137)
hasfree.append(137)
def_op('STORE_DEREF', 138)
hasfree.append(138)
def_op('DELETE_DEREF', 139)
hasfree.append(139)
jrel_op('JUMP_BACKWARD', 140) # Number of words to skip (backwards)
name_op('LOAD_SUPER_ATTR', 141)
def_op('JUMP_BACKWARD', 140) # Number of words to skip (backwards)
def_op('LOAD_SUPER_ATTR', 141)
def_op('CALL_FUNCTION_EX', 142) # Flags
def_op('LOAD_FAST_AND_CLEAR', 143) # Local variable number
haslocal.append(143)
def_op('EXTENDED_ARG', 144)
EXTENDED_ARG = 144
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
def_op('LIST_APPEND', 145)
def_op('SET_ADD', 146)
def_op('MAP_ADD', 147)
hasfree.append(148)
def_op('COPY_FREE_VARS', 149)
def_op('YIELD_VALUE', 150)
def_op('RESUME', 151) # This must be kept in sync with deepfreeze.py
Expand All @@ -224,12 +180,10 @@ def pseudo_op(name, op, real_ops):
def_op('STORE_FAST_STORE_FAST', 170)
def_op('CALL', 171)
def_op('KW_NAMES', 172)
hasconst.append(172)
def_op('CALL_INTRINSIC_1', 173)
def_op('CALL_INTRINSIC_2', 174)
name_op('LOAD_FROM_DICT_OR_GLOBALS', 175)
def_op('LOAD_FROM_DICT_OR_GLOBALS', 175)
def_op('LOAD_FROM_DICT_OR_DEREF', 176)
hasfree.append(176)
def_op('SET_FUNCTION_ATTRIBUTE', 177) # Attribute

# Optimizer hook
Expand Down Expand Up @@ -258,16 +212,12 @@ def pseudo_op(name, op, real_ops):
def_op('INSTRUMENTED_LINE', 254)
# 255 is reserved

hasarg.extend([op for op in opmap.values() if op >= HAVE_ARGUMENT])

MIN_PSEUDO_OPCODE = 256

pseudo_op('SETUP_FINALLY', 256, ['NOP'])
hasexc.append(256)
pseudo_op('SETUP_CLEANUP', 257, ['NOP'])
hasexc.append(257)
pseudo_op('SETUP_WITH', 258, ['NOP'])
hasexc.append(258)
pseudo_op('POP_BLOCK', 259, ['NOP'])

pseudo_op('JUMP', 260, ['JUMP_FORWARD', 'JUMP_BACKWARD'])
Expand All @@ -283,12 +233,28 @@ def pseudo_op(name, op, real_ops):

MAX_PSEUDO_OPCODE = MIN_PSEUDO_OPCODE + len(_pseudo_ops) - 1

del def_op, name_op, jrel_op, jabs_op, pseudo_op
del def_op, pseudo_op

opname = ['<%r>' % (op,) for op in range(MAX_PSEUDO_OPCODE + 1)]
for op, i in opmap.items():
opname[i] = op

# The build uses older versions of Python which do not have _opcode.has_* functions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite mysterious. IIRC Brandt (?) wrote some hack that reads opcode.py and then execs it? But I can't find it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in Tools/build/generate_opcode_h.py. But when it execs opcode.py that tries to import _opcode and call its has_arg etc to construct the oplists (which are not used during the build).

Ideally we should just get rid of generate_opcode_h.py if we can.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need to import _opcode into opcode at all. We can just import it directly into dis (which is not used by any build scripts). I'll try that.

if sys.version_info[:2] >= (3, 13):
hasarg = [op for op in opmap.values() if _opcode.has_arg(op)]
hasconst = [op for op in opmap.values() if _opcode.has_const(op)]
hasname = [op for op in opmap.values() if _opcode.has_name(op)]
hasjump = [op for op in opmap.values() if _opcode.has_jump(op)]
hasjrel = hasjump # for backward compatibility
hasjabs = []
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
hasfree = [op for op in opmap.values() if _opcode.has_free(op)]
haslocal = [op for op in opmap.values() if _opcode.has_local(op)]
hasexc = [op for op in opmap.values() if _opcode.has_exc(op)]

__all__.extend(["hasarg", "hasconst", "hasname", "hasjump", "hasjrel",
"hasjabs", "hasfree", "haslocal", "hasexc"])
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved

hascompare = [opmap["COMPARE_OP"]] # for backwards compatibility
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved

_nb_ops = [
("NB_ADD", "+"),
Expand Down
33 changes: 22 additions & 11 deletions Lib/test/test__opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def test_invalid_opcodes(self):
self.check_bool_function_result(_opcode.has_const, invalid, False)
self.check_bool_function_result(_opcode.has_name, invalid, False)
self.check_bool_function_result(_opcode.has_jump, invalid, False)
self.check_bool_function_result(_opcode.has_free, invalid, False)
self.check_bool_function_result(_opcode.has_local, invalid, False)
self.check_bool_function_result(_opcode.has_exc, invalid, False)

def test_is_valid(self):
names = [
Expand Down Expand Up @@ -61,17 +64,25 @@ def test_has_jump(self):
self.check_bool_function_result(_opcode.has_jump, has_jump, True)
self.check_bool_function_result(_opcode.has_jump, no_jump, False)

# the following test is part of the refactor, it will be removed soon
def test_against_legacy_bool_values(self):
# limiting to ops up to ENTER_EXECUTOR, because everything after that
# is not currently categorized correctly in opcode.py.
for op in range(0, opcode.opmap['ENTER_EXECUTOR']):
with self.subTest(op=op):
if opcode.opname[op] != f'<{op}>':
self.assertEqual(op in dis.hasarg, _opcode.has_arg(op))
self.assertEqual(op in dis.hasconst, _opcode.has_const(op))
self.assertEqual(op in dis.hasname, _opcode.has_name(op))
self.assertEqual(op in dis.hasjrel, _opcode.has_jump(op))
def test_has_free(self):
has_free = ['MAKE_CELL', 'LOAD_DEREF', 'STORE_DEREF', 'DELETE_DEREF',
'LOAD_FROM_DICT_OR_DEREF']
no_free = ['SETUP_WITH', 'POP_TOP', 'NOP', 'CACHE']
self.check_bool_function_result(_opcode.has_free, has_free, True)
self.check_bool_function_result(_opcode.has_free, no_free, False)

def test_has_local(self):
has_local = ['LOAD_FAST', 'LOAD_FAST_CHECK', 'LOAD_FAST_AND_CLEAR',
'STORE_FAST_MAYBE_NULL', 'LOAD_CLOSURE']
no_local = ['SETUP_WITH', 'POP_TOP', 'NOP', 'CACHE']
self.check_bool_function_result(_opcode.has_local, has_local, True)
self.check_bool_function_result(_opcode.has_local, no_local, False)

def test_has_exc(self):
has_exc = ['SETUP_FINALLY', 'SETUP_WITH', 'SETUP_CLEANUP']
no_exc = ['DELETE_DEREF', 'POP_TOP', 'NOP', 'CACHE']
self.check_bool_function_result(_opcode.has_exc, has_exc, True)
self.check_bool_function_result(_opcode.has_exc, no_exc, False)

def test_stack_effect(self):
self.assertEqual(stack_effect(dis.opmap['POP_TOP']), -1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The various opcode lists in the :mod:`dis` module are now generated from bytecodes.c instead of explicitly constructed in opcode.py.