Skip to content

Commit dec1ab0

Browse files
authored
gh-98831: rewrite UNPACK_EX, UNPACK_SEQUENCE, UNPACK_SEQUENCE_TWO_TUPLE in the instruction definition DSL (#101641)
1 parent f87f6e2 commit dec1ab0

File tree

3 files changed

+40
-54
lines changed

3 files changed

+40
-54
lines changed

Python/bytecodes.c

+11-26
Original file line numberDiff line numberDiff line change
@@ -859,41 +859,31 @@ dummy_func(
859859
}
860860
}
861861

862-
// stack effect: (__0 -- __array[oparg])
863-
inst(UNPACK_SEQUENCE) {
862+
inst(UNPACK_SEQUENCE, (unused/1, seq -- unused[oparg])) {
864863
#if ENABLE_SPECIALIZATION
865864
_PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)next_instr;
866865
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
867866
assert(cframe.use_tracing == 0);
868-
PyObject *seq = TOP();
869867
next_instr--;
870868
_Py_Specialize_UnpackSequence(seq, next_instr, oparg);
871869
DISPATCH_SAME_OPARG();
872870
}
873871
STAT_INC(UNPACK_SEQUENCE, deferred);
874872
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
875873
#endif /* ENABLE_SPECIALIZATION */
876-
PyObject *seq = POP();
877-
PyObject **top = stack_pointer + oparg;
878-
if (!unpack_iterable(tstate, seq, oparg, -1, top)) {
879-
Py_DECREF(seq);
880-
goto error;
881-
}
882-
STACK_GROW(oparg);
874+
PyObject **top = stack_pointer + oparg - 1;
875+
int res = unpack_iterable(tstate, seq, oparg, -1, top);
883876
Py_DECREF(seq);
884-
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
877+
ERROR_IF(res == 0, error);
885878
}
886879

887-
// stack effect: (__0 -- __array[oparg])
888-
inst(UNPACK_SEQUENCE_TWO_TUPLE) {
889-
PyObject *seq = TOP();
880+
inst(UNPACK_SEQUENCE_TWO_TUPLE, (unused/1, seq -- v1, v0)) {
890881
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
891882
DEOPT_IF(PyTuple_GET_SIZE(seq) != 2, UNPACK_SEQUENCE);
892883
STAT_INC(UNPACK_SEQUENCE, hit);
893-
SET_TOP(Py_NewRef(PyTuple_GET_ITEM(seq, 1)));
894-
PUSH(Py_NewRef(PyTuple_GET_ITEM(seq, 0)));
884+
v1 = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
885+
v0 = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
895886
Py_DECREF(seq);
896-
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
897887
}
898888

899889
// stack effect: (__0 -- __array[oparg])
@@ -926,17 +916,12 @@ dummy_func(
926916
JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
927917
}
928918

929-
// error: UNPACK_EX has irregular stack effect
930-
inst(UNPACK_EX) {
919+
inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
931920
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
932-
PyObject *seq = POP();
933-
PyObject **top = stack_pointer + totalargs;
934-
if (!unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top)) {
935-
Py_DECREF(seq);
936-
goto error;
937-
}
938-
STACK_GROW(totalargs);
921+
PyObject **top = stack_pointer + totalargs - 1;
922+
int res = unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top);
939923
Py_DECREF(seq);
924+
ERROR_IF(res == 0, error);
940925
}
941926

942927
family(store_attr, INLINE_CACHE_ENTRIES_STORE_ATTR) = {

Python/generated_cases.c.h

+21-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/opcode_metadata.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
121121
case DELETE_NAME:
122122
return 0;
123123
case UNPACK_SEQUENCE:
124-
return -1;
124+
return 1;
125125
case UNPACK_SEQUENCE_TWO_TUPLE:
126-
return -1;
126+
return 1;
127127
case UNPACK_SEQUENCE_TUPLE:
128128
return -1;
129129
case UNPACK_SEQUENCE_LIST:
130130
return -1;
131131
case UNPACK_EX:
132-
return -1;
132+
return 1;
133133
case STORE_ATTR:
134134
return 2;
135135
case DELETE_ATTR:
@@ -467,15 +467,15 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
467467
case DELETE_NAME:
468468
return 0;
469469
case UNPACK_SEQUENCE:
470-
return -1;
470+
return oparg;
471471
case UNPACK_SEQUENCE_TWO_TUPLE:
472-
return -1;
472+
return 2;
473473
case UNPACK_SEQUENCE_TUPLE:
474474
return -1;
475475
case UNPACK_SEQUENCE_LIST:
476476
return -1;
477477
case UNPACK_EX:
478-
return -1;
478+
return (oparg & 0xFF) + (oparg >> 8) + 1;
479479
case STORE_ATTR:
480480
return 0;
481481
case DELETE_ATTR:
@@ -759,8 +759,8 @@ struct opcode_metadata {
759759
[LOAD_BUILD_CLASS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
760760
[STORE_NAME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
761761
[DELETE_NAME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
762-
[UNPACK_SEQUENCE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
763-
[UNPACK_SEQUENCE_TWO_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
762+
[UNPACK_SEQUENCE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
763+
[UNPACK_SEQUENCE_TWO_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IXC },
764764
[UNPACK_SEQUENCE_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
765765
[UNPACK_SEQUENCE_LIST] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
766766
[UNPACK_EX] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },

0 commit comments

Comments
 (0)