Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion Include/internal/pycore_stackref.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,28 @@ static const _PyStackRef PyStackRef_ERROR = { .index = (1 << Py_TAGGED_SHIFT) };
#define PyStackRef_None ((_PyStackRef){ .index = (2 << Py_TAGGED_SHIFT) } )
#define _Py_STACKREF_FALSE_INDEX (3 << Py_TAGGED_SHIFT)
#define _Py_STACKREF_TRUE_INDEX (4 << Py_TAGGED_SHIFT)
/* Tier-2 transient bit values (gh-149238). */
#define _Py_STACKREF_BIT_0_INDEX (5 << Py_TAGGED_SHIFT)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than expose the internal representation, can you add new PyStackRef_WrapBit PyStackRef_UnwrapBit functions to present a nicer API.

How there are stored doesn't need to leak into the rest of the code.

#define _Py_STACKREF_BIT_1_INDEX (6 << Py_TAGGED_SHIFT)
#define PyStackRef_False ((_PyStackRef){ .index = _Py_STACKREF_FALSE_INDEX })
#define PyStackRef_True ((_PyStackRef){ .index = _Py_STACKREF_TRUE_INDEX })

#define INITIAL_STACKREF_INDEX (5 << Py_TAGGED_SHIFT)
static inline _PyStackRef
PyStackRef_WrapBit(bool truthy)
{
return (_PyStackRef){ .index = truthy ? _Py_STACKREF_BIT_1_INDEX
: _Py_STACKREF_BIT_0_INDEX };
}

static inline bool
PyStackRef_UnwrapBit(_PyStackRef bit)
{
assert(bit.index == _Py_STACKREF_BIT_0_INDEX ||
bit.index == _Py_STACKREF_BIT_1_INDEX);
return bit.index == _Py_STACKREF_BIT_1_INDEX;
}

#define INITIAL_STACKREF_INDEX (7 << Py_TAGGED_SHIFT)

#define PyStackRef_ZERO_BITS PyStackRef_NULL

Expand Down Expand Up @@ -472,6 +490,19 @@ static const _PyStackRef PyStackRef_NULL = { .bits = PyStackRef_NULL_BITS };
#define PyStackRef_False ((_PyStackRef){.bits = ((uintptr_t)&_Py_FalseStruct) | Py_TAG_REFCNT })
#define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) | Py_TAG_REFCNT })

static inline _PyStackRef
PyStackRef_WrapBit(bool truthy)
{
return (_PyStackRef){ .bits = (uintptr_t)truthy };
}

static inline bool
PyStackRef_UnwrapBit(_PyStackRef bit)
{
assert(bit.bits == 0 || bit.bits == 1);
return (bool)bit.bits;
}

#ifdef Py_GIL_DISABLED
// Checks that mask out the deferred bit in the free threading build.
#define PyStackRef_IsNone(REF) (((REF).bits & ~Py_TAG_REFCNT) == (uintptr_t)&_Py_NoneStruct)
Expand Down
Loading
Loading