From 9cfd23ed4a61e1d191bd50bab2bf0da105d9b61f Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 6 Feb 2023 19:45:32 +0000 Subject: [PATCH 1/3] COPY --- Python/bytecodes.c | 6 ++---- Python/generated_cases.c.h | 7 +++++-- Python/opcode_metadata.h | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 8993567ac82206..cb6b405ebbc050 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -3098,11 +3098,9 @@ dummy_func( PUSH(result); } - // stack effect: ( -- __0) - inst(COPY) { + inst(COPY, (values[oparg] -- values[oparg], copy)) { assert(oparg != 0); - PyObject *peek = PEEK(oparg); - PUSH(Py_NewRef(peek)); + copy = Py_NewRef(values[0]); } inst(BINARY_OP, (unused/1, lhs, rhs -- res)) { diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index e524bfcb99d470..7de8ef6848ed93 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -3723,9 +3723,12 @@ } TARGET(COPY) { + PyObject **values = &PEEK(oparg); + PyObject *copy; assert(oparg != 0); - PyObject *peek = PEEK(oparg); - PUSH(Py_NewRef(peek)); + copy = Py_NewRef(values[0]); + STACK_GROW(1); + POKE(1, copy); DISPATCH(); } diff --git a/Python/opcode_metadata.h b/Python/opcode_metadata.h index 857526c35aa5b6..f95a0bf36108e1 100644 --- a/Python/opcode_metadata.h +++ b/Python/opcode_metadata.h @@ -333,7 +333,7 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) { case FORMAT_VALUE: return -1; case COPY: - return -1; + return oparg; case BINARY_OP: return 2; case SWAP: @@ -679,7 +679,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) { case FORMAT_VALUE: return -1; case COPY: - return -1; + return oparg + 1; case BINARY_OP: return 1; case SWAP: From c9451dd007ad13cf3e62ba3d9177e35fc8acea02 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 6 Feb 2023 21:38:11 +0000 Subject: [PATCH 2/3] SWAP --- Python/bytecodes.c | 9 +++------ Python/generated_cases.c.h | 9 +++++---- Python/opcode_metadata.h | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index cb6b405ebbc050..895a320359e053 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -3124,12 +3124,9 @@ dummy_func( ERROR_IF(res == NULL, error); } - // stack effect: ( -- ) - inst(SWAP) { - assert(oparg != 0); - PyObject *top = TOP(); - SET_TOP(PEEK(oparg)); - PEEK(oparg) = top; + inst(SWAP, (bottom, unused[oparg-2], top -- + top, unused[oparg-2], bottom)) { + assert(oparg >= 2); } inst(EXTENDED_ARG, (--)) { diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 7de8ef6848ed93..d03957b7059b87 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -3763,10 +3763,11 @@ } TARGET(SWAP) { - assert(oparg != 0); - PyObject *top = TOP(); - SET_TOP(PEEK(oparg)); - PEEK(oparg) = top; + PyObject *top = PEEK(1); + PyObject *bottom = PEEK(2 + (oparg-2)); + assert(oparg >= 2); + POKE(1, bottom); + POKE(2 + (oparg-2), top); DISPATCH(); } diff --git a/Python/opcode_metadata.h b/Python/opcode_metadata.h index f95a0bf36108e1..3af3ca0f8bc4b1 100644 --- a/Python/opcode_metadata.h +++ b/Python/opcode_metadata.h @@ -337,7 +337,7 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) { case BINARY_OP: return 2; case SWAP: - return -1; + return (oparg-2) + 2; case EXTENDED_ARG: return 0; case CACHE: @@ -683,7 +683,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) { case BINARY_OP: return 1; case SWAP: - return -1; + return (oparg-2) + 2; case EXTENDED_ARG: return 0; case CACHE: From dd0b1442cd71ece5f959cfaf39b5dde785176209 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 6 Feb 2023 22:08:58 +0000 Subject: [PATCH 3/3] simplify COPY --- Python/bytecodes.c | 6 +++--- Python/generated_cases.c.h | 10 +++++----- Python/opcode_metadata.h | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 895a320359e053..0fc0b3b8280f8b 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -3098,9 +3098,9 @@ dummy_func( PUSH(result); } - inst(COPY, (values[oparg] -- values[oparg], copy)) { - assert(oparg != 0); - copy = Py_NewRef(values[0]); + inst(COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) { + assert(oparg > 0); + top = Py_NewRef(bottom); } inst(BINARY_OP, (unused/1, lhs, rhs -- res)) { diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index d03957b7059b87..f0f314a143c2c0 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -3723,12 +3723,12 @@ } TARGET(COPY) { - PyObject **values = &PEEK(oparg); - PyObject *copy; - assert(oparg != 0); - copy = Py_NewRef(values[0]); + PyObject *bottom = PEEK(1 + (oparg-1)); + PyObject *top; + assert(oparg > 0); + top = Py_NewRef(bottom); STACK_GROW(1); - POKE(1, copy); + POKE(1, top); DISPATCH(); } diff --git a/Python/opcode_metadata.h b/Python/opcode_metadata.h index 3af3ca0f8bc4b1..948d17519e2709 100644 --- a/Python/opcode_metadata.h +++ b/Python/opcode_metadata.h @@ -333,7 +333,7 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) { case FORMAT_VALUE: return -1; case COPY: - return oparg; + return (oparg-1) + 1; case BINARY_OP: return 2; case SWAP: @@ -679,7 +679,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) { case FORMAT_VALUE: return -1; case COPY: - return oparg + 1; + return (oparg-1) + 2; case BINARY_OP: return 1; case SWAP: