aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/bytecodes.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2022-11-17 17:06:07 -0800
committerGitHub <noreply@github.com>2022-11-17 17:06:07 -0800
commit4f5e1cb00a914692895c1c16e446c8d2ab3efb7e (patch)
tree9c32754374b1831bd1e3022a82e0177994b98f13 /Python/bytecodes.c
parentb629fdd88ac1c20439b49cbc9aa33b27cd5f6daf (diff)
downloadcpython-4f5e1cb00a914692895c1c16e446c8d2ab3efb7e.tar.gz
cpython-4f5e1cb00a914692895c1c16e446c8d2ab3efb7e.zip
GH-98831: Refactor and fix cases generator (#99526)
Also complete cache effects for BINARY_SUBSCR family.
Diffstat (limited to 'Python/bytecodes.c')
-rw-r--r--Python/bytecodes.c63
1 files changed, 25 insertions, 38 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index a3e02674c29..78f7d4ac061 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -71,7 +71,7 @@ do { \
#define inst(name, ...) case name:
#define super(name) static int SUPER_##name
-#define family(name) static int family_##name
+#define family(name, ...) static int family_##name
#define NAME_ERROR_MSG \
"name '%.200s' is not defined"
@@ -79,6 +79,7 @@ do { \
// Dummy variables for stack effects.
static PyObject *value, *value1, *value2, *left, *right, *res, *sum, *prod, *sub;
static PyObject *container, *start, *stop, *v, *lhs, *rhs;
+static PyObject *list, *tuple, *dict;
static PyObject *
dummy_func(
@@ -322,7 +323,15 @@ dummy_func(
ERROR_IF(sum == NULL, error);
}
- inst(BINARY_SUBSCR, (container, sub -- res)) {
+ family(binary_subscr, INLINE_CACHE_ENTRIES_BINARY_SUBSCR) = {
+ BINARY_SUBSCR,
+ BINARY_SUBSCR_DICT,
+ BINARY_SUBSCR_GETITEM,
+ BINARY_SUBSCR_LIST_INT,
+ BINARY_SUBSCR_TUPLE_INT,
+ };
+
+ inst(BINARY_SUBSCR, (container, sub, unused/4 -- res)) {
_PyBinarySubscrCache *cache = (_PyBinarySubscrCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
assert(cframe.use_tracing == 0);
@@ -336,7 +345,6 @@ dummy_func(
Py_DECREF(container);
Py_DECREF(sub);
ERROR_IF(res == NULL, error);
- JUMPBY(INLINE_CACHE_ENTRIES_BINARY_SUBSCR);
}
inst(BINARY_SLICE, (container, start, stop -- res)) {
@@ -369,11 +377,8 @@ dummy_func(
ERROR_IF(err, error);
}
- // stack effect: (__0 -- )
- inst(BINARY_SUBSCR_LIST_INT) {
+ inst(BINARY_SUBSCR_LIST_INT, (list, sub, unused/4 -- res)) {
assert(cframe.use_tracing == 0);
- PyObject *sub = TOP();
- PyObject *list = SECOND();
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
DEOPT_IF(!PyList_CheckExact(list), BINARY_SUBSCR);
@@ -384,21 +389,15 @@ dummy_func(
Py_ssize_t index = ((PyLongObject*)sub)->ob_digit[0];
DEOPT_IF(index >= PyList_GET_SIZE(list), BINARY_SUBSCR);
STAT_INC(BINARY_SUBSCR, hit);
- PyObject *res = PyList_GET_ITEM(list, index);
+ res = PyList_GET_ITEM(list, index);
assert(res != NULL);
Py_INCREF(res);
- STACK_SHRINK(1);
_Py_DECREF_SPECIALIZED(sub, (destructor)PyObject_Free);
- SET_TOP(res);
Py_DECREF(list);
- JUMPBY(INLINE_CACHE_ENTRIES_BINARY_SUBSCR);
}
- // stack effect: (__0 -- )
- inst(BINARY_SUBSCR_TUPLE_INT) {
+ inst(BINARY_SUBSCR_TUPLE_INT, (tuple, sub, unused/4 -- res)) {
assert(cframe.use_tracing == 0);
- PyObject *sub = TOP();
- PyObject *tuple = SECOND();
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
DEOPT_IF(!PyTuple_CheckExact(tuple), BINARY_SUBSCR);
@@ -409,51 +408,39 @@ dummy_func(
Py_ssize_t index = ((PyLongObject*)sub)->ob_digit[0];
DEOPT_IF(index >= PyTuple_GET_SIZE(tuple), BINARY_SUBSCR);
STAT_INC(BINARY_SUBSCR, hit);
- PyObject *res = PyTuple_GET_ITEM(tuple, index);
+ res = PyTuple_GET_ITEM(tuple, index);
assert(res != NULL);
Py_INCREF(res);
- STACK_SHRINK(1);
_Py_DECREF_SPECIALIZED(sub, (destructor)PyObject_Free);
- SET_TOP(res);
Py_DECREF(tuple);
- JUMPBY(INLINE_CACHE_ENTRIES_BINARY_SUBSCR);
}
- // stack effect: (__0 -- )
- inst(BINARY_SUBSCR_DICT) {
+ inst(BINARY_SUBSCR_DICT, (dict, sub, unused/4 -- res)) {
assert(cframe.use_tracing == 0);
- PyObject *dict = SECOND();
- DEOPT_IF(!PyDict_CheckExact(SECOND()), BINARY_SUBSCR);
+ DEOPT_IF(!PyDict_CheckExact(dict), BINARY_SUBSCR);
STAT_INC(BINARY_SUBSCR, hit);
- PyObject *sub = TOP();
- PyObject *res = PyDict_GetItemWithError(dict, sub);
+ res = PyDict_GetItemWithError(dict, sub);
if (res == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetKeyError(sub);
}
- goto error;
+ Py_DECREF(dict);
+ Py_DECREF(sub);
+ ERROR_IF(1, error);
}
- Py_INCREF(res);
- STACK_SHRINK(1);
- Py_DECREF(sub);
- SET_TOP(res);
+ Py_INCREF(res); // Do this before DECREF'ing dict, sub
Py_DECREF(dict);
- JUMPBY(INLINE_CACHE_ENTRIES_BINARY_SUBSCR);
+ Py_DECREF(sub);
}
- // stack effect: (__0 -- )
- inst(BINARY_SUBSCR_GETITEM) {
- PyObject *sub = TOP();
- PyObject *container = SECOND();
- _PyBinarySubscrCache *cache = (_PyBinarySubscrCache *)next_instr;
- uint32_t type_version = read_u32(cache->type_version);
+ inst(BINARY_SUBSCR_GETITEM, (container, sub, unused/1, type_version/2, func_version/1 -- unused)) {
PyTypeObject *tp = Py_TYPE(container);
DEOPT_IF(tp->tp_version_tag != type_version, BINARY_SUBSCR);
assert(tp->tp_flags & Py_TPFLAGS_HEAPTYPE);
PyObject *cached = ((PyHeapTypeObject *)tp)->_spec_cache.getitem;
assert(PyFunction_Check(cached));
PyFunctionObject *getitem = (PyFunctionObject *)cached;
- DEOPT_IF(getitem->func_version != cache->func_version, BINARY_SUBSCR);
+ DEOPT_IF(getitem->func_version != func_version, BINARY_SUBSCR);
PyCodeObject *code = (PyCodeObject *)getitem->func_code;
assert(code->co_argcount == 2);
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), BINARY_SUBSCR);