1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
//===--- KnownMetadata.cpp - Swift Language ABI Known Metadata Objects ----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Definitions of some builtin metadata objects.
//
//===----------------------------------------------------------------------===//
#include "swift/Runtime/Metadata.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Numeric.h"
#include "MetadataImpl.h"
#include "Private.h"
#include <cstring>
#include <climits>
namespace swift {
class AsyncTask;
class Job;
}
using namespace swift;
using namespace metadataimpl;
/// Copy a value from one object to another based on the size in the
/// given type metadata.
OpaqueValue *swift::swift_copyPOD(OpaqueValue *dest, OpaqueValue *src,
const Metadata *type) {
return (OpaqueValue*) memcpy(dest, src, type->getValueWitnesses()->size);
}
namespace {
// A type sized and aligned the way Swift wants Int128 (and Float80/Float128)
// to be sized and aligned.
struct alignas(16) int128_like {
char data[16];
};
static_assert(MaximumAlignment == 16, "max alignment was hardcoded");
struct alignas(16) int256_like {
char data[32];
};
struct alignas(16) int512_like {
char data[64];
};
struct alignas(16) float80_like {
char data[10];
};
} // end anonymous namespace
namespace ctypes {
namespace {
// Type definitions that map each names Swift builtin type to their
// C counterparts.
using Bi1_ = uint8_t;
using Bi7_ = uint8_t;
using Bi8_ = uint8_t;
using Bi16_ = uint16_t;
using Bi32_ = uint32_t;
using Bi63_ = uint64_t;
using Bi64_ = uint64_t;
using Bi128_ = int128_like;
using Bi256_ = int256_like;
using Bi512_ = int512_like;
using Bw = intptr_t;
using BL_ = IntegerLiteral;
using Bf16_ = uint16_t;
using Bf32_ = float;
using Bf64_ = double;
using Bf80_ = float80_like;
using Bf128_ = int128_like;
/// The value-witness table for UnsafeValueBuffer. You can do layout
/// with this, but the type isn't copyable, so most of the value
/// operations are meaningless.
using BB = ValueBuffer;
// Types that are defined in the _Concurrency library
// Default actor storage type.
struct alignas(2 * alignof(void*)) BD {
void *storage[NumWords_DefaultActor];
};
// SerialExecutorRef type.
struct Be {
HeapObject *Identity;
uintptr_t Implementation;
};
// Types that are defined in the Distributed library
// Non-default distributed actor storage type.
struct alignas(2 * alignof(void*)) Bd {
void *storage[NumWords_NonDefaultDistributedActor];
};
}
}
namespace pointer_types {
namespace {
/// The basic value-witness table for Swift object pointers.
using Bo = SwiftRetainableBox;
/// The value-witness table for raw pointers.
using Bp = RawPointerBox;
/// The value-witness table for BridgeObject.
using Bb = BridgeObjectBox;
// RawUnsafeContinuation type.
using Bc = RawPointerBox;
// Job type.
using Bj = RawPointerBox;
#if SWIFT_OBJC_INTEROP
/*** Objective-C pointers *************************************************/
// This section can reasonably be suppressed in builds that don't
// need to support Objective-C.
/// The basic value-witness table for ObjC object pointers.
using BO = ObjCRetainableBox;
#else
using BO = UnknownObjectRetainableBox;
#endif
}
}
namespace {
template <typename T>
struct BuiltinType {
static constexpr const size_t Alignment = alignof(T);
};
#define SET_FIXED_ALIGNMENT(Type, Align) \
template <> \
struct BuiltinType<Type> { \
static constexpr const size_t Alignment = Align; \
};
SET_FIXED_ALIGNMENT(uint8_t, 1)
SET_FIXED_ALIGNMENT(uint16_t, 2)
SET_FIXED_ALIGNMENT(uint32_t, 4)
SET_FIXED_ALIGNMENT(uint64_t, 8)
SET_FIXED_ALIGNMENT(int128_like, 16)
static_assert(MaximumAlignment == 16, "max alignment was hardcoded");
SET_FIXED_ALIGNMENT(int256_like, 16)
SET_FIXED_ALIGNMENT(int512_like, 16)
#undef SET_FIXED_ALIGNMENT
template <typename T, unsigned N>
struct SIMDVector {
using Type = T __attribute__((__ext_vector_type__(N)));
};
template <>
struct SIMDVector<float, 3> {
using Type = float __attribute__((__ext_vector_type__(4)));
};
template <>
struct SIMDVector<double, 3> {
using Type = double __attribute__((__ext_vector_type__(4)));
};
template <typename T, unsigned N>
using SIMDVectorType = typename SIMDVector<T, N>::Type;
}
#define BUILTIN_TYPE(Symbol, Name) \
const ValueWitnessTable swift::VALUE_WITNESS_SYM(Symbol) = \
ValueWitnessTableForBox<NativeBox<ctypes::Symbol, \
BuiltinType<ctypes::Symbol>::Alignment>>::table;
#define BUILTIN_POINTER_TYPE(Symbol, Name) \
const ValueWitnessTable swift::VALUE_WITNESS_SYM(Symbol) = \
ValueWitnessTableForBox<pointer_types::Symbol>::table;
#if SWIFT_STDLIB_ENABLE_VECTOR_TYPES
#define BUILTIN_VECTOR_TYPE(ElementSymbol, _, Width) \
const ValueWitnessTable \
swift::VALUE_WITNESS_SYM(VECTOR_BUILTIN_SYMBOL_NAME(ElementSymbol,Width)) = \
ValueWitnessTableForBox<NativeBox<SIMDVectorType<ctypes::ElementSymbol, \
Width>>>::table;
#else
#define BUILTIN_VECTOR_TYPE(ElementSymbol, ElementName, Width)
#endif
#include "swift/Runtime/BuiltinTypes.def"
/// The value-witness table for pointer-aligned unmanaged pointer types.
const ValueWitnessTable swift::METATYPE_VALUE_WITNESS_SYM(Bo) =
ValueWitnessTableForBox<PointerPointerBox>::table;
/*** Functions ***************************************************************/
namespace {
// @escaping function types.
struct ThickFunctionBox
: AggregateBox<FunctionPointerBox, SwiftRetainableBox> {
static constexpr unsigned numExtraInhabitants =
FunctionPointerBox::numExtraInhabitants;
static void storeExtraInhabitantTag(char *dest, unsigned tag) {
FunctionPointerBox::storeExtraInhabitantTag((void**) dest, tag);
}
static unsigned getExtraInhabitantTag(const char *src) {
return FunctionPointerBox::getExtraInhabitantTag((void * const *) src);
}
};
/// @noescape function types.
struct TrivialThickFunctionBox
: AggregateBox<FunctionPointerBox, RawPointerBox> {
static constexpr unsigned numExtraInhabitants =
FunctionPointerBox::numExtraInhabitants;
static void storeExtraInhabitantTag(char *dest, unsigned tag) {
FunctionPointerBox::storeExtraInhabitantTag((void **)dest, tag);
}
static unsigned getExtraInhabitantTag(const char *src) {
return FunctionPointerBox::getExtraInhabitantTag((void *const *)src);
}
};
struct DiffFunctionBox
: AggregateBox<ThickFunctionBox, ThickFunctionBox, ThickFunctionBox> {
static constexpr unsigned numExtraInhabitants =
ThickFunctionBox::numExtraInhabitants;
static void storeExtraInhabitantTag(char *dest, unsigned tag) {
ThickFunctionBox::storeExtraInhabitantTag(dest, tag);
}
static unsigned getExtraInhabitantTag(const char *src) {
return ThickFunctionBox::getExtraInhabitantTag(src);
}
};
} // end anonymous namespace
/// The basic value-witness table for escaping function types.
const ValueWitnessTable
swift::VALUE_WITNESS_SYM(FUNCTION_MANGLING) =
ValueWitnessTableForBox<ThickFunctionBox>::table;
const ValueWitnessTable
swift::VALUE_WITNESS_SYM(DIFF_FUNCTION_MANGLING) =
ValueWitnessTableForBox<DiffFunctionBox>::table;
/// The basic value-witness table for @noescape function types.
const ValueWitnessTable
swift::VALUE_WITNESS_SYM(NOESCAPE_FUNCTION_MANGLING) =
ValueWitnessTableForBox<TrivialThickFunctionBox>::table;
/// The basic value-witness table for thin function types.
const ValueWitnessTable
swift::VALUE_WITNESS_SYM(THIN_FUNCTION_MANGLING) =
ValueWitnessTableForBox<FunctionPointerBox>::table;
/*** Empty tuples ************************************************************/
/// The basic value-witness table for empty types.
const ValueWitnessTable swift::VALUE_WITNESS_SYM(EMPTY_TUPLE_MANGLING) =
ValueWitnessTableForBox<AggregateBox<>>::table;
/*** Known metadata **********************************************************/
// Define some builtin opaque metadata.
#define OPAQUE_METADATA(TYPE) \
const FullOpaqueMetadata swift::METADATA_SYM(TYPE) = { \
{ &VALUE_WITNESS_SYM(TYPE) }, \
{ { MetadataKind::Opaque } } \
};
#define BUILTIN_TYPE(Symbol, Name) \
OPAQUE_METADATA(Symbol)
#if !SWIFT_STDLIB_ENABLE_VECTOR_TYPES
#define BUILTIN_VECTOR_TYPE(ElementSymbol, ElementName, Width)
#endif
#include "swift/Runtime/BuiltinTypes.def"
/// The standard metadata for the empty tuple.
const FullMetadata<TupleTypeMetadata> swift::
METADATA_SYM(EMPTY_TUPLE_MANGLING) = {
{ &VALUE_WITNESS_SYM(EMPTY_TUPLE_MANGLING) }, // ValueWitnesses
{
{ MetadataKind::Tuple }, // Kind
0, // NumElements
nullptr // Labels
}
};
|