Skip to content

Commit 3d04fb5

Browse files
committed
[shims] Move bit masks to SwiftShims and include/swift/ABI.
Move bits mask from Metadata.h to SwiftShims's HeapObject.h. This exposes the bit masks to the stdlib, so that the stdlib doesn't have to have its own magic numbers per-platform. This also enhances readability for BridgeObject, whose magic numbers are mostly derived from Swift's ABI.
1 parent f2ef42b commit 3d04fb5

File tree

7 files changed

+266
-289
lines changed

7 files changed

+266
-289
lines changed

include/swift/ABI/System.h

Lines changed: 4 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -14,128 +14,9 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#ifndef SWIFT_ABI_SYSTEM_H
18-
#define SWIFT_ABI_SYSTEM_H
17+
#ifndef __SWIFT_ABI_SYSTEM_H__
18+
#define __SWIFT_ABI_SYSTEM_H__
1919

20-
// In general, these macros are expected to expand to host-independent
21-
// integer constant expressions. This allows the same data to feed
22-
// both the compiler and runtime implementation.
20+
#include "../../../stdlib/public/SwiftShims/System.h"
2321

24-
/******************************* Default Rules ********************************/
25-
26-
/// The least valid pointer value for an actual pointer (as opposed to
27-
/// Objective-C pointers, which may be tagged pointers and are covered
28-
/// separately). Values up to this are "extra inhabitants" of the
29-
/// pointer representation, and payloaded enum types can take
30-
/// advantage of that as they see fit.
31-
///
32-
/// By default, we assume that there's at least an unmapped page at
33-
/// the bottom of the address space. 4K is a reasonably likely page
34-
/// size.
35-
///
36-
/// The minimum possible value for this macro is 1; we always assume
37-
/// that the null representation is available.
38-
#define SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER 4096
39-
40-
/// The bitmask of spare bits in a function pointer.
41-
#define SWIFT_ABI_DEFAULT_FUNCTION_SPARE_BITS_MASK 0
42-
43-
/// The bitmask of spare bits in a Swift heap object pointer. A Swift
44-
/// heap object allocation will never set any of these bits.
45-
#define SWIFT_ABI_DEFAULT_SWIFT_SPARE_BITS_MASK 0
46-
47-
/// The bitmask of reserved bits in an Objective-C object pointer.
48-
/// By default we assume the ObjC runtime doesn't use tagged pointers.
49-
#define SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK 0
50-
51-
/// The number of low bits in an Objective-C object pointer that
52-
/// are reserved by the Objective-C runtime.
53-
#define SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS 0
54-
55-
/// The ObjC runtime will not use pointer values for which
56-
/// ``pointer & SWIFT_ABI_XXX_OBJC_RESERVED_BITS_MASK == 0 &&
57-
/// pointer & SWIFT_ABI_XXX_SWIFT_SPARE_BITS_MASK != 0``.
58-
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-
65-
/*********************************** i386 *************************************/
66-
67-
// Heap objects are pointer-aligned, so the low two bits are unused.
68-
#define SWIFT_ABI_I386_SWIFT_SPARE_BITS_MASK 0x00000003U
69-
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-
77-
/*********************************** arm **************************************/
78-
79-
// Heap objects are pointer-aligned, so the low two bits are unused.
80-
#define SWIFT_ABI_ARM_SWIFT_SPARE_BITS_MASK 0x00000003U
81-
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-
89-
/*********************************** x86-64 ***********************************/
90-
91-
/// Darwin reserves the low 4GB of address space.
92-
#define SWIFT_ABI_DARWIN_X86_64_LEAST_VALID_POINTER (4ULL*1024*1024*1024)
93-
94-
// Only the bottom 56 bits are used, and heap objects are eight-byte-aligned.
95-
#define SWIFT_ABI_X86_64_SWIFT_SPARE_BITS_MASK 0xFF00000000000007ULL
96-
97-
// Objective-C reserves the high and low bits for tagged pointers.
98-
// Systems exist which use either bit.
99-
#define SWIFT_ABI_X86_64_OBJC_RESERVED_BITS_MASK 0x8000000000000001ULL
100-
#define SWIFT_ABI_X86_64_OBJC_NUM_RESERVED_LOW_BITS 1
101-
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-
110-
/*********************************** arm64 ************************************/
111-
112-
/// Darwin reserves the low 4GB of address space.
113-
#define SWIFT_ABI_DARWIN_ARM64_LEAST_VALID_POINTER (4ULL*1024*1024*1024)
114-
115-
// TBI guarantees the top byte of pointers is unused.
116-
// Heap objects are eight-byte aligned.
117-
#define SWIFT_ABI_ARM64_SWIFT_SPARE_BITS_MASK 0xFF00000000000007ULL
118-
119-
// Objective-C reserves just the high bit for tagged pointers.
120-
#define SWIFT_ABI_ARM64_OBJC_RESERVED_BITS_MASK 0x8000000000000000ULL
121-
#define SWIFT_ABI_ARM64_OBJC_NUM_RESERVED_LOW_BITS 0
122-
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-
131-
/*********************************** powerpc64 ********************************/
132-
133-
// Heap objects are pointer-aligned, so the low three bits are unused.
134-
#define SWIFT_ABI_POWERPC64_SWIFT_SPARE_BITS_MASK 0x0000000000000007ULL
135-
136-
/*********************************** s390x ************************************/
137-
138-
// Top byte of pointers is unused, and heap objects are eight-byte aligned.
139-
#define SWIFT_ABI_S390X_SWIFT_SPARE_BITS_MASK 0x0000000000000007ULL
140-
141-
#endif /* SWIFT_ABI_SYSTEM_H */
22+
#endif // __SWIFT_ABI_SYSTEM_H__

include/swift/Runtime/Metadata.h

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -719,87 +719,7 @@ namespace {
719719
using type = R;
720720
};
721721
}
722-
723-
namespace heap_object_abi {
724-
725-
// The extra inhabitants and spare bits of heap object pointers.
726-
// These must align with the values in IRGen's SwiftTargetInfo.cpp.
727-
#if defined(__x86_64__)
728-
729-
# ifdef __APPLE__
730-
static const uintptr_t LeastValidPointerValue =
731-
SWIFT_ABI_DARWIN_X86_64_LEAST_VALID_POINTER;
732-
# else
733-
static const uintptr_t LeastValidPointerValue =
734-
SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER;
735-
# endif
736-
static const uintptr_t SwiftSpareBitsMask =
737-
SWIFT_ABI_X86_64_SWIFT_SPARE_BITS_MASK;
738-
static const uintptr_t ObjCReservedBitsMask =
739-
SWIFT_ABI_X86_64_OBJC_RESERVED_BITS_MASK;
740-
static const unsigned ObjCReservedLowBits =
741-
SWIFT_ABI_X86_64_OBJC_NUM_RESERVED_LOW_BITS;
742-
743-
#elif defined(__arm64__)
744-
745-
# ifdef __APPLE__
746-
static const uintptr_t LeastValidPointerValue =
747-
SWIFT_ABI_DARWIN_ARM64_LEAST_VALID_POINTER;
748-
# else
749-
static const uintptr_t LeastValidPointerValue =
750-
SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER;
751-
# endif
752-
static const uintptr_t SwiftSpareBitsMask =
753-
SWIFT_ABI_ARM64_SWIFT_SPARE_BITS_MASK;
754-
static const uintptr_t ObjCReservedBitsMask =
755-
SWIFT_ABI_ARM64_OBJC_RESERVED_BITS_MASK;
756-
static const unsigned ObjCReservedLowBits =
757-
SWIFT_ABI_ARM64_OBJC_NUM_RESERVED_LOW_BITS;
758-
759-
#elif defined(__powerpc64__)
760-
761-
static const uintptr_t LeastValidPointerValue =
762-
SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER;
763-
static const uintptr_t SwiftSpareBitsMask =
764-
SWIFT_ABI_POWERPC64_SWIFT_SPARE_BITS_MASK;
765-
static const uintptr_t ObjCReservedBitsMask =
766-
SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK;
767-
static const unsigned ObjCReservedLowBits =
768-
SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS;
769-
770-
#elif defined(__s390x__)
771-
772-
static const uintptr_t LeastValidPointerValue =
773-
SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER;
774-
static const uintptr_t SwiftSpareBitsMask =
775-
SWIFT_ABI_S390X_SWIFT_SPARE_BITS_MASK;
776-
static const uintptr_t ObjCReservedBitsMask =
777-
SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK;
778-
static const unsigned ObjCReservedLowBits =
779-
SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS;
780-
781-
#else
782-
783-
static const uintptr_t LeastValidPointerValue =
784-
SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER;
785-
static const uintptr_t SwiftSpareBitsMask =
786-
# if __i386__
787-
SWIFT_ABI_I386_SWIFT_SPARE_BITS_MASK
788-
# elif __arm__
789-
SWIFT_ABI_ARM_SWIFT_SPARE_BITS_MASK
790-
# else
791-
SWIFT_ABI_DEFAULT_SWIFT_SPARE_BITS_MASK
792-
# endif
793-
;
794-
static const uintptr_t ObjCReservedBitsMask =
795-
SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK;
796-
static const unsigned ObjCReservedLowBits =
797-
SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS;
798722

799-
#endif
800-
801-
}
802-
803723
template <typename Runtime> struct TargetNominalTypeDescriptor;
804724
template <typename Runtime> struct TargetGenericMetadata;
805725
template <typename Runtime> struct TargetClassMetadata;

stdlib/public/SwiftShims/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(sources
1212
SwiftStdbool.h
1313
SwiftStddef.h
1414
SwiftStdint.h
15+
System.h
1516
UnicodeShims.h
1617
Visibility.h
1718

stdlib/public/SwiftShims/HeapObject.h

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define SWIFT_STDLIB_SHIMS_HEAPOBJECT_H
1414

1515
#include "RefCount.h"
16+
#include "System.h"
1617

1718
#define SWIFT_ABI_HEAP_OBJECT_HEADER_SIZE_64 16
1819
// TODO: Should be 8
@@ -53,7 +54,7 @@ struct HeapObject {
5354
: metadata(newMetadata)
5455
, refCounts(InlineRefCounts::Initialized)
5556
{ }
56-
#endif
57+
#endif // __cplusplus
5758
};
5859

5960
#ifdef __cplusplus
@@ -88,6 +89,105 @@ static_assert(alignof(HeapObject) == alignof(void*),
8889
"HeapObject must be pointer-aligned");
8990

9091
} // end namespace swift
92+
#endif // __cplusplus
93+
94+
/// Global bit masks
95+
96+
// TODO(<rdar://problem/34837179>): Convert each macro below to static consts
97+
// when static consts are visible to SIL.
98+
99+
// The extra inhabitants and spare bits of heap object pointers.
100+
// These must align with the values in IRGen's SwiftTargetInfo.cpp.
101+
#if defined(__x86_64__)
102+
103+
#ifdef __APPLE__
104+
#define _swift_abi_LeastValidPointerValue \
105+
(__swift_uintptr_t) SWIFT_ABI_DARWIN_X86_64_LEAST_VALID_POINTER
106+
#else
107+
#define _swift_abi_LeastValidPointerValue \
108+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER
109+
#endif
110+
#define _swift_abi_SwiftSpareBitsMask \
111+
(__swift_uintptr_t) SWIFT_ABI_X86_64_SWIFT_SPARE_BITS_MASK
112+
#define _swift_abi_ObjCReservedBitsMask \
113+
(__swift_uintptr_t) SWIFT_ABI_X86_64_OBJC_RESERVED_BITS_MASK
114+
#define _swift_abi_ObjCReservedLowBits \
115+
(unsigned) SWIFT_ABI_X86_64_OBJC_NUM_RESERVED_LOW_BITS
116+
117+
#elif defined(__arm64__)
118+
119+
#ifdef __APPLE__
120+
#define _swift_abi_LeastValidPointerValue \
121+
(__swift_uintptr_t) SWIFT_ABI_DARWIN_ARM64_LEAST_VALID_POINTER
122+
#else
123+
#define _swift_abi_LeastValidPointerValue \
124+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER
125+
#endif
126+
#define _swift_abi_SwiftSpareBitsMask \
127+
(__swift_uintptr_t) SWIFT_ABI_ARM64_SWIFT_SPARE_BITS_MASK
128+
#define _swift_abi_ObjCReservedBitsMask \
129+
(__swift_uintptr_t) SWIFT_ABI_ARM64_OBJC_RESERVED_BITS_MASK
130+
#define _swift_abi_ObjCReservedLowBits \
131+
(unsigned) SWIFT_ABI_ARM64_OBJC_NUM_RESERVED_LOW_BITS
132+
133+
#elif defined(__powerpc64__)
134+
135+
#define _swift_abi_LeastValidPointerValue \
136+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER
137+
#define _swift_abi_SwiftSpareBitsMask \
138+
(__swift_uintptr_t) SWIFT_ABI_POWERPC64_SWIFT_SPARE_BITS_MASK
139+
#define _swift_abi_ObjCReservedBitsMask \
140+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK
141+
#define _swift_abi_ObjCReservedLowBits \
142+
(unsigned) SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS
143+
144+
#elif defined(__s390x__)
145+
146+
#define _swift_abi_LeastValidPointerValue \
147+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER
148+
#define _swift_abi_SwiftSpareBitsMask \
149+
(__swift_uintptr_t) SWIFT_ABI_S390X_SWIFT_SPARE_BITS_MASK
150+
#define _swift_abi_ObjCReservedBitsMask \
151+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK
152+
#define _swift_abi_ObjCReservedLowBits \
153+
(unsigned) SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS
154+
155+
#else
156+
157+
#define _swift_abi_LeastValidPointerValue \
158+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER
159+
160+
#if __i386__
161+
#define _swift_abi_SwiftSpareBitsMask \
162+
(__swift_uintptr_t) SWIFT_ABI_I386_SWIFT_SPARE_BITS_MASK
163+
#elif __arm__
164+
#define _swift_abi_SwiftSpareBitsMask \
165+
(__swift_uintptr_t) SWIFT_ABI_ARM_SWIFT_SPARE_BITS_MASK
166+
#else
167+
#define _swift_abi_SwiftSpareBitsMask \
168+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_SWIFT_SPARE_BITS_MASK
91169
#endif
92170

171+
#define _swift_abi_ObjCReservedBitsMask \
172+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK
173+
#define _swift_abi_ObjCReservedLowBits \
174+
(unsigned) SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS
93175
#endif
176+
177+
/// Corresponding namespaced decls
178+
#ifdef __cplusplus
179+
namespace heap_object_abi {
180+
static const uintptr_t LeastValidPointerValue =
181+
_swift_abi_LeastValidPointerValue;
182+
static const uintptr_t SwiftSpareBitsMask = _swift_abi_SwiftSpareBitsMask;
183+
static const uintptr_t ObjCReservedBitsMask = _swift_abi_ObjCReservedBitsMask;
184+
static const unsigned ObjCReservedLowBits = _swift_abi_ObjCReservedLowBits;
185+
} // heap_object_abi
186+
#endif // __cplusplus
187+
188+
/// BridgeObject masks
189+
190+
#define _swift_BridgeObject_TaggedPointerBits _swift_abi_ObjCReservedBitsMask
191+
192+
193+
#endif // SWIFT_STDLIB_SHIMS_HEAPOBJECT_H

0 commit comments

Comments
 (0)