aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/stackrefs.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2025-04-29 18:00:35 +0100
committerGitHub <noreply@github.com>2025-04-29 18:00:35 +0100
commitccf1b0b1c18e6d00fb919bce107f2793bab0a471 (patch)
tree94c1bd2fd030defe28291ae62acbfcb1c2cb4b2b /Python/stackrefs.c
parentcaee16f05229de5bc5ed2743c531f1696641888a (diff)
downloadcpython-ccf1b0b1c18e6d00fb919bce107f2793bab0a471.tar.gz
cpython-ccf1b0b1c18e6d00fb919bce107f2793bab0a471.zip
GH-132508: Use tagged integers on the evaluation stack for the last instruction offset (GH-132545)
Diffstat (limited to 'Python/stackrefs.c')
-rw-r--r--Python/stackrefs.c29
1 files changed, 24 insertions, 5 deletions
diff --git a/Python/stackrefs.c b/Python/stackrefs.c
index 1a2f66feb04..979a6b1c628 100644
--- a/Python/stackrefs.c
+++ b/Python/stackrefs.c
@@ -70,7 +70,7 @@ _Py_stackref_close(_PyStackRef ref, const char *filename, int linenumber)
}
PyObject *obj;
- if (ref.index <= LAST_PREDEFINED_STACKREF_INDEX) {
+ if (ref.index < INITIAL_STACKREF_INDEX) {
if (ref.index == 0) {
_Py_FatalErrorFormat(__func__, "Passing NULL to PyStackRef_CLOSE at %s:%d\n", filename, linenumber);
}
@@ -113,7 +113,8 @@ _Py_stackref_create(PyObject *obj, const char *filename, int linenumber)
Py_FatalError("Cannot create a stackref for NULL");
}
PyInterpreterState *interp = PyInterpreterState_Get();
- uint64_t new_id = interp->next_stackref++;
+ uint64_t new_id = interp->next_stackref;
+ interp->next_stackref = new_id + 2;
TableEntry *entry = make_table_entry(obj, filename, linenumber);
if (entry == NULL) {
Py_FatalError("No memory left for stackref debug table");
@@ -127,7 +128,7 @@ _Py_stackref_create(PyObject *obj, const char *filename, int linenumber)
void
_Py_stackref_record_borrow(_PyStackRef ref, const char *filename, int linenumber)
{
- if (ref.index <= LAST_PREDEFINED_STACKREF_INDEX) {
+ if (ref.index < INITIAL_STACKREF_INDEX) {
return;
}
PyInterpreterState *interp = PyInterpreterState_Get();
@@ -151,8 +152,7 @@ _Py_stackref_record_borrow(_PyStackRef ref, const char *filename, int linenumber
void
_Py_stackref_associate(PyInterpreterState *interp, PyObject *obj, _PyStackRef ref)
{
- assert(interp->next_stackref >= ref.index);
- interp->next_stackref = ref.index+1;
+ assert(ref.index < INITIAL_STACKREF_INDEX);
TableEntry *entry = make_table_entry(obj, "builtin-object", 0);
if (entry == NULL) {
Py_FatalError("No memory left for stackref debug table");
@@ -197,4 +197,23 @@ _PyStackRef_CLOSE_SPECIALIZED(_PyStackRef ref, destructor destruct, const char *
_Py_DECREF_SPECIALIZED(obj, destruct);
}
+_PyStackRef PyStackRef_TagInt(intptr_t i)
+{
+ return (_PyStackRef){ .index = (i << 1) + 1 };
+}
+
+intptr_t
+PyStackRef_UntagInt(_PyStackRef i)
+{
+ assert(PyStackRef_IsTaggedInt(i));
+ intptr_t val = (intptr_t)i.index;
+ return Py_ARITHMETIC_RIGHT_SHIFT(intptr_t, val, 1);
+}
+
+bool
+PyStackRef_IsNullOrInt(_PyStackRef ref)
+{
+ return PyStackRef_IsNull(ref) || PyStackRef_IsTaggedInt(ref);
+}
+
#endif