Skip to content

Commit 24d1fc2

Browse files
author
Greg Parker
committed
Add descriptors of weak reference bits for lldb.
1 parent 1b5b8c4 commit 24d1fc2

File tree

2 files changed

+72
-13
lines changed

2 files changed

+72
-13
lines changed

include/swift/ABI/System.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,36 @@
5656
/// ``pointer & SWIFT_ABI_XXX_OBJC_RESERVED_BITS_MASK == 0 &&
5757
/// pointer & SWIFT_ABI_XXX_SWIFT_SPARE_BITS_MASK != 0``.
5858

59+
// Weak references use a marker to tell when they are controlled by
60+
// the ObjC runtime and when they are controlled by the Swift runtime.
61+
// Non-ObjC platforms don't use this marker.
62+
#define SWIFT_ABI_DEFAULT_OBJC_WEAK_REFERENCE_MARKER_MASK 0
63+
#define SWIFT_ABI_DEFAULT_OBJC_WEAK_REFERENCE_MARKER_VALUE 0
64+
5965
/*********************************** i386 *************************************/
6066

6167
// Heap objects are pointer-aligned, so the low two bits are unused.
6268
#define SWIFT_ABI_I386_SWIFT_SPARE_BITS_MASK 0x00000003U
6369

70+
// ObjC weak reference discriminator is the LSB.
71+
#define SWIFT_ABI_I386_OBJC_WEAK_REFERENCE_MARKER_MASK \
72+
(SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK | \
73+
1<<SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS)
74+
#define SWIFT_ABI_I386_OBJC_WEAK_REFERENCE_MARKER_VALUE \
75+
(1<<SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS)
76+
6477
/*********************************** arm **************************************/
6578

6679
// Heap objects are pointer-aligned, so the low two bits are unused.
6780
#define SWIFT_ABI_ARM_SWIFT_SPARE_BITS_MASK 0x00000003U
6881

82+
// ObjC weak reference discriminator is the LSB.
83+
#define SWIFT_ABI_ARM_OBJC_WEAK_REFERENCE_MARKER_MASK \
84+
(SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK | \
85+
1<<SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS)
86+
#define SWIFT_ABI_ARM_OBJC_WEAK_REFERENCE_MARKER_VALUE \
87+
(1<<SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS)
88+
6989
/*********************************** x86-64 ***********************************/
7090

7191
/// Darwin reserves the low 4GB of address space.
@@ -79,6 +99,14 @@
7999
#define SWIFT_ABI_X86_64_OBJC_RESERVED_BITS_MASK 0x8000000000000001ULL
80100
#define SWIFT_ABI_X86_64_OBJC_NUM_RESERVED_LOW_BITS 1
81101

102+
// ObjC weak reference discriminator is the two bits
103+
// reserved for ObjC tagged pointers plus one more low bit.
104+
#define SWIFT_ABI_X86_64_OBJC_WEAK_REFERENCE_MARKER_MASK \
105+
(SWIFT_ABI_X86_64_OBJC_RESERVED_BITS_MASK | \
106+
1<<SWIFT_ABI_X86_64_OBJC_NUM_RESERVED_LOW_BITS)
107+
#define SWIFT_ABI_X86_64_OBJC_WEAK_REFERENCE_MARKER_VALUE \
108+
(1<<SWIFT_ABI_X86_64_OBJC_NUM_RESERVED_LOW_BITS)
109+
82110
/*********************************** arm64 ************************************/
83111

84112
/// Darwin reserves the low 4GB of address space.
@@ -92,6 +120,14 @@
92120
#define SWIFT_ABI_ARM64_OBJC_RESERVED_BITS_MASK 0x8000000000000000ULL
93121
#define SWIFT_ABI_ARM64_OBJC_NUM_RESERVED_LOW_BITS 0
94122

123+
// ObjC weak reference discriminator is the high bit
124+
// reserved for ObjC tagged pointers plus the LSB.
125+
#define SWIFT_ABI_ARM64_OBJC_WEAK_REFERENCE_MARKER_MASK \
126+
(SWIFT_ABI_ARM64_OBJC_RESERVED_BITS_MASK | \
127+
1<<SWIFT_ABI_ARM64_OBJC_NUM_RESERVED_LOW_BITS)
128+
#define SWIFT_ABI_ARM64_OBJC_WEAK_REFERENCE_MARKER_VALUE \
129+
(1<<SWIFT_ABI_ARM64_OBJC_NUM_RESERVED_LOW_BITS)
130+
95131
/*********************************** powerpc64 ********************************/
96132

97133
// Heap objects are pointer-aligned, so the low three bits are unused.

stdlib/public/runtime/WeakReference.h

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,46 @@ namespace swift {
6666
// * concurrent writes other than zeroing
6767

6868
class WeakReferenceBits {
69-
#if SWIFT_OBJC_INTEROP
70-
// NativeMarker: ObjC low bits all zero, next bit 1
69+
// On ObjC platforms, a weak variable may be controlled by the ObjC
70+
// runtime or by the Swift runtime. NativeMarkerMask and NativeMarkerValue
71+
// are used to distinguish them.
72+
// if ((ptr & NativeMarkerMask) == NativeMarkerValue) it's Swift
73+
// else it's ObjC
74+
// NativeMarkerMask incorporates the ObjC tagged pointer bits
75+
// plus one more bit that is set in Swift-controlled weak pointer values.
76+
// Non-ObjC platforms don't use any markers.
7177
enum : uintptr_t {
72-
NativeMarkerValue =
73-
uintptr_t(1) << swift::heap_object_abi::ObjCReservedLowBits,
74-
NativeMarkerMask =
75-
NativeMarkerValue | (NativeMarkerValue - 1)
76-
};
78+
#if !SWIFT_OBJC_INTEROP
79+
NativeMarkerMask = 0,
80+
NativeMarkerValue = 0
81+
#elif __x86_64__
82+
NativeMarkerMask = SWIFT_ABI_X86_64_OBJC_WEAK_REFERENCE_MARKER_MASK,
83+
NativeMarkerValue = SWIFT_ABI_X86_64_OBJC_WEAK_REFERENCE_MARKER_VALUE
84+
#elif __i386__
85+
NativeMarkerMask = SWIFT_ABI_I386_OBJC_WEAK_REFERENCE_MARKER_MASK,
86+
NativeMarkerValue = SWIFT_ABI_I386_OBJC_WEAK_REFERENCE_MARKER_VALUE
87+
#elif __arm__
88+
NativeMarkerMask = SWIFT_ABI_ARM_OBJC_WEAK_REFERENCE_MARKER_MASK,
89+
NativeMarkerValue = SWIFT_ABI_ARM_OBJC_WEAK_REFERENCE_MARKER_VALUE
90+
#elif __arm64__
91+
NativeMarkerMask = SWIFT_ABI_ARM64_OBJC_WEAK_REFERENCE_MARKER_MASK,
92+
NativeMarkerValue = SWIFT_ABI_ARM64_OBJC_WEAK_REFERENCE_MARKER_VALUE
7793
#else
78-
// No NativeMarkerValue needed.
79-
enum : uintptr_t {
80-
NativeMarkerValue = 0, NativeMarkerMask = 0
81-
};
94+
#error unknown architecture
8295
#endif
96+
};
8397

84-
static_assert(NativeMarkerMask < alignof(void*),
85-
"native marker bit must not interfere with pointer bits");
98+
static_assert((NativeMarkerMask & NativeMarkerValue) == NativeMarkerValue,
99+
"native marker value must fall within native marker mask");
100+
static_assert((NativeMarkerMask & heap_object_abi::SwiftSpareBitsMask)
101+
== NativeMarkerMask,
102+
"native marker mask must fall within Swift spare bits");
103+
static_assert((NativeMarkerMask & heap_object_abi::ObjCReservedBitsMask)
104+
== heap_object_abi::ObjCReservedBitsMask,
105+
"native marker mask must contain all ObjC tagged pointer bits");
106+
static_assert((NativeMarkerValue & heap_object_abi::ObjCReservedBitsMask)
107+
== 0,
108+
"native marker value must not interfere with ObjC bits");
86109

87110
uintptr_t bits;
88111

0 commit comments

Comments
 (0)