]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-98831: rewrite COPY and SWAP in the instruction definition DSL (#101620)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Mon, 6 Feb 2023 22:45:18 +0000 (22:45 +0000)
committerGitHub <noreply@github.com>
Mon, 6 Feb 2023 22:45:18 +0000 (22:45 +0000)
Python/bytecodes.c
Python/generated_cases.c.h
Python/opcode_metadata.h

index 8993567ac82206b7ac5a159c64db38a397d56f4d..0fc0b3b8280f8ba78c8a6422d9d9109da08fffe1 100644 (file)
@@ -3098,11 +3098,9 @@ dummy_func(
             PUSH(result);
         }
 
-        // stack effect: ( -- __0)
-        inst(COPY) {
-            assert(oparg != 0);
-            PyObject *peek = PEEK(oparg);
-            PUSH(Py_NewRef(peek));
+        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)) {
@@ -3126,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, (--)) {
index e524bfcb99d4700336aa4d4114b442d1571b1fa3..f0f314a143c2c0ffb77ad4a96a55902a31b548b1 100644 (file)
         }
 
         TARGET(COPY) {
-            assert(oparg != 0);
-            PyObject *peek = PEEK(oparg);
-            PUSH(Py_NewRef(peek));
+            PyObject *bottom = PEEK(1 + (oparg-1));
+            PyObject *top;
+            assert(oparg > 0);
+            top = Py_NewRef(bottom);
+            STACK_GROW(1);
+            POKE(1, top);
             DISPATCH();
         }
 
         }
 
         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();
         }
 
index 857526c35aa5b65db8aa76e95966f3f3be91cc7e..948d17519e2709afcaea22c5960a783d0bb50391 100644 (file)
@@ -333,11 +333,11 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
         case FORMAT_VALUE:
             return -1;
         case COPY:
-            return -1;
+            return (oparg-1) + 1;
         case BINARY_OP:
             return 2;
         case SWAP:
-            return -1;
+            return (oparg-2) + 2;
         case EXTENDED_ARG:
             return 0;
         case CACHE:
@@ -679,11 +679,11 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
         case FORMAT_VALUE:
             return -1;
         case COPY:
-            return -1;
+            return (oparg-1) + 2;
         case BINARY_OP:
             return 1;
         case SWAP:
-            return -1;
+            return (oparg-2) + 2;
         case EXTENDED_ARG:
             return 0;
         case CACHE: