From e48824da30aa301e76b4e03abe8c8407bf70ff82 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 24 Dec 2022 20:16:02 -0800 Subject: [PATCH 01/32] load_const and load_fast aren't families for now --- Python/bytecodes.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c0b625bd662cc2..fffb85edf8d517 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -3631,8 +3631,6 @@ family(load_attr) = { LOAD_ATTR_PROPERTY, LOAD_ATTR_SLOT, LOAD_ATTR_WITH_HINT, LOAD_ATTR_METHOD_LAZY_DICT, LOAD_ATTR_METHOD_NO_DICT, LOAD_ATTR_METHOD_WITH_DICT, LOAD_ATTR_METHOD_WITH_VALUES }; -family(load_const) = { LOAD_CONST, LOAD_CONST__LOAD_FAST }; -family(load_fast) = { LOAD_FAST, LOAD_FAST__LOAD_CONST, LOAD_FAST__LOAD_FAST }; family(load_global) = { LOAD_GLOBAL, LOAD_GLOBAL_BUILTIN, LOAD_GLOBAL_MODULE }; From 852ad17529eb649004e8c03d947ac123e42580cf Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 24 Dec 2022 20:32:20 -0800 Subject: [PATCH 02/32] Modernize GET_ANEXT --- Python/bytecodes.c | 6 +----- Python/generated_cases.c.h | 7 ++++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index fffb85edf8d517..04f8223fd72b5d 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -638,12 +638,9 @@ dummy_func( } } - // stack effect: ( -- __0) - inst(GET_ANEXT) { + inst(GET_ANEXT, (aiter -- aiter, awaitable)) { unaryfunc getter = NULL; PyObject *next_iter = NULL; - PyObject *awaitable = NULL; - PyObject *aiter = TOP(); PyTypeObject *type = Py_TYPE(aiter); if (PyAsyncGen_CheckExact(aiter)) { @@ -685,7 +682,6 @@ dummy_func( } } - PUSH(awaitable); PREDICT(LOAD_CONST); } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 42b7ca08670540..6d4aa2f3fb73c3 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -795,10 +795,10 @@ } TARGET(GET_ANEXT) { + PyObject *aiter = PEEK(1); + PyObject *awaitable; unaryfunc getter = NULL; PyObject *next_iter = NULL; - PyObject *awaitable = NULL; - PyObject *aiter = TOP(); PyTypeObject *type = Py_TYPE(aiter); if (PyAsyncGen_CheckExact(aiter)) { @@ -840,7 +840,8 @@ } } - PUSH(awaitable); + STACK_GROW(1); + POKE(1, awaitable); PREDICT(LOAD_CONST); DISPATCH(); } From ec12b8c887108eae21352a3f77dbc1c67ffcdb9d Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 24 Dec 2022 21:15:45 -0800 Subject: [PATCH 03/32] Modernize GET_AWAITABLE --- Python/bytecodes.c | 14 ++++---------- Python/generated_cases.c.h | 12 +++++------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 04f8223fd72b5d..11da18d31a9c75 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -685,16 +685,14 @@ dummy_func( PREDICT(LOAD_CONST); } - // stack effect: ( -- ) - inst(GET_AWAITABLE) { - PyObject *iterable = TOP(); - PyObject *iter = _PyCoro_GetAwaitableIter(iterable); + inst(GET_AWAITABLE, (iterable -- iter)) { + iter = _PyCoro_GetAwaitableIter(iterable); if (iter == NULL) { format_awaitable_error(tstate, Py_TYPE(iterable), oparg); } - Py_DECREF(iterable); + DECREF_INPUTS(); if (iter != NULL && PyCoro_CheckExact(iter)) { PyObject *yf = _PyGen_yf((PyGenObject*)iter); @@ -710,11 +708,7 @@ dummy_func( } } - SET_TOP(iter); /* Even if it's NULL */ - - if (iter == NULL) { - goto error; - } + ERROR_IF(iter == NULL, error); PREDICT(LOAD_CONST); } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 6d4aa2f3fb73c3..a2dc09bf6d5cb5 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -848,8 +848,9 @@ TARGET(GET_AWAITABLE) { PREDICTED(GET_AWAITABLE); - PyObject *iterable = TOP(); - PyObject *iter = _PyCoro_GetAwaitableIter(iterable); + PyObject *iterable = PEEK(1); + PyObject *iter; + iter = _PyCoro_GetAwaitableIter(iterable); if (iter == NULL) { format_awaitable_error(tstate, Py_TYPE(iterable), oparg); @@ -871,12 +872,9 @@ } } - SET_TOP(iter); /* Even if it's NULL */ - - if (iter == NULL) { - goto error; - } + if (iter == NULL) goto pop_1_error; + POKE(1, iter); PREDICT(LOAD_CONST); DISPATCH(); } From f58b42198cb5574985d729c3c51c42b898cc1d78 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 24 Dec 2022 21:21:09 -0800 Subject: [PATCH 04/32] Modernize ASYNC_GEN_WRAP --- Python/bytecodes.c | 13 ++++--------- Python/generated_cases.c.h | 11 +++++------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 11da18d31a9c75..1aeed0c4b42dec 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -763,16 +763,11 @@ dummy_func( } } - // stack effect: ( -- ) - inst(ASYNC_GEN_WRAP) { - PyObject *v = TOP(); + inst(ASYNC_GEN_WRAP, (v -- w)) { assert(frame->f_code->co_flags & CO_ASYNC_GENERATOR); - PyObject *w = _PyAsyncGenValueWrapperNew(v); - if (w == NULL) { - goto error; - } - SET_TOP(w); - Py_DECREF(v); + w = _PyAsyncGenValueWrapperNew(v); + DECREF_INPUTS(); + ERROR_IF(w == NULL, error); } // stack effect: ( -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index a2dc09bf6d5cb5..13e482b07fe4d4 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -930,14 +930,13 @@ } TARGET(ASYNC_GEN_WRAP) { - PyObject *v = TOP(); + PyObject *v = PEEK(1); + PyObject *w; assert(frame->f_code->co_flags & CO_ASYNC_GENERATOR); - PyObject *w = _PyAsyncGenValueWrapperNew(v); - if (w == NULL) { - goto error; - } - SET_TOP(w); + w = _PyAsyncGenValueWrapperNew(v); Py_DECREF(v); + if (w == NULL) goto pop_1_error; + POKE(1, w); DISPATCH(); } From c4c821e13b07f7e5cf7353fafc260f6fa3c29acb Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 24 Dec 2022 21:38:36 -0800 Subject: [PATCH 05/32] Modernize YIELD_VALUE Because this is fiddling with stack frames, there is some weirdness in the `_PyFrame_SetStackPointer()` call. --- Python/bytecodes.c | 6 ++---- Python/generated_cases.c.h | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 1aeed0c4b42dec..9230dec6f18a70 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -770,17 +770,15 @@ dummy_func( ERROR_IF(w == NULL, error); } - // stack effect: ( -- ) - inst(YIELD_VALUE) { + inst(YIELD_VALUE, (retval --)) { // NOTE: It's important that YIELD_VALUE never raises an exception! // The compiler treats any exception raised here as a failed close() // or throw() call. assert(oparg == STACK_LEVEL()); assert(frame != &entry_frame); - PyObject *retval = POP(); PyGenObject *gen = _PyFrame_GetGenerator(frame); gen->gi_frame_state = FRAME_SUSPENDED; - _PyFrame_SetStackPointer(frame, stack_pointer); + _PyFrame_SetStackPointer(frame, stack_pointer - 1); TRACE_FUNCTION_EXIT(); DTRACE_FUNCTION_EXIT(); tstate->exc_info = gen->gi_exc_state.previous_item; diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 13e482b07fe4d4..c3d653479b9666 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -941,15 +941,15 @@ } TARGET(YIELD_VALUE) { + PyObject *retval = PEEK(1); // NOTE: It's important that YIELD_VALUE never raises an exception! // The compiler treats any exception raised here as a failed close() // or throw() call. assert(oparg == STACK_LEVEL()); assert(frame != &entry_frame); - PyObject *retval = POP(); PyGenObject *gen = _PyFrame_GetGenerator(frame); gen->gi_frame_state = FRAME_SUSPENDED; - _PyFrame_SetStackPointer(frame, stack_pointer); + _PyFrame_SetStackPointer(frame, stack_pointer - 1); TRACE_FUNCTION_EXIT(); DTRACE_FUNCTION_EXIT(); tstate->exc_info = gen->gi_exc_state.previous_item; From 55c009c25d8f28a223decb5e9a263324120111b1 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 24 Dec 2022 21:44:49 -0800 Subject: [PATCH 06/32] Modernize POP_EXCEPT (in more than one way) --- Python/bytecodes.c | 7 ++----- Python/generated_cases.c.h | 6 +++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 9230dec6f18a70..f7597fae7eee28 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -792,12 +792,9 @@ dummy_func( goto resume_frame; } - // stack effect: (__0 -- ) - inst(POP_EXCEPT) { + inst(POP_EXCEPT, (exc_value -- )) { _PyErr_StackItem *exc_info = tstate->exc_info; - PyObject *value = exc_info->exc_value; - exc_info->exc_value = POP(); - Py_XDECREF(value); + Py_XSETREF(exc_info->exc_value, exc_value); } // stack effect: (__0 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index c3d653479b9666..7b489eb4ab9617 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -964,10 +964,10 @@ } TARGET(POP_EXCEPT) { + PyObject *exc_value = PEEK(1); _PyErr_StackItem *exc_info = tstate->exc_info; - PyObject *value = exc_info->exc_value; - exc_info->exc_value = POP(); - Py_XDECREF(value); + Py_XSETREF(exc_info->exc_value, exc_value); + STACK_SHRINK(1); DISPATCH(); } From d678363ad35176da36334e193d187405fc8d2dcd Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 13:44:14 -0800 Subject: [PATCH 07/32] Modernize PREP_RERAISE_STAR --- Python/bytecodes.c | 16 ++++------------ Python/generated_cases.c.h | 17 ++++++++--------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index f7597fae7eee28..e72533011285c4 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -819,21 +819,13 @@ dummy_func( goto exception_unwind; } - // stack effect: (__0 -- ) - inst(PREP_RERAISE_STAR) { - PyObject *excs = POP(); + inst(PREP_RERAISE_STAR, (orig, excs -- val)) { assert(PyList_Check(excs)); - PyObject *orig = POP(); - - PyObject *val = _PyExc_PrepReraiseStar(orig, excs); - Py_DECREF(excs); - Py_DECREF(orig); - if (val == NULL) { - goto error; - } + val = _PyExc_PrepReraiseStar(orig, excs); + DECREF_INPUTS(); - PUSH(val); + ERROR_IF(val == NULL, error); } // stack effect: (__0, __1 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 7b489eb4ab9617..fcca562219ec60 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -993,19 +993,18 @@ } TARGET(PREP_RERAISE_STAR) { - PyObject *excs = POP(); + PyObject *excs = PEEK(1); + PyObject *orig = PEEK(2); + PyObject *val; assert(PyList_Check(excs)); - PyObject *orig = POP(); - PyObject *val = _PyExc_PrepReraiseStar(orig, excs); - Py_DECREF(excs); + val = _PyExc_PrepReraiseStar(orig, excs); Py_DECREF(orig); + Py_DECREF(excs); - if (val == NULL) { - goto error; - } - - PUSH(val); + if (val == NULL) goto pop_2_error; + STACK_SHRINK(1); + POKE(1, val); DISPATCH(); } From 301cc6ecafac235009fb76468732b21b8d3cf849 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 13:53:47 -0800 Subject: [PATCH 08/32] Modernize LOAD_ASSERTION_ERROR --- Python/bytecodes.c | 7 ++----- Python/generated_cases.c.h | 6 ++++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index e72533011285c4..dc7dbf5ce933e9 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -906,11 +906,8 @@ dummy_func( } } - - // stack effect: ( -- __0) - inst(LOAD_ASSERTION_ERROR) { - PyObject *value = PyExc_AssertionError; - PUSH(Py_NewRef(value)); + inst(LOAD_ASSERTION_ERROR, ( -- value)) { + value = Py_NewRef(PyExc_AssertionError); } // stack effect: ( -- __0) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index fcca562219ec60..5759aa32ec0941 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1088,8 +1088,10 @@ } TARGET(LOAD_ASSERTION_ERROR) { - PyObject *value = PyExc_AssertionError; - PUSH(Py_NewRef(value)); + PyObject *value; + value = Py_NewRef(PyExc_AssertionError); + STACK_GROW(1); + POKE(1, value); DISPATCH(); } From 84fcd6142cb68446985b5880a027823ebd3a4196 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 13:56:59 -0800 Subject: [PATCH 09/32] Modernize LOAD_BUILD_CLASS --- Python/bytecodes.c | 9 +++------ Python/generated_cases.c.h | 7 ++++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index dc7dbf5ce933e9..b2f9f1a0ce72c4 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -910,9 +910,7 @@ dummy_func( value = Py_NewRef(PyExc_AssertionError); } - // stack effect: ( -- __0) - inst(LOAD_BUILD_CLASS) { - PyObject *bc; + inst(LOAD_BUILD_CLASS, ( -- bc)) { if (PyDict_CheckExact(BUILTINS())) { bc = _PyDict_GetItemWithError(BUILTINS(), &_Py_ID(__build_class__)); @@ -921,7 +919,7 @@ dummy_func( _PyErr_SetString(tstate, PyExc_NameError, "__build_class__ not found"); } - goto error; + ERROR_IF(true, error); } Py_INCREF(bc); } @@ -931,10 +929,9 @@ dummy_func( if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) _PyErr_SetString(tstate, PyExc_NameError, "__build_class__ not found"); - goto error; + ERROR_IF(true, error); } } - PUSH(bc); } // stack effect: (__0 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 5759aa32ec0941..3d9bea16e158fc 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1105,7 +1105,7 @@ _PyErr_SetString(tstate, PyExc_NameError, "__build_class__ not found"); } - goto error; + if (true) goto error; } Py_INCREF(bc); } @@ -1115,10 +1115,11 @@ if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) _PyErr_SetString(tstate, PyExc_NameError, "__build_class__ not found"); - goto error; + if (true) goto error; } } - PUSH(bc); + STACK_GROW(1); + POKE(1, bc); DISPATCH(); } From 2fcb3d7adfb3236b927621062987534254e230a2 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 14:14:32 -0800 Subject: [PATCH 10/32] Modernize STORE_NAME --- Python/bytecodes.c | 13 +++++-------- Python/generated_cases.c.h | 8 ++++---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index b2f9f1a0ce72c4..0f57f6b2ce4a76 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -934,25 +934,22 @@ dummy_func( } } - // stack effect: (__0 -- ) - inst(STORE_NAME) { + inst(STORE_NAME, (v -- )) { PyObject *name = GETITEM(names, oparg); - PyObject *v = POP(); PyObject *ns = LOCALS(); int err; if (ns == NULL) { _PyErr_Format(tstate, PyExc_SystemError, "no locals found when storing %R", name); - Py_DECREF(v); - goto error; + DECREF_INPUTS(); + ERROR_IF(true, error); } if (PyDict_CheckExact(ns)) err = PyDict_SetItem(ns, name, v); else err = PyObject_SetItem(ns, name, v); - Py_DECREF(v); - if (err != 0) - goto error; + DECREF_INPUTS(); + ERROR_IF(err, error); } inst(DELETE_NAME, (--)) { diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3d9bea16e158fc..f7a0a658cd6527 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1124,23 +1124,23 @@ } TARGET(STORE_NAME) { + PyObject *v = PEEK(1); PyObject *name = GETITEM(names, oparg); - PyObject *v = POP(); PyObject *ns = LOCALS(); int err; if (ns == NULL) { _PyErr_Format(tstate, PyExc_SystemError, "no locals found when storing %R", name); Py_DECREF(v); - goto error; + if (true) goto pop_1_error; } if (PyDict_CheckExact(ns)) err = PyDict_SetItem(ns, name, v); else err = PyObject_SetItem(ns, name, v); Py_DECREF(v); - if (err != 0) - goto error; + if (err) goto pop_1_error; + STACK_SHRINK(1); DISPATCH(); } From c094ff4768de787fef4428441d4c1d26a1ad3480 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 14:24:00 -0800 Subject: [PATCH 11/32] Modernize LOAD_NAME --- Python/bytecodes.c | 5 +---- Python/generated_cases.c.h | 5 +++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 0f57f6b2ce4a76..c7be5ebb4dbb0d 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1102,11 +1102,9 @@ dummy_func( } } - // stack effect: ( -- __0) - inst(LOAD_NAME) { + inst(LOAD_NAME, ( -- v)) { PyObject *name = GETITEM(names, oparg); PyObject *locals = LOCALS(); - PyObject *v; if (locals == NULL) { _PyErr_Format(tstate, PyExc_SystemError, "no locals when loading %R", name); @@ -1163,7 +1161,6 @@ dummy_func( } } } - PUSH(v); } // error: LOAD_GLOBAL has irregular stack effect diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index f7a0a658cd6527..988eee904b386d 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1304,9 +1304,9 @@ } TARGET(LOAD_NAME) { + PyObject *v; PyObject *name = GETITEM(names, oparg); PyObject *locals = LOCALS(); - PyObject *v; if (locals == NULL) { _PyErr_Format(tstate, PyExc_SystemError, "no locals when loading %R", name); @@ -1363,7 +1363,8 @@ } } } - PUSH(v); + STACK_GROW(1); + POKE(1, v); DISPATCH(); } From c89dfd58440444cbddd4cf90dcdc44bf03d0d1bf Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 14:26:18 -0800 Subject: [PATCH 12/32] Modernize LOAD_CLASSDEREF --- Python/bytecodes.c | 6 ++---- Python/generated_cases.c.h | 6 ++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c7be5ebb4dbb0d..3d152c2bc4ec5e 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1299,9 +1299,8 @@ dummy_func( Py_DECREF(oldobj); } - // stack effect: ( -- __0) - inst(LOAD_CLASSDEREF) { - PyObject *name, *value, *locals = LOCALS(); + inst(LOAD_CLASSDEREF, ( -- value)) { + PyObject *name, *locals = LOCALS(); assert(locals); assert(oparg >= 0 && oparg < frame->f_code->co_nlocalsplus); name = PyTuple_GET_ITEM(frame->f_code->co_localsplusnames, oparg); @@ -1332,7 +1331,6 @@ dummy_func( } Py_INCREF(value); } - PUSH(value); } // stack effect: ( -- __0) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 988eee904b386d..1f2f8f397020ac 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1509,7 +1509,8 @@ } TARGET(LOAD_CLASSDEREF) { - PyObject *name, *value, *locals = LOCALS(); + PyObject *value; + PyObject *name, *locals = LOCALS(); assert(locals); assert(oparg >= 0 && oparg < frame->f_code->co_nlocalsplus); name = PyTuple_GET_ITEM(frame->f_code->co_localsplusnames, oparg); @@ -1540,7 +1541,8 @@ } Py_INCREF(value); } - PUSH(value); + STACK_GROW(1); + POKE(1, value); DISPATCH(); } From 415203ca65d780af362ff9efca1e430f6ec206d2 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 14:29:00 -0800 Subject: [PATCH 13/32] Modernize LOAD_DEREF --- Python/bytecodes.c | 9 ++++----- Python/generated_cases.c.h | 9 ++++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 3d152c2bc4ec5e..d3a1cb47545aa3 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1333,15 +1333,14 @@ dummy_func( } } - // stack effect: ( -- __0) - inst(LOAD_DEREF) { + inst(LOAD_DEREF, ( -- value)) { PyObject *cell = GETLOCAL(oparg); - PyObject *value = PyCell_GET(cell); + value = PyCell_GET(cell); if (value == NULL) { format_exc_unbound(tstate, frame->f_code, oparg); - goto error; + ERROR_IF(true, error); } - PUSH(Py_NewRef(value)); + Py_INCREF(value); } // stack effect: (__0 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 1f2f8f397020ac..3edbc4a346e8cc 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1547,13 +1547,16 @@ } TARGET(LOAD_DEREF) { + PyObject *value; PyObject *cell = GETLOCAL(oparg); - PyObject *value = PyCell_GET(cell); + value = PyCell_GET(cell); if (value == NULL) { format_exc_unbound(tstate, frame->f_code, oparg); - goto error; + if (true) goto error; } - PUSH(Py_NewRef(value)); + Py_INCREF(value); + STACK_GROW(1); + POKE(1, value); DISPATCH(); } From cc7bf1f4e5c8c2ccf3bea4390974ef85f031779f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 14:31:56 -0800 Subject: [PATCH 14/32] Modernize STORE_DEREF --- Python/bytecodes.c | 4 +--- Python/generated_cases.c.h | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index d3a1cb47545aa3..784f3e39e1bc86 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1343,9 +1343,7 @@ dummy_func( Py_INCREF(value); } - // stack effect: (__0 -- ) - inst(STORE_DEREF) { - PyObject *v = POP(); + inst(STORE_DEREF, (v --)) { PyObject *cell = GETLOCAL(oparg); PyObject *oldobj = PyCell_GET(cell); PyCell_SET(cell, v); diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3edbc4a346e8cc..d1223c52c80e2a 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1561,11 +1561,12 @@ } TARGET(STORE_DEREF) { - PyObject *v = POP(); + PyObject *v = PEEK(1); PyObject *cell = GETLOCAL(oparg); PyObject *oldobj = PyCell_GET(cell); PyCell_SET(cell, v); Py_XDECREF(oldobj); + STACK_SHRINK(1); DISPATCH(); } From 304997f74bb407bff45f0604019e8bec7ecf7c5f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 14:33:52 -0800 Subject: [PATCH 15/32] Modernize COPY_FREE_VARS (mark it as done) --- Python/bytecodes.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 784f3e39e1bc86..202cb1188d54d8 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1350,8 +1350,7 @@ dummy_func( Py_XDECREF(oldobj); } - // stack effect: ( -- ) - inst(COPY_FREE_VARS) { + inst(COPY_FREE_VARS, (--)) { /* Copy closure variables to free variables */ PyCodeObject *co = frame->f_code; assert(PyFunction_Check(frame->f_funcobj)); From b388032e72b205cbbe377b9649e59ce526e145c7 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 14:36:03 -0800 Subject: [PATCH 16/32] Modernize LIST_TO_TUPLE --- Python/bytecodes.c | 13 ++++--------- Python/generated_cases.c.h | 11 +++++------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 202cb1188d54d8..3451ba1e641aae 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1398,15 +1398,10 @@ dummy_func( PUSH(list); } - // stack effect: ( -- ) - inst(LIST_TO_TUPLE) { - PyObject *list = POP(); - PyObject *tuple = PyList_AsTuple(list); - Py_DECREF(list); - if (tuple == NULL) { - goto error; - } - PUSH(tuple); + inst(LIST_TO_TUPLE, (list -- tuple)) { + tuple = PyList_AsTuple(list); + DECREF_INPUTS(); + ERROR_IF(tuple == NULL, error); } // stack effect: (__0 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index d1223c52c80e2a..6dd101fa6f73af 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1620,13 +1620,12 @@ } TARGET(LIST_TO_TUPLE) { - PyObject *list = POP(); - PyObject *tuple = PyList_AsTuple(list); + PyObject *list = PEEK(1); + PyObject *tuple; + tuple = PyList_AsTuple(list); Py_DECREF(list); - if (tuple == NULL) { - goto error; - } - PUSH(tuple); + if (tuple == NULL) goto pop_1_error; + POKE(1, tuple); DISPATCH(); } From a84c0e35618ccbf3d426ebaf75fea0e5bdae7582 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 14:41:19 -0800 Subject: [PATCH 17/32] Modernize LIST_EXTEND --- Python/bytecodes.c | 12 +++++------- Python/generated_cases.c.h | 7 ++++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 3451ba1e641aae..fe0a33ec24d3d2 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1404,10 +1404,8 @@ dummy_func( ERROR_IF(tuple == NULL, error); } - // stack effect: (__0 -- ) - inst(LIST_EXTEND) { - PyObject *iterable = POP(); - PyObject *list = PEEK(oparg); + inst(LIST_EXTEND, (iterable -- )) { + PyObject *list = PEEK(oparg + 1); // iterable is still on the stack PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable); if (none_val == NULL) { if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && @@ -1418,11 +1416,11 @@ dummy_func( "Value after * must be an iterable, not %.200s", Py_TYPE(iterable)->tp_name); } - Py_DECREF(iterable); - goto error; + DECREF_INPUTS(); + ERROR_IF(true, error); } Py_DECREF(none_val); - Py_DECREF(iterable); + DECREF_INPUTS(); } // stack effect: (__0 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 6dd101fa6f73af..2edd099f3890be 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1630,8 +1630,8 @@ } TARGET(LIST_EXTEND) { - PyObject *iterable = POP(); - PyObject *list = PEEK(oparg); + PyObject *iterable = PEEK(1); + PyObject *list = PEEK(oparg + 1); // iterable is still on the stack PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable); if (none_val == NULL) { if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && @@ -1643,10 +1643,11 @@ Py_TYPE(iterable)->tp_name); } Py_DECREF(iterable); - goto error; + if (true) goto pop_1_error; } Py_DECREF(none_val); Py_DECREF(iterable); + STACK_SHRINK(1); DISPATCH(); } From 9099c400ca6cd1fdbffaf5bd8031ce2e37c06d04 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 14:57:17 -0800 Subject: [PATCH 18/32] Modernize SET_UPDATE --- Python/bytecodes.c | 12 ++++-------- Python/generated_cases.c.h | 9 ++++----- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index fe0a33ec24d3d2..73c64989de863b 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1423,15 +1423,11 @@ dummy_func( DECREF_INPUTS(); } - // stack effect: (__0 -- ) - inst(SET_UPDATE) { - PyObject *iterable = POP(); - PyObject *set = PEEK(oparg); + inst(SET_UPDATE, (iterable --)) { + PyObject *set = PEEK(oparg + 1); // iterable is still on the stack int err = _PySet_Update(set, iterable); - Py_DECREF(iterable); - if (err < 0) { - goto error; - } + DECREF_INPUTS(); + ERROR_IF(err < 0, error); } // stack effect: (__array[oparg] -- __0) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 2edd099f3890be..3ec076511a634a 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1652,13 +1652,12 @@ } TARGET(SET_UPDATE) { - PyObject *iterable = POP(); - PyObject *set = PEEK(oparg); + PyObject *iterable = PEEK(1); + PyObject *set = PEEK(oparg + 1); // iterable is still on the stack int err = _PySet_Update(set, iterable); Py_DECREF(iterable); - if (err < 0) { - goto error; - } + if (err < 0) goto pop_1_error; + STACK_SHRINK(1); DISPATCH(); } From fc534edbd74a4ce3af636ab76a2407cc3156d986 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 15:00:20 -0800 Subject: [PATCH 19/32] Modernize SETUP_ANNOTATIONS --- Python/bytecodes.c | 29 ++++++++--------------------- Python/generated_cases.c.h | 26 +++++++------------------- 2 files changed, 15 insertions(+), 40 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 73c64989de863b..e475e735f6ba7d 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1467,54 +1467,41 @@ dummy_func( PUSH(map); } - // stack effect: ( -- ) - inst(SETUP_ANNOTATIONS) { + inst(SETUP_ANNOTATIONS, (--)) { int err; PyObject *ann_dict; if (LOCALS() == NULL) { _PyErr_Format(tstate, PyExc_SystemError, "no locals found when setting up annotations"); - goto error; + ERROR_IF(true, error); } /* check if __annotations__ in locals()... */ if (PyDict_CheckExact(LOCALS())) { ann_dict = _PyDict_GetItemWithError(LOCALS(), &_Py_ID(__annotations__)); if (ann_dict == NULL) { - if (_PyErr_Occurred(tstate)) { - goto error; - } + ERROR_IF(_PyErr_Occurred(tstate), error); /* ...if not, create a new one */ ann_dict = PyDict_New(); - if (ann_dict == NULL) { - goto error; - } + ERROR_IF(ann_dict == NULL, error); err = PyDict_SetItem(LOCALS(), &_Py_ID(__annotations__), ann_dict); Py_DECREF(ann_dict); - if (err != 0) { - goto error; - } + ERROR_IF(err, error); } } else { /* do the same if locals() is not a dict */ ann_dict = PyObject_GetItem(LOCALS(), &_Py_ID(__annotations__)); if (ann_dict == NULL) { - if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - goto error; - } + ERROR_IF(!_PyErr_ExceptionMatches(tstate, PyExc_KeyError), error); _PyErr_Clear(tstate); ann_dict = PyDict_New(); - if (ann_dict == NULL) { - goto error; - } + ERROR_IF(ann_dict == NULL, error); err = PyObject_SetItem(LOCALS(), &_Py_ID(__annotations__), ann_dict); Py_DECREF(ann_dict); - if (err != 0) { - goto error; - } + ERROR_IF(err, error); } else { Py_DECREF(ann_dict); diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3ec076511a634a..8ebdae1ad89acf 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1704,47 +1704,35 @@ if (LOCALS() == NULL) { _PyErr_Format(tstate, PyExc_SystemError, "no locals found when setting up annotations"); - goto error; + if (true) goto error; } /* check if __annotations__ in locals()... */ if (PyDict_CheckExact(LOCALS())) { ann_dict = _PyDict_GetItemWithError(LOCALS(), &_Py_ID(__annotations__)); if (ann_dict == NULL) { - if (_PyErr_Occurred(tstate)) { - goto error; - } + if (_PyErr_Occurred(tstate)) goto error; /* ...if not, create a new one */ ann_dict = PyDict_New(); - if (ann_dict == NULL) { - goto error; - } + if (ann_dict == NULL) goto error; err = PyDict_SetItem(LOCALS(), &_Py_ID(__annotations__), ann_dict); Py_DECREF(ann_dict); - if (err != 0) { - goto error; - } + if (err) goto error; } } else { /* do the same if locals() is not a dict */ ann_dict = PyObject_GetItem(LOCALS(), &_Py_ID(__annotations__)); if (ann_dict == NULL) { - if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - goto error; - } + if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) goto error; _PyErr_Clear(tstate); ann_dict = PyDict_New(); - if (ann_dict == NULL) { - goto error; - } + if (ann_dict == NULL) goto error; err = PyObject_SetItem(LOCALS(), &_Py_ID(__annotations__), ann_dict); Py_DECREF(ann_dict); - if (err != 0) { - goto error; - } + if (err) goto error; } else { Py_DECREF(ann_dict); From 3a1e63ced2dd6da717ea9393081a2059a7dbb0a9 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 15:06:37 -0800 Subject: [PATCH 20/32] Modernize DICT_UPDATE --- Python/bytecodes.c | 12 +++++------- Python/generated_cases.c.h | 7 ++++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index e475e735f6ba7d..623cfa580a5441 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1533,20 +1533,18 @@ dummy_func( PUSH(map); } - // stack effect: (__0 -- ) - inst(DICT_UPDATE) { - PyObject *update = POP(); - PyObject *dict = PEEK(oparg); + inst(DICT_UPDATE, (update --)) { + PyObject *dict = PEEK(oparg + 1); // update is still on the stack if (PyDict_Update(dict, update) < 0) { if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { _PyErr_Format(tstate, PyExc_TypeError, "'%.200s' object is not a mapping", Py_TYPE(update)->tp_name); } - Py_DECREF(update); - goto error; + DECREF_INPUTS(); + ERROR_IF(true, error); } - Py_DECREF(update); + DECREF_INPUTS(); } // stack effect: (__0 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 8ebdae1ad89acf..45a486531a10a5 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1766,8 +1766,8 @@ } TARGET(DICT_UPDATE) { - PyObject *update = POP(); - PyObject *dict = PEEK(oparg); + PyObject *update = PEEK(1); + PyObject *dict = PEEK(oparg + 1); // update is still on the stack if (PyDict_Update(dict, update) < 0) { if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { _PyErr_Format(tstate, PyExc_TypeError, @@ -1775,9 +1775,10 @@ Py_TYPE(update)->tp_name); } Py_DECREF(update); - goto error; + if (true) goto pop_1_error; } Py_DECREF(update); + STACK_SHRINK(1); DISPATCH(); } From f9fc6404d5f8de46f637e69476e684d2b7b00a24 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 16:17:32 -0800 Subject: [PATCH 21/32] Modernize DICT_MERGE --- Python/bytecodes.c | 14 ++++++-------- Python/generated_cases.c.h | 9 +++++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 623cfa580a5441..61c717af1472df 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1547,17 +1547,15 @@ dummy_func( DECREF_INPUTS(); } - // stack effect: (__0 -- ) - inst(DICT_MERGE) { - PyObject *update = POP(); - PyObject *dict = PEEK(oparg); + inst(DICT_MERGE, (update --)) { + PyObject *dict = PEEK(oparg + 1); // update is still on the stack if (_PyDict_MergeEx(dict, update, 2) < 0) { - format_kwargs_error(tstate, PEEK(2 + oparg), update); - Py_DECREF(update); - goto error; + format_kwargs_error(tstate, PEEK(3 + oparg), update); + DECREF_INPUTS(); + ERROR_IF(true, error); } - Py_DECREF(update); + DECREF_INPUTS(); PREDICT(CALL_FUNCTION_EX); } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 45a486531a10a5..9492c73947a987 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1783,15 +1783,16 @@ } TARGET(DICT_MERGE) { - PyObject *update = POP(); - PyObject *dict = PEEK(oparg); + PyObject *update = PEEK(1); + PyObject *dict = PEEK(oparg + 1); // update is still on the stack if (_PyDict_MergeEx(dict, update, 2) < 0) { - format_kwargs_error(tstate, PEEK(2 + oparg), update); + format_kwargs_error(tstate, PEEK(3 + oparg), update); Py_DECREF(update); - goto error; + if (true) goto pop_1_error; } Py_DECREF(update); + STACK_SHRINK(1); PREDICT(CALL_FUNCTION_EX); DISPATCH(); } From ad38a3cd01ef335a7077e41b43e0520eb45aecee Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 16:23:13 -0800 Subject: [PATCH 22/32] Modernize MAP_ADD --- Python/bytecodes.c | 18 ++++++------------ Python/generated_cases.c.h | 16 +++++++--------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 61c717af1472df..bfcbd326bc0627 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1559,18 +1559,12 @@ dummy_func( PREDICT(CALL_FUNCTION_EX); } - // stack effect: (__0, __1 -- ) - inst(MAP_ADD) { - PyObject *value = TOP(); - PyObject *key = SECOND(); - PyObject *map; - STACK_SHRINK(2); - map = PEEK(oparg); /* dict */ - assert(PyDict_CheckExact(map)); - /* map[key] = value */ - if (_PyDict_SetItem_Take2((PyDictObject *)map, key, value) != 0) { - goto error; - } + inst(MAP_ADD, (key, value --)) { + PyObject *dict = PEEK(oparg + 2); // key, value are still on the stack + assert(PyDict_CheckExact(dict)); + /* dict[key] = value */ + // Do not DECREF INPUTS because the function steals the references + ERROR_IF(_PyDict_SetItem_Take2((PyDictObject *)dict, key, value) != 0, error); PREDICT(JUMP_BACKWARD); } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 9492c73947a987..a5bde7f76fbd8b 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1798,16 +1798,14 @@ } TARGET(MAP_ADD) { - PyObject *value = TOP(); - PyObject *key = SECOND(); - PyObject *map; + PyObject *value = PEEK(1); + PyObject *key = PEEK(2); + PyObject *dict = PEEK(oparg + 2); // key, value are still on the stack + assert(PyDict_CheckExact(dict)); + /* dict[key] = value */ + // Do not DECREF INPUTS because the function steals the references + if (_PyDict_SetItem_Take2((PyDictObject *)dict, key, value) != 0) goto pop_2_error; STACK_SHRINK(2); - map = PEEK(oparg); /* dict */ - assert(PyDict_CheckExact(map)); - /* map[key] = value */ - if (_PyDict_SetItem_Take2((PyDictObject *)map, key, value) != 0) { - goto error; - } PREDICT(JUMP_BACKWARD); DISPATCH(); } From b4fe3231f0bae166f84bbd19bba64ea872340113 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 16:28:28 -0800 Subject: [PATCH 23/32] Modernize IS_OP --- Python/bytecodes.c | 11 +++-------- Python/generated_cases.c.h | 10 ++++++---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index bfcbd326bc0627..7eeb0116fff1dd 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1993,15 +1993,10 @@ dummy_func( } super(COMPARE_OP_STR_JUMP) = _COMPARE_OP_STR + _JUMP_IF; - // stack effect: (__0 -- ) - inst(IS_OP) { - PyObject *right = POP(); - PyObject *left = TOP(); + inst(IS_OP, (left, right -- b)) { int res = Py_Is(left, right) ^ oparg; - PyObject *b = res ? Py_True : Py_False; - SET_TOP(Py_NewRef(b)); - Py_DECREF(left); - Py_DECREF(right); + DECREF_INPUTS(); + b = Py_NewRef(res ? Py_True : Py_False); } // stack effect: (__0 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index a5bde7f76fbd8b..80eda99d4098a3 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2306,13 +2306,15 @@ } TARGET(IS_OP) { - PyObject *right = POP(); - PyObject *left = TOP(); + PyObject *right = PEEK(1); + PyObject *left = PEEK(2); + PyObject *b; int res = Py_Is(left, right) ^ oparg; - PyObject *b = res ? Py_True : Py_False; - SET_TOP(Py_NewRef(b)); Py_DECREF(left); Py_DECREF(right); + b = Py_NewRef(res ? Py_True : Py_False); + STACK_SHRINK(1); + POKE(1, b); DISPATCH(); } From 9e6980647c99c2df79fde6fea2bb422a66a65f3d Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 16:33:17 -0800 Subject: [PATCH 24/32] Modernize CONTAINS_OP --- Python/bytecodes.c | 15 ++++----------- Python/generated_cases.c.h | 14 +++++++------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 7eeb0116fff1dd..51ae15be04ed8e 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1999,18 +1999,11 @@ dummy_func( b = Py_NewRef(res ? Py_True : Py_False); } - // stack effect: (__0 -- ) - inst(CONTAINS_OP) { - PyObject *right = POP(); - PyObject *left = POP(); + inst(CONTAINS_OP, (left, right -- b)) { int res = PySequence_Contains(right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) { - goto error; - } - PyObject *b = (res^oparg) ? Py_True : Py_False; - PUSH(Py_NewRef(b)); + DECREF_INPUTS(); + ERROR_IF(res < 0, error); + b = Py_NewRef((res^oparg) ? Py_True : Py_False); } // stack effect: ( -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 80eda99d4098a3..8b7d57e34b85b6 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2319,16 +2319,16 @@ } TARGET(CONTAINS_OP) { - PyObject *right = POP(); - PyObject *left = POP(); + PyObject *right = PEEK(1); + PyObject *left = PEEK(2); + PyObject *b; int res = PySequence_Contains(right, left); Py_DECREF(left); Py_DECREF(right); - if (res < 0) { - goto error; - } - PyObject *b = (res^oparg) ? Py_True : Py_False; - PUSH(Py_NewRef(b)); + if (res < 0) goto pop_2_error; + b = Py_NewRef((res^oparg) ? Py_True : Py_False); + STACK_SHRINK(1); + POKE(1, b); DISPATCH(); } From d1aa4d5fa4612b6ed315b445bc2b70aad6135877 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 19:05:24 -0800 Subject: [PATCH 25/32] Don't decref unmoved names --- Tools/cases_generator/generate_cases.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Tools/cases_generator/generate_cases.py b/Tools/cases_generator/generate_cases.py index 85a7c6098e0b95..5eed74c5e1472b 100644 --- a/Tools/cases_generator/generate_cases.py +++ b/Tools/cases_generator/generate_cases.py @@ -125,6 +125,7 @@ class Instruction: # Set later family: parser.Family | None = None predicted: bool = False + unmoved_names: frozenset[str] = frozenset() def __init__(self, inst: parser.InstDef): self.inst = inst @@ -141,6 +142,13 @@ def __init__(self, inst: parser.InstDef): effect for effect in inst.inputs if isinstance(effect, StackEffect) ] self.output_effects = inst.outputs # For consistency/completeness + unmoved_names: set[str] = set() + for ieffect, oeffect in zip(self.input_effects, self.output_effects): + if ieffect.name == oeffect.name: + unmoved_names.add(ieffect.name) + else: + break + self.unmoved_names = frozenset(unmoved_names) def write(self, out: Formatter) -> None: """Write one instruction, sans prologue and epilogue.""" @@ -175,12 +183,8 @@ def write(self, out: Formatter) -> None: out.stack_adjust(diff) # Write output stack effect assignments - unmoved_names: set[str] = set() - for ieffect, oeffect in zip(self.input_effects, self.output_effects): - if ieffect.name == oeffect.name: - unmoved_names.add(ieffect.name) for i, oeffect in enumerate(reversed(self.output_effects), 1): - if oeffect.name not in unmoved_names: + if oeffect.name not in self.unmoved_names: dst = StackEffect(f"PEEK({i})", "") out.assign(dst, oeffect) @@ -235,7 +239,8 @@ def write_body(self, out: Formatter, dedent: int, cache_adjust: int = 0) -> None elif m := re.match(r"(\s*)DECREF_INPUTS\(\);\s*$", line): space = m.group(1) for ieff in self.input_effects: - out.write_raw(f"{extra}{space}Py_DECREF({ieff.name});\n") + if ieff.name not in self.unmoved_names: + out.write_raw(f"{extra}{space}Py_DECREF({ieff.name});\n") else: out.write_raw(extra + line) @@ -533,7 +538,7 @@ def stack_analysis( ) -> tuple[list[StackEffect], int]: """Analyze a super-instruction or macro. - Print an error if there's a cache effect (which we don't support yet). + Ignore cache effects. Return the list of variable names and the initial stack pointer. """ From b06aafa4a3e348853388d2b949dae21522f5570f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 19:07:54 -0800 Subject: [PATCH 26/32] Modernize CHECK_EXC_MATCH --- Python/bytecodes.c | 13 +++++-------- Python/generated_cases.c.h | 10 ++++++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 51ae15be04ed8e..90bc7a6c5e38c0 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2047,19 +2047,16 @@ dummy_func( } } - // stack effect: ( -- ) - inst(CHECK_EXC_MATCH) { - PyObject *right = POP(); - PyObject *left = TOP(); + inst(CHECK_EXC_MATCH, (left, right -- left, b)) { assert(PyExceptionInstance_Check(left)); if (check_except_type_valid(tstate, right) < 0) { - Py_DECREF(right); - goto error; + DECREF_INPUTS(); + ERROR_IF(true, error); } int res = PyErr_GivenExceptionMatches(left, right); - Py_DECREF(right); - PUSH(Py_NewRef(res ? Py_True : Py_False)); + DECREF_INPUTS(); + b = Py_NewRef(res ? Py_True : Py_False); } // stack effect: (__0 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 8b7d57e34b85b6..08fcb792ee8f9c 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2374,17 +2374,19 @@ } TARGET(CHECK_EXC_MATCH) { - PyObject *right = POP(); - PyObject *left = TOP(); + PyObject *right = PEEK(1); + PyObject *left = PEEK(2); + PyObject *b; assert(PyExceptionInstance_Check(left)); if (check_except_type_valid(tstate, right) < 0) { Py_DECREF(right); - goto error; + if (true) goto pop_1_error; } int res = PyErr_GivenExceptionMatches(left, right); Py_DECREF(right); - PUSH(Py_NewRef(res ? Py_True : Py_False)); + b = Py_NewRef(res ? Py_True : Py_False); + POKE(1, b); DISPATCH(); } From 154ae4623b3fcc08f29e0e890771170afa49c201 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 19:58:42 -0800 Subject: [PATCH 27/32] Modernize IMPORT_NAME --- Python/bytecodes.c | 13 +++---------- Python/generated_cases.c.h | 12 ++++++------ 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 90bc7a6c5e38c0..996e45e0b8c4eb 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2059,18 +2059,11 @@ dummy_func( b = Py_NewRef(res ? Py_True : Py_False); } - // stack effect: (__0 -- ) - inst(IMPORT_NAME) { + inst(IMPORT_NAME, (level, fromlist -- res)) { PyObject *name = GETITEM(names, oparg); - PyObject *fromlist = POP(); - PyObject *level = TOP(); - PyObject *res; res = import_name(tstate, frame, name, fromlist, level); - Py_DECREF(level); - Py_DECREF(fromlist); - SET_TOP(res); - if (res == NULL) - goto error; + DECREF_INPUTS(); + ERROR_IF(res == NULL, error); } // stack effect: (__0 -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 08fcb792ee8f9c..eaebf7a1b55c9c 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2391,16 +2391,16 @@ } TARGET(IMPORT_NAME) { - PyObject *name = GETITEM(names, oparg); - PyObject *fromlist = POP(); - PyObject *level = TOP(); + PyObject *fromlist = PEEK(1); + PyObject *level = PEEK(2); PyObject *res; + PyObject *name = GETITEM(names, oparg); res = import_name(tstate, frame, name, fromlist, level); Py_DECREF(level); Py_DECREF(fromlist); - SET_TOP(res); - if (res == NULL) - goto error; + if (res == NULL) goto pop_2_error; + STACK_SHRINK(1); + POKE(1, res); DISPATCH(); } From d1ea9573836ccdf00a5969c647eac4e0da5f598f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 20:03:25 -0800 Subject: [PATCH 28/32] Modernize IMPORT_STAR --- Python/bytecodes.c | 18 ++++++++---------- Python/generated_cases.c.h | 11 ++++++----- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 996e45e0b8c4eb..9c8497a4a800fc 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2066,27 +2066,25 @@ dummy_func( ERROR_IF(res == NULL, error); } - // stack effect: (__0 -- ) - inst(IMPORT_STAR) { - PyObject *from = POP(), *locals; + inst(IMPORT_STAR, (from --)) { + PyObject *locals; int err; if (_PyFrame_FastToLocalsWithError(frame) < 0) { - Py_DECREF(from); - goto error; + DECREF_INPUTS(); + ERROR_IF(true, error); } locals = LOCALS(); if (locals == NULL) { _PyErr_SetString(tstate, PyExc_SystemError, "no locals found during 'import *'"); - Py_DECREF(from); - goto error; + DECREF_INPUTS(); + ERROR_IF(true, error); } err = import_all_from(tstate, locals, from); _PyFrame_LocalsToFast(frame, 0); - Py_DECREF(from); - if (err != 0) - goto error; + DECREF_INPUTS(); + ERROR_IF(err, error); } // stack effect: ( -- __0) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index eaebf7a1b55c9c..30f14edf651cab 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2405,11 +2405,12 @@ } TARGET(IMPORT_STAR) { - PyObject *from = POP(), *locals; + PyObject *from = PEEK(1); + PyObject *locals; int err; if (_PyFrame_FastToLocalsWithError(frame) < 0) { Py_DECREF(from); - goto error; + if (true) goto pop_1_error; } locals = LOCALS(); @@ -2417,13 +2418,13 @@ _PyErr_SetString(tstate, PyExc_SystemError, "no locals found during 'import *'"); Py_DECREF(from); - goto error; + if (true) goto pop_1_error; } err = import_all_from(tstate, locals, from); _PyFrame_LocalsToFast(frame, 0); Py_DECREF(from); - if (err != 0) - goto error; + if (err) goto pop_1_error; + STACK_SHRINK(1); DISPATCH(); } From 9a1626ece3dabc0302588a1de0719a32cc022427 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 20:06:26 -0800 Subject: [PATCH 29/32] Modernize IMPORT_FROM --- Python/bytecodes.c | 9 ++------- Python/generated_cases.c.h | 10 +++++----- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 9c8497a4a800fc..e71f88f57b59ce 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2087,15 +2087,10 @@ dummy_func( ERROR_IF(err, error); } - // stack effect: ( -- __0) - inst(IMPORT_FROM) { + inst(IMPORT_FROM, (from -- from, res)) { PyObject *name = GETITEM(names, oparg); - PyObject *from = TOP(); - PyObject *res; res = import_from(tstate, from, name); - PUSH(res); - if (res == NULL) - goto error; + ERROR_IF(res == NULL, error); } // stack effect: ( -- ) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 30f14edf651cab..1179bdfc696c62 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2429,13 +2429,13 @@ } TARGET(IMPORT_FROM) { - PyObject *name = GETITEM(names, oparg); - PyObject *from = TOP(); + PyObject *from = PEEK(1); PyObject *res; + PyObject *name = GETITEM(names, oparg); res = import_from(tstate, from, name); - PUSH(res); - if (res == NULL) - goto error; + if (res == NULL) goto error; + STACK_GROW(1); + POKE(1, res); DISPATCH(); } From d5c79e7454b529b87e1c8bff224cf3824039b70f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 20:08:15 -0800 Subject: [PATCH 30/32] Modernize JUMP_FORWARD (mark it as done) --- Python/bytecodes.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index e71f88f57b59ce..13339c50acd8ef 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2093,8 +2093,7 @@ dummy_func( ERROR_IF(res == NULL, error); } - // stack effect: ( -- ) - inst(JUMP_FORWARD) { + inst(JUMP_FORWARD, (--)) { JUMPBY(oparg); } From 7a40a8c6b49e3a7e64e3580441d924bf4c653186 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 20:09:14 -0800 Subject: [PATCH 31/32] Modernize JUMP_BACKWARD (mark it as done) --- Python/bytecodes.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 13339c50acd8ef..65f94e4a3477e2 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2097,8 +2097,7 @@ dummy_func( JUMPBY(oparg); } - // stack effect: ( -- ) - inst(JUMP_BACKWARD) { + inst(JUMP_BACKWARD, (--)) { assert(oparg < INSTR_OFFSET()); JUMPBY(-oparg); CHECK_EVAL_BREAKER(); From 841f08609d5df5105ba17324460141c4eeb8b554 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 26 Dec 2022 20:24:44 -0800 Subject: [PATCH 32/32] Add/update dummies to quiet VS Code --- Python/bytecodes.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 65f94e4a3477e2..e1c73ab6b32fbf 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -83,9 +83,11 @@ static PyObject *value, *value1, *value2, *left, *right, *res, *sum, *prod, *sub static PyObject *container, *start, *stop, *v, *lhs, *rhs; static PyObject *list, *tuple, *dict, *owner; static PyObject *exit_func, *lasti, *val, *retval, *obj, *iter; +static PyObject *aiter, *awaitable, *iterable, *w, *exc_value, *bc; +static PyObject *orig, *excs, *update, *b, *fromlist, *level, *from; static size_t jump; // Dummy variables for cache effects -static _Py_CODEUNIT when_to_jump_mask, invert, counter, index, hint; +static uint16_t when_to_jump_mask, invert, counter, index, hint; static uint32_t type_version; // Dummy opcode names for 'op' opcodes #define _COMPARE_OP_FLOAT 1003