Skip to content

Commit 3875276

Browse files
authored
gh-98831: rewrite COPY and SWAP in the instruction definition DSL (#101620)
1 parent 949c58f commit 3875276

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

Python/bytecodes.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,11 +3098,9 @@ dummy_func(
30983098
PUSH(result);
30993099
}
31003100

3101-
// stack effect: ( -- __0)
3102-
inst(COPY) {
3103-
assert(oparg != 0);
3104-
PyObject *peek = PEEK(oparg);
3105-
PUSH(Py_NewRef(peek));
3101+
inst(COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
3102+
assert(oparg > 0);
3103+
top = Py_NewRef(bottom);
31063104
}
31073105

31083106
inst(BINARY_OP, (unused/1, lhs, rhs -- res)) {
@@ -3126,12 +3124,9 @@ dummy_func(
31263124
ERROR_IF(res == NULL, error);
31273125
}
31283126

3129-
// stack effect: ( -- )
3130-
inst(SWAP) {
3131-
assert(oparg != 0);
3132-
PyObject *top = TOP();
3133-
SET_TOP(PEEK(oparg));
3134-
PEEK(oparg) = top;
3127+
inst(SWAP, (bottom, unused[oparg-2], top --
3128+
top, unused[oparg-2], bottom)) {
3129+
assert(oparg >= 2);
31353130
}
31363131

31373132
inst(EXTENDED_ARG, (--)) {

Python/generated_cases.c.h

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

Python/opcode_metadata.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,11 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
333333
case FORMAT_VALUE:
334334
return -1;
335335
case COPY:
336-
return -1;
336+
return (oparg-1) + 1;
337337
case BINARY_OP:
338338
return 2;
339339
case SWAP:
340-
return -1;
340+
return (oparg-2) + 2;
341341
case EXTENDED_ARG:
342342
return 0;
343343
case CACHE:
@@ -679,11 +679,11 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
679679
case FORMAT_VALUE:
680680
return -1;
681681
case COPY:
682-
return -1;
682+
return (oparg-1) + 2;
683683
case BINARY_OP:
684684
return 1;
685685
case SWAP:
686-
return -1;
686+
return (oparg-2) + 2;
687687
case EXTENDED_ARG:
688688
return 0;
689689
case CACHE:

0 commit comments

Comments
 (0)