Skip to content

Commit

Permalink
pythonGH-100982: Break up COMPARE_AND_BRANCH (pythonGH-102801)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandtbucher authored and warsaw committed Apr 11, 2023
1 parent 0e964a8 commit 73558f2
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 447 deletions.
9 changes: 0 additions & 9 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1042,15 +1042,6 @@ iterations of the loop.
``cmp_op[opname]``.


.. opcode:: COMPARE_AND_BRANCH (opname)

Compares the top two values on the stack, popping them, then branches.
The direction and offset of the jump is embedded as a ``POP_JUMP_IF_TRUE``
or ``POP_JUMP_IF_FALSE`` instruction immediately following the cache.

.. versionadded:: 3.12


.. opcode:: IS_OP (invert)

Performs ``is`` comparison, or ``is not`` if ``invert`` is 1.
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ extern void _Py_Specialize_Call(PyObject *callable, _Py_CODEUNIT *instr,
int nargs, PyObject *kwnames);
extern void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
int oparg, PyObject **locals);
extern void _Py_Specialize_CompareAndBranch(PyObject *lhs, PyObject *rhs,
extern void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs,
_Py_CODEUNIT *instr, int oparg);
extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
int oparg);
Expand Down
23 changes: 11 additions & 12 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.12a6 3519 (Modify SEND instruction)
# Python 3.12a6 3520 (Remove PREP_RERAISE_STAR, add CALL_INTRINSIC_2)
# Python 3.12a7 3521 (Shrink the LOAD_GLOBAL caches)
# Python 3.12a7 3522 (Removed JUMP_IF_FALSE_OR_POP/JUMP_IF_TRUE_OR_POP)
# Python 3.12a7 3523 (Convert COMPARE_AND_BRANCH back to COMPARE_OP)

# Python 3.13 will start with 3550

Expand All @@ -452,7 +452,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3522).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3523).to_bytes(2, 'little') + b'\r\n'

_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

Expand Down
13 changes: 4 additions & 9 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ def pseudo_op(name, op, real_ops):
def_op('DELETE_DEREF', 139)
hasfree.append(139)
jrel_op('JUMP_BACKWARD', 140) # Number of words to skip (backwards)
def_op('COMPARE_AND_BRANCH', 141) # Comparison and jump
hascompare.append(141)

def_op('CALL_FUNCTION_EX', 142) # Flags

Expand Down Expand Up @@ -314,10 +312,10 @@ def pseudo_op(name, op, real_ops):
"CALL_NO_KW_TUPLE_1",
"CALL_NO_KW_TYPE_1",
],
"COMPARE_AND_BRANCH": [
"COMPARE_AND_BRANCH_FLOAT",
"COMPARE_AND_BRANCH_INT",
"COMPARE_AND_BRANCH_STR",
"COMPARE_OP": [
"COMPARE_OP_FLOAT",
"COMPARE_OP_INT",
"COMPARE_OP_STR",
],
"FOR_ITER": [
"FOR_ITER_LIST",
Expand Down Expand Up @@ -392,9 +390,6 @@ def pseudo_op(name, op, real_ops):
"COMPARE_OP": {
"counter": 1,
},
"COMPARE_AND_BRANCH": {
"counter": 1,
},
"BINARY_SUBSCR": {
"counter": 1,
"type_version": 2,
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ def test_multiline_boolean_expression(self):
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_FALSE',
line=2, end_line=2, column=15, end_column=16, occurrence=2)
# compare d and 0
self.assertOpcodeSourcePositionIs(compiled_code, 'COMPARE_AND_BRANCH',
self.assertOpcodeSourcePositionIs(compiled_code, 'COMPARE_OP',
line=4, end_line=4, column=8, end_column=13, occurrence=1)
# jump if comparison it True
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
Expand Down
16 changes: 8 additions & 8 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def cm(cls, x):
%3d LOAD_FAST 1 (x)
LOAD_CONST 1 (1)
COMPARE_OP 32 (==)
COMPARE_OP 40 (==)
LOAD_FAST 0 (self)
STORE_ATTR 0 (x)
RETURN_CONST 0 (None)
Expand All @@ -56,7 +56,7 @@ def cm(cls, x):
RESUME 0
LOAD_FAST 1
LOAD_CONST 1
COMPARE_OP 32 (==)
COMPARE_OP 40 (==)
LOAD_FAST 0
STORE_ATTR 0
RETURN_CONST 0
Expand All @@ -67,7 +67,7 @@ def cm(cls, x):
%3d LOAD_FAST 1 (x)
LOAD_CONST 1 (1)
COMPARE_OP 32 (==)
COMPARE_OP 40 (==)
LOAD_FAST 0 (cls)
STORE_ATTR 0 (x)
RETURN_CONST 0 (None)
Expand All @@ -78,7 +78,7 @@ def cm(cls, x):
%3d LOAD_FAST 0 (x)
LOAD_CONST 1 (1)
COMPARE_OP 32 (==)
COMPARE_OP 40 (==)
STORE_FAST 0 (x)
RETURN_CONST 0 (None)
""" % (_C.sm.__code__.co_firstlineno, _C.sm.__code__.co_firstlineno + 2,)
Expand Down Expand Up @@ -1554,12 +1554,12 @@ def _prepare_test_cases():
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=5, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=58, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_AND_BRANCH', opcode=141, arg=13, argval='<', argrepr='<', offset=60, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_OP', opcode=107, arg=2, argval='<', argrepr='<', offset=60, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=1, argval=68, argrepr='to 68', offset=64, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=21, argval=26, argrepr='to 26', offset=66, starts_line=6, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=68, starts_line=7, is_jump_target=True, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=70, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_AND_BRANCH', opcode=141, arg=68, argval='>', argrepr='>', offset=72, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_OP', opcode=107, arg=68, argval='>', argrepr='>', offset=72, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=1, argval=80, argrepr='to 80', offset=76, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=27, argval=26, argrepr='to 26', offset=78, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=80, starts_line=8, is_jump_target=True, positions=None),
Expand All @@ -1581,12 +1581,12 @@ def _prepare_test_cases():
Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=146, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=148, starts_line=14, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=150, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_AND_BRANCH', opcode=141, arg=75, argval='>', argrepr='>', offset=152, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_OP', opcode=107, arg=68, argval='>', argrepr='>', offset=152, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=1, argval=160, argrepr='to 160', offset=156, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=25, argval=110, argrepr='to 110', offset=158, starts_line=15, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=160, starts_line=16, is_jump_target=True, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=162, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_AND_BRANCH', opcode=141, arg=13, argval='<', argrepr='<', offset=164, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_OP', opcode=107, arg=2, argval='<', argrepr='<', offset=164, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=1, argval=172, argrepr='to 172', offset=168, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_FORWARD', opcode=110, arg=15, argval=202, argrepr='to 202', offset=170, starts_line=17, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=172, starts_line=11, is_jump_target=True, positions=None),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace all occurrences of ``COMPARE_AND_BRANCH`` with :opcode:`COMPARE_OP`.
8 changes: 0 additions & 8 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,6 @@ mark_stacks(PyCodeObject *code_obj, int len)
assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
stacks[j] = next_stack;
break;
case COMPARE_AND_BRANCH:
next_stack = pop_value(pop_value(next_stack));
i++;
j = get_arg(code, i) + i + 1;
assert(j < len);
assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
stacks[j] = next_stack;
break;
case GET_ITER:
case GET_AITER:
next_stack = push_value(pop_value(next_stack), Iterator);
Expand Down
Loading

0 comments on commit 73558f2

Please sign in to comment.