Skip to content

Commit 0444ae2

Browse files
authored
GH-100982: Break up COMPARE_AND_BRANCH (GH-102801)
1 parent bd06375 commit 0444ae2

18 files changed

+345
-447
lines changed

Doc/library/dis.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,15 +1042,6 @@ iterations of the loop.
10421042
``cmp_op[opname]``.
10431043

10441044

1045-
.. opcode:: COMPARE_AND_BRANCH (opname)
1046-
1047-
Compares the top two values on the stack, popping them, then branches.
1048-
The direction and offset of the jump is embedded as a ``POP_JUMP_IF_TRUE``
1049-
or ``POP_JUMP_IF_FALSE`` instruction immediately following the cache.
1050-
1051-
.. versionadded:: 3.12
1052-
1053-
10541045
.. opcode:: IS_OP (invert)
10551046

10561047
Performs ``is`` comparison, or ``is not`` if ``invert`` is 1.

Include/internal/pycore_code.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ extern void _Py_Specialize_Call(PyObject *callable, _Py_CODEUNIT *instr,
234234
int nargs, PyObject *kwnames);
235235
extern void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
236236
int oparg, PyObject **locals);
237-
extern void _Py_Specialize_CompareAndBranch(PyObject *lhs, PyObject *rhs,
237+
extern void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs,
238238
_Py_CODEUNIT *instr, int oparg);
239239
extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
240240
int oparg);

Include/internal/pycore_opcode.h

Lines changed: 11 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/opcode.h

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/importlib/_bootstrap_external.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def _write_atomic(path, data, mode=0o666):
435435
# Python 3.12a6 3519 (Modify SEND instruction)
436436
# Python 3.12a6 3520 (Remove PREP_RERAISE_STAR, add CALL_INTRINSIC_2)
437437
# Python 3.12a7 3521 (Shrink the LOAD_GLOBAL caches)
438-
# Python 3.12a7 3522 (Removed JUMP_IF_FALSE_OR_POP/JUMP_IF_TRUE_OR_POP)
438+
# Python 3.12a7 3523 (Convert COMPARE_AND_BRANCH back to COMPARE_OP)
439439

440440
# Python 3.13 will start with 3550
441441

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

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

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

Lib/opcode.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,6 @@ def pseudo_op(name, op, real_ops):
191191
def_op('DELETE_DEREF', 139)
192192
hasfree.append(139)
193193
jrel_op('JUMP_BACKWARD', 140) # Number of words to skip (backwards)
194-
def_op('COMPARE_AND_BRANCH', 141) # Comparison and jump
195-
hascompare.append(141)
196194

197195
def_op('CALL_FUNCTION_EX', 142) # Flags
198196

@@ -314,10 +312,10 @@ def pseudo_op(name, op, real_ops):
314312
"CALL_NO_KW_TUPLE_1",
315313
"CALL_NO_KW_TYPE_1",
316314
],
317-
"COMPARE_AND_BRANCH": [
318-
"COMPARE_AND_BRANCH_FLOAT",
319-
"COMPARE_AND_BRANCH_INT",
320-
"COMPARE_AND_BRANCH_STR",
315+
"COMPARE_OP": [
316+
"COMPARE_OP_FLOAT",
317+
"COMPARE_OP_INT",
318+
"COMPARE_OP_STR",
321319
],
322320
"FOR_ITER": [
323321
"FOR_ITER_LIST",
@@ -392,9 +390,6 @@ def pseudo_op(name, op, real_ops):
392390
"COMPARE_OP": {
393391
"counter": 1,
394392
},
395-
"COMPARE_AND_BRANCH": {
396-
"counter": 1,
397-
},
398393
"BINARY_SUBSCR": {
399394
"counter": 1,
400395
"type_version": 2,

Lib/test/test_compile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ def test_multiline_boolean_expression(self):
12831283
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_FALSE',
12841284
line=2, end_line=2, column=15, end_column=16, occurrence=2)
12851285
# compare d and 0
1286-
self.assertOpcodeSourcePositionIs(compiled_code, 'COMPARE_AND_BRANCH',
1286+
self.assertOpcodeSourcePositionIs(compiled_code, 'COMPARE_OP',
12871287
line=4, end_line=4, column=8, end_column=13, occurrence=1)
12881288
# jump if comparison it True
12891289
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',

Lib/test/test_dis.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def cm(cls, x):
4646
4747
%3d LOAD_FAST 1 (x)
4848
LOAD_CONST 1 (1)
49-
COMPARE_OP 32 (==)
49+
COMPARE_OP 40 (==)
5050
LOAD_FAST 0 (self)
5151
STORE_ATTR 0 (x)
5252
RETURN_CONST 0 (None)
@@ -56,7 +56,7 @@ def cm(cls, x):
5656
RESUME 0
5757
LOAD_FAST 1
5858
LOAD_CONST 1
59-
COMPARE_OP 32 (==)
59+
COMPARE_OP 40 (==)
6060
LOAD_FAST 0
6161
STORE_ATTR 0
6262
RETURN_CONST 0
@@ -67,7 +67,7 @@ def cm(cls, x):
6767
6868
%3d LOAD_FAST 1 (x)
6969
LOAD_CONST 1 (1)
70-
COMPARE_OP 32 (==)
70+
COMPARE_OP 40 (==)
7171
LOAD_FAST 0 (cls)
7272
STORE_ATTR 0 (x)
7373
RETURN_CONST 0 (None)
@@ -78,7 +78,7 @@ def cm(cls, x):
7878
7979
%3d LOAD_FAST 0 (x)
8080
LOAD_CONST 1 (1)
81-
COMPARE_OP 32 (==)
81+
COMPARE_OP 40 (==)
8282
STORE_FAST 0 (x)
8383
RETURN_CONST 0 (None)
8484
""" % (_C.sm.__code__.co_firstlineno, _C.sm.__code__.co_firstlineno + 2,)
@@ -1554,12 +1554,12 @@ def _prepare_test_cases():
15541554
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False, positions=None),
15551555
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=5, is_jump_target=False, positions=None),
15561556
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=58, starts_line=None, is_jump_target=False, positions=None),
1557-
Instruction(opname='COMPARE_AND_BRANCH', opcode=141, arg=13, argval='<', argrepr='<', offset=60, starts_line=None, is_jump_target=False, positions=None),
1557+
Instruction(opname='COMPARE_OP', opcode=107, arg=2, argval='<', argrepr='<', offset=60, starts_line=None, is_jump_target=False, positions=None),
15581558
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),
15591559
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=21, argval=26, argrepr='to 26', offset=66, starts_line=6, is_jump_target=False, positions=None),
15601560
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=68, starts_line=7, is_jump_target=True, positions=None),
15611561
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=70, starts_line=None, is_jump_target=False, positions=None),
1562-
Instruction(opname='COMPARE_AND_BRANCH', opcode=141, arg=68, argval='>', argrepr='>', offset=72, starts_line=None, is_jump_target=False, positions=None),
1562+
Instruction(opname='COMPARE_OP', opcode=107, arg=68, argval='>', argrepr='>', offset=72, starts_line=None, is_jump_target=False, positions=None),
15631563
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),
15641564
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=27, argval=26, argrepr='to 26', offset=78, starts_line=None, is_jump_target=False, positions=None),
15651565
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=80, starts_line=8, is_jump_target=True, positions=None),
@@ -1581,12 +1581,12 @@ def _prepare_test_cases():
15811581
Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=146, starts_line=None, is_jump_target=False, positions=None),
15821582
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=148, starts_line=14, is_jump_target=False, positions=None),
15831583
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=150, starts_line=None, is_jump_target=False, positions=None),
1584-
Instruction(opname='COMPARE_AND_BRANCH', opcode=141, arg=75, argval='>', argrepr='>', offset=152, starts_line=None, is_jump_target=False, positions=None),
1584+
Instruction(opname='COMPARE_OP', opcode=107, arg=68, argval='>', argrepr='>', offset=152, starts_line=None, is_jump_target=False, positions=None),
15851585
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),
15861586
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=25, argval=110, argrepr='to 110', offset=158, starts_line=15, is_jump_target=False, positions=None),
15871587
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=160, starts_line=16, is_jump_target=True, positions=None),
15881588
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=162, starts_line=None, is_jump_target=False, positions=None),
1589-
Instruction(opname='COMPARE_AND_BRANCH', opcode=141, arg=13, argval='<', argrepr='<', offset=164, starts_line=None, is_jump_target=False, positions=None),
1589+
Instruction(opname='COMPARE_OP', opcode=107, arg=2, argval='<', argrepr='<', offset=164, starts_line=None, is_jump_target=False, positions=None),
15901590
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),
15911591
Instruction(opname='JUMP_FORWARD', opcode=110, arg=15, argval=202, argrepr='to 202', offset=170, starts_line=17, is_jump_target=False, positions=None),
15921592
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=172, starts_line=11, is_jump_target=True, positions=None),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace all occurrences of ``COMPARE_AND_BRANCH`` with :opcode:`COMPARE_OP`.

Objects/frameobject.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,6 @@ mark_stacks(PyCodeObject *code_obj, int len)
347347
assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
348348
stacks[j] = next_stack;
349349
break;
350-
case COMPARE_AND_BRANCH:
351-
next_stack = pop_value(pop_value(next_stack));
352-
i++;
353-
j = get_arg(code, i) + i + 1;
354-
assert(j < len);
355-
assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
356-
stacks[j] = next_stack;
357-
break;
358350
case GET_ITER:
359351
case GET_AITER:
360352
next_stack = push_value(pop_value(next_stack), Iterator);

0 commit comments

Comments
 (0)