-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathArray.cpp
225 lines (196 loc) · 7.51 KB
/
Array.cpp
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
//===------------- Array.cpp - Swift Array Operations Support -------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Implementations of the array runtime functions.
//
// arrayInitWithCopy(T *dest, T *src, size_t count, M* self)
// arrayInitWithTake(NoAlias|FrontToBack|BackToFront)(T *dest, T *src,
// size_t count, M* self)
// arrayAssignWithCopy(NoAlias|FrontToBack|BackToFront)(T *dest, T *src,
// size_t count, M* self)
// arrayAssignWithTake(T *dest, T *src, size_t count, M* self)
// arrayDestroy(T *dst, size_t count, M* self)
//
//===----------------------------------------------------------------------===//
#include "BytecodeLayouts.h"
#include "swift/Runtime/Config.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Metadata.h"
using namespace swift;
namespace {
enum class ArrayCopy : unsigned {
NoAlias = 0,
FrontToBack = 1,
BackToFront = 2
};
enum class ArraySource {
Copy,
Take
};
enum class ArrayDest {
Init,
Assign
};
} // end anonymous namespace.
static void array_pod_copy(ArrayCopy copyKind, OpaqueValue *dest,
OpaqueValue *src, size_t stride, size_t count) {
if (copyKind == ArrayCopy::NoAlias) {
memcpy(dest, src, stride * count);
return;
}
assert(copyKind == ArrayCopy::FrontToBack ||
copyKind == ArrayCopy::BackToFront);
memmove(dest, src, stride * count);
}
namespace {
typedef OpaqueValue *(*const WitnessFunction)(OpaqueValue *, OpaqueValue *,
const Metadata *);
}
template <ArrayDest destOp, ArraySource srcOp>
static WitnessFunction get_witness_function(const ValueWitnessTable *wtable) {
if (destOp == ArrayDest::Init) {
if (srcOp == ArraySource::Copy)
return wtable->initializeWithCopy;
else {
assert(srcOp == ArraySource::Take);
return wtable->initializeWithTake;
}
} else {
assert(destOp == ArrayDest::Assign);
if (srcOp == ArraySource::Copy) {
return wtable->assignWithCopy;
} else {
assert(srcOp == ArraySource::Take);
return wtable->assignWithTake;
}
}
}
template <ArrayDest destOp, ArraySource srcOp, ArrayCopy copyKind>
static void array_copy_operation(OpaqueValue *dest, OpaqueValue *src,
size_t count, const Metadata *self) {
if (count == 0)
return;
auto wtable = self->getValueWitnesses();
auto stride = wtable->getStride();
// If we are doing a copy we need PODness for a memcpy.
if (srcOp == ArraySource::Copy) {
auto isPOD = wtable->isPOD();
if (isPOD) {
array_pod_copy(copyKind, dest, src, stride, count);
return;
}
} else {
// Otherwise, we are doing a take and need bitwise takability for a copy.
assert(srcOp == ArraySource::Take);
auto isBitwiseTakable = wtable->isBitwiseTakable();
if (isBitwiseTakable && (destOp == ArrayDest::Init || wtable->isPOD())) {
array_pod_copy(copyKind, dest, src, stride, count);
return;
}
}
// Call the witness to do the copy.
if (copyKind == ArrayCopy::NoAlias || copyKind == ArrayCopy::FrontToBack) {
if (self->hasLayoutString() && destOp == ArrayDest::Init &&
srcOp == ArraySource::Copy) {
return swift_cvw_arrayInitWithCopy(dest, src, count, stride, self);
}
if (self->hasLayoutString() && destOp == ArrayDest::Assign &&
srcOp == ArraySource::Copy) {
return swift_cvw_arrayAssignWithCopy(dest, src, count, stride, self);
}
auto copy = get_witness_function<destOp, srcOp>(wtable);
for (size_t i = 0; i < count; ++i) {
auto offset = i * stride;
auto *from = reinterpret_cast<OpaqueValue *>((char *)src + offset);
auto *to = reinterpret_cast<OpaqueValue *>((char *)dest + offset);
copy(to, from, self);
}
return;
}
// Back-to-front copy.
assert(copyKind == ArrayCopy::BackToFront);
assert(count != 0);
auto copy = get_witness_function<destOp, srcOp>(wtable);
size_t i = count;
do {
auto offset = --i * stride;
auto *from = reinterpret_cast<OpaqueValue *>((char *)src + offset);
auto *to = reinterpret_cast<OpaqueValue *>((char *)dest + offset);
copy(to, from, self);
} while (i != 0);
}
SWIFT_RUNTIME_EXPORT
void swift_arrayInitWithCopy(OpaqueValue *dest, OpaqueValue *src, size_t count,
const Metadata *self) {
array_copy_operation<ArrayDest::Init, ArraySource::Copy, ArrayCopy::NoAlias>(
dest, src, count, self);
}
SWIFT_RUNTIME_EXPORT
void swift_arrayInitWithTakeNoAlias(OpaqueValue *dest, OpaqueValue *src,
size_t count, const Metadata *self) {
array_copy_operation<ArrayDest::Init, ArraySource::Take, ArrayCopy::NoAlias>(
dest, src, count, self);
}
SWIFT_RUNTIME_EXPORT
void swift_arrayInitWithTakeFrontToBack(OpaqueValue *dest, OpaqueValue *src,
size_t count, const Metadata *self) {
array_copy_operation<ArrayDest::Init, ArraySource::Take,
ArrayCopy::FrontToBack>(dest, src, count, self);
}
SWIFT_RUNTIME_EXPORT
void swift_arrayInitWithTakeBackToFront(OpaqueValue *dest, OpaqueValue *src,
size_t count, const Metadata *self) {
array_copy_operation<ArrayDest::Init, ArraySource::Take,
ArrayCopy::BackToFront>(dest, src, count, self);
}
SWIFT_RUNTIME_EXPORT
void swift_arrayAssignWithCopyNoAlias(OpaqueValue *dest, OpaqueValue *src,
size_t count, const Metadata *self) {
array_copy_operation<ArrayDest::Assign, ArraySource::Copy,
ArrayCopy::NoAlias>(dest, src, count, self);
}
SWIFT_RUNTIME_EXPORT
void swift_arrayAssignWithCopyFrontToBack(OpaqueValue *dest, OpaqueValue *src,
size_t count, const Metadata *self) {
array_copy_operation<ArrayDest::Assign, ArraySource::Copy,
ArrayCopy::FrontToBack>(dest, src, count, self);
}
SWIFT_RUNTIME_EXPORT
void swift_arrayAssignWithCopyBackToFront(OpaqueValue *dest, OpaqueValue *src,
size_t count, const Metadata *self) {
array_copy_operation<ArrayDest::Assign, ArraySource::Copy,
ArrayCopy::BackToFront>(dest, src, count, self);
}
SWIFT_RUNTIME_EXPORT
void swift_arrayAssignWithTake(OpaqueValue *dest, OpaqueValue *src,
size_t count, const Metadata *self) {
array_copy_operation<ArrayDest::Assign, ArraySource::Take,
ArrayCopy::NoAlias>(dest, src, count, self);
}
SWIFT_RUNTIME_EXPORT
void swift_arrayDestroy(OpaqueValue *begin, size_t count, const Metadata *self) {
if (count == 0)
return;
auto wtable = self->getValueWitnesses();
// Nothing to do if the type is POD.
if (wtable->isPOD())
return;
auto stride = wtable->getStride();
if (self->hasLayoutString()) {
return swift_cvw_arrayDestroy(begin, count, stride, self);
}
for (size_t i = 0; i < count; ++i) {
auto offset = i * stride;
auto *obj = reinterpret_cast<OpaqueValue *>((char *)begin + offset);
wtable->destroy(obj, self);
}
}