forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuntime.swift.gyb
535 lines (474 loc) · 16.5 KB
/
Runtime.swift.gyb
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// This file contains Swift wrappers for functions defined in the C++ runtime.
///
//===----------------------------------------------------------------------===//
import SwiftShims
//===----------------------------------------------------------------------===//
// Atomics
//===----------------------------------------------------------------------===//
@_transparent
@warn_unused_result
public // @testable
func _stdlib_atomicCompareExchangeStrongPtrImpl(
object target: UnsafeMutablePointer<COpaquePointer>,
expected: UnsafeMutablePointer<COpaquePointer>,
desired: COpaquePointer) -> Bool {
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_RawPointer(
target._rawValue, expected.memory._rawValue, desired._rawValue)
expected.memory._rawValue = oldValue
return Bool(won)
}
/// Atomic compare and exchange of `UnsafeMutablePointer<T>` with sequentially
/// consistent memory ordering. Precise semantics are defined in C++11 or C11.
///
/// - Warning: This operation is extremely tricky to use correctly because of
/// writeback semantics.
///
/// It is best to use it directly on an
/// `UnsafeMutablePointer<UnsafeMutablePointer<T>>` that is known to point
/// directly to the memory where the value is stored.
///
/// In a call like this:
///
/// _stdlib_atomicCompareExchangeStrongPtr(&foo.property1.property2, ...)
///
/// you need to manually make sure that:
///
/// - all properties in the chain are physical (to make sure that no writeback
/// happens; the compare-and-exchange instruction should operate on on the
/// shared memory); and
///
/// - the shared memory that you are accessing is located inside a heap
/// allocation (a class instance property, a `_HeapBuffer`, a pointer to
/// an `Array` element etc.)
///
/// If the conditions above are not met, the code will still compile, but the
/// compare-and-exchange instruction will operate on the writeback buffer, and
/// you will get a *race* while doing writeback into shared memory.
@_transparent
@warn_unused_result
public // @testable
func _stdlib_atomicCompareExchangeStrongPtr<T>(
object target: UnsafeMutablePointer<UnsafeMutablePointer<T>>,
expected: UnsafeMutablePointer<UnsafeMutablePointer<T>>,
desired: UnsafeMutablePointer<T>) -> Bool {
return _stdlib_atomicCompareExchangeStrongPtrImpl(
object: UnsafeMutablePointer(target),
expected: UnsafeMutablePointer(expected),
desired: COpaquePointer(desired))
}
@_transparent
public // @testable
func _stdlib_atomicInitializeARCRef(
object target: UnsafeMutablePointer<AnyObject?>,
desired: AnyObject) -> Bool {
var expected = COpaquePointer()
let desiredPtr = Unmanaged.passRetained(desired).toOpaque()
let wonRace = _stdlib_atomicCompareExchangeStrongPtrImpl(
object: UnsafeMutablePointer(target),
expected: &expected,
desired: desiredPtr)
if !wonRace {
// Some other thread initialized the value. Balance the retain that we
// performed on 'desired'.
Unmanaged.passUnretained(desired).release()
}
return wonRace
}
% for bits in [ 32, 64 ]:
@warn_unused_result
@_transparent
public // @testable
func _stdlib_atomicCompareExchangeStrongUInt${bits}(
object target: UnsafeMutablePointer<UInt${bits}>,
expected: UnsafeMutablePointer<UInt${bits}>,
desired: UInt${bits}) -> Bool {
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int${bits}(
target._rawValue, expected.memory._value, desired._value)
expected.memory._value = oldValue
return Bool(won)
}
@warn_unused_result
@_transparent
public // @testable
func _stdlib_atomicCompareExchangeStrongInt${bits}(
object target: UnsafeMutablePointer<Int${bits}>,
expected: UnsafeMutablePointer<Int${bits}>,
desired: Int${bits}) -> Bool {
return _stdlib_atomicCompareExchangeStrongUInt${bits}(
object: UnsafeMutablePointer(target),
expected: UnsafeMutablePointer(expected),
desired: UInt${bits}(bitPattern: desired))
}
@_transparent
public // @testable
func _swift_stdlib_atomicStoreUInt${bits}(
object target: UnsafeMutablePointer<UInt${bits}>,
desired: UInt${bits}) {
Builtin.atomicstore_seqcst_Int${bits}(target._rawValue, desired._value)
}
func _swift_stdlib_atomicStoreInt${bits}(
object target: UnsafeMutablePointer<Int${bits}>,
desired: Int${bits}) {
return _swift_stdlib_atomicStoreUInt${bits}(
object: UnsafeMutablePointer(target),
desired: UInt${bits}(bitPattern: desired))
}
@warn_unused_result
public // @testable
func _swift_stdlib_atomicLoadUInt${bits}(
object target: UnsafeMutablePointer<UInt${bits}>) -> UInt${bits} {
let value = Builtin.atomicload_seqcst_Int${bits}(target._rawValue)
return UInt${bits}(value)
}
@warn_unused_result
func _swift_stdlib_atomicLoadInt${bits}(
object target: UnsafeMutablePointer<Int${bits}>) -> Int${bits} {
return Int${bits}(bitPattern:
_swift_stdlib_atomicLoadUInt${bits}(
object: UnsafeMutablePointer(target)))
}
% for operation in [ 'Add', 'And', 'Or', 'Xor' ]:
// Warning: no overflow checking.
@_transparent
public // @testable
func _swift_stdlib_atomicFetch${operation}UInt${bits}(
object target: UnsafeMutablePointer<UInt${bits}>,
operand: UInt${bits}) -> UInt${bits} {
let value = Builtin.atomicrmw_${operation.lower()}_seqcst_Int${bits}(
target._rawValue, operand._value)
return UInt${bits}(value)
}
// Warning: no overflow checking.
@warn_unused_result
func _swift_stdlib_atomicFetch${operation}Int${bits}(
object target: UnsafeMutablePointer<Int${bits}>,
operand: Int${bits}) -> Int${bits} {
return Int${bits}(bitPattern:
_swift_stdlib_atomicFetch${operation}UInt${bits}(
object: UnsafeMutablePointer(target),
operand: UInt${bits}(bitPattern: operand)))
}
% end
% end
@warn_unused_result
func _stdlib_atomicCompareExchangeStrongInt(
object target: UnsafeMutablePointer<Int>,
expected: UnsafeMutablePointer<Int>,
desired: Int) -> Bool {
#if arch(i386) || arch(arm)
return _stdlib_atomicCompareExchangeStrongUInt32(
object: UnsafeMutablePointer(target),
expected: UnsafeMutablePointer(expected),
desired: UInt32(bitPattern: Int32(desired)))
#elseif arch(x86_64) || arch(arm64)
return _stdlib_atomicCompareExchangeStrongUInt64(
object: UnsafeMutablePointer(target),
expected: UnsafeMutablePointer(expected),
desired: UInt64(bitPattern: Int64(desired)))
#endif
}
func _swift_stdlib_atomicStoreInt(
object target: UnsafeMutablePointer<Int>,
desired: Int) {
#if arch(i386) || arch(arm)
return _swift_stdlib_atomicStoreUInt32(
object: UnsafeMutablePointer(target),
desired: UInt32(bitPattern: Int32(desired)))
#elseif arch(x86_64) || arch(arm64)
return _swift_stdlib_atomicStoreUInt64(
object: UnsafeMutablePointer(target),
desired: UInt64(bitPattern: Int64(desired)))
#endif
}
@_transparent
@warn_unused_result
public func _swift_stdlib_atomicLoadInt(
object target: UnsafeMutablePointer<Int>) -> Int {
#if arch(i386) || arch(arm)
return Int(Int32(bitPattern:
_swift_stdlib_atomicLoadUInt32(
object: UnsafeMutablePointer(target))))
#elseif arch(x86_64) || arch(arm64)
return Int(Int64(bitPattern:
_swift_stdlib_atomicLoadUInt64(
object: UnsafeMutablePointer(target))))
#endif
}
@warn_unused_result
@_transparent
public // @testable
func _swift_stdlib_atomicLoadPtrImpl(
object target: UnsafeMutablePointer<COpaquePointer>
) -> COpaquePointer {
let value = Builtin.atomicload_seqcst_RawPointer(target._rawValue)
return COpaquePointer(value)
}
@_transparent
@warn_unused_result
public // @testable
func _stdlib_atomicLoadARCRef(
object target: UnsafeMutablePointer<AnyObject?>
) -> AnyObject? {
let result = _swift_stdlib_atomicLoadPtrImpl(
object: UnsafeMutablePointer(target))
if result != nil {
return Unmanaged<AnyObject>.fromOpaque(result).takeUnretainedValue()
}
return nil
}
% for operation in [ 'Add', 'And', 'Or', 'Xor' ]:
// Warning: no overflow checking.
public func _swift_stdlib_atomicFetch${operation}Int(
object target: UnsafeMutablePointer<Int>,
operand: Int) -> Int {
#if arch(i386) || arch(arm)
return Int(Int32(bitPattern:
_swift_stdlib_atomicFetch${operation}UInt32(
object: UnsafeMutablePointer(target),
operand: UInt32(bitPattern: Int32(operand)))))
#elseif arch(x86_64) || arch(arm64)
return Int(Int64(bitPattern:
_swift_stdlib_atomicFetch${operation}UInt64(
object: UnsafeMutablePointer(target),
operand: UInt64(bitPattern: Int64(operand)))))
#endif
}
% end
public final class _stdlib_AtomicInt {
var _value: Int
var _valuePtr: UnsafeMutablePointer<Int> {
return UnsafeMutablePointer(_getUnsafePointerToStoredProperties(self))
}
public init(_ value: Int = 0) {
_value = value
}
public func store(desired: Int) {
return _swift_stdlib_atomicStoreInt(object: _valuePtr, desired: desired)
}
@warn_unused_result
public func load() -> Int {
return _swift_stdlib_atomicLoadInt(object: _valuePtr)
}
% for operation_name, operation in [ ('Add', '+'), ('And', '&'), ('Or', '|'), ('Xor', '^') ]:
public func fetchAnd${operation_name}(operand: Int) -> Int {
return _swift_stdlib_atomicFetch${operation_name}Int(
object: _valuePtr,
operand: operand)
}
public func ${operation_name.lower()}AndFetch(operand: Int) -> Int {
return fetchAnd${operation_name}(operand) ${operation} operand
}
% end
@warn_unused_result
public func compareExchange(
inout expected expected: Int, desired: Int
) -> Bool {
var expectedVar = expected
let result = _stdlib_atomicCompareExchangeStrongInt(
object: _valuePtr,
expected: &expectedVar,
desired: desired)
expected = expectedVar
return result
}
}
//===----------------------------------------------------------------------===//
// Conversion of primitive types to `String`
//===----------------------------------------------------------------------===//
/// A 32 byte buffer.
struct _Buffer32 {
var x0: UInt64 = 0
var x1: UInt64 = 0
var x2: UInt64 = 0
var x3: UInt64 = 0
}
/// A 72 byte buffer.
struct _Buffer72 {
var x0: UInt64 = 0
var x1: UInt64 = 0
var x2: UInt64 = 0
var x3: UInt64 = 0
var x4: UInt64 = 0
var x5: UInt64 = 0
var x6: UInt64 = 0
var x7: UInt64 = 0
var x8: UInt64 = 0
}
% for bits in [ 32, 64, 80 ]:
% if bits == 80:
#if arch(i386) || arch(x86_64)
% end
@warn_unused_result
@_silgen_name("swift_float${bits}ToString")
func _float${bits}ToStringImpl(
buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
_ bufferLength: UInt, _ value: Float${bits}
) -> UInt
@warn_unused_result
func _float${bits}ToString(value: Float${bits}) -> String {
_sanityCheck(sizeof(_Buffer32.self) == 32)
_sanityCheck(sizeof(_Buffer72.self) == 72)
var buffer = _Buffer32()
return withUnsafeMutablePointer(&buffer) {
(bufferPtr) in
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
let actualLength = _float${bits}ToStringImpl(bufferUTF8Ptr, 32, value)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(
start: bufferUTF8Ptr, count: Int(actualLength)))
}
}
% if bits == 80:
#endif
% end
% end
@warn_unused_result
@_silgen_name("swift_int64ToString")
func _int64ToStringImpl(
buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
_ bufferLength: UInt, _ value: Int64,
_ radix: Int64, _ uppercase: Bool
) -> UInt
@warn_unused_result
func _int64ToString(
value: Int64, radix: Int64 = 10, uppercase: Bool = false
) -> String {
if radix >= 10 {
var buffer = _Buffer32()
return withUnsafeMutablePointer(&buffer) {
(bufferPtr) in
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
let actualLength =
_int64ToStringImpl(bufferUTF8Ptr, 32, value, radix, uppercase)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(
start: bufferUTF8Ptr, count: Int(actualLength)))
}
} else {
var buffer = _Buffer72()
return withUnsafeMutablePointer(&buffer) {
(bufferPtr) in
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
let actualLength =
_int64ToStringImpl(bufferUTF8Ptr, 72, value, radix, uppercase)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(
start: bufferUTF8Ptr, count: Int(actualLength)))
}
}
}
@warn_unused_result
@_silgen_name("swift_uint64ToString")
func _uint64ToStringImpl(
buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
_ bufferLength: UInt, _ value: UInt64, _ radix: Int64, _ uppercase: Bool
) -> UInt
@warn_unused_result
public // @testable
func _uint64ToString(
value: UInt64, radix: Int64 = 10, uppercase: Bool = false
) -> String {
if radix >= 10 {
var buffer = _Buffer32()
return withUnsafeMutablePointer(&buffer) {
(bufferPtr) in
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
let actualLength =
_uint64ToStringImpl(bufferUTF8Ptr, 32, value, radix, uppercase)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(
start: bufferUTF8Ptr, count: Int(actualLength)))
}
} else {
var buffer = _Buffer72()
return withUnsafeMutablePointer(&buffer) {
(bufferPtr) in
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
let actualLength =
_uint64ToStringImpl(bufferUTF8Ptr, 72, value, radix, uppercase)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(
start: bufferUTF8Ptr, count: Int(actualLength)))
}
}
}
@warn_unused_result
func _rawPointerToString(value: Builtin.RawPointer) -> String {
var result = _uint64ToString(
UInt64(unsafeBitCast(value, UInt.self)), radix: 16, uppercase: false)
for _ in 0..<(2 * sizeof(Builtin.RawPointer) - result.utf16.count) {
result = "0" + result
}
return "0x" + result
}
#if _runtime(_ObjC)
// At runtime, these classes are derived from `_SwiftNativeNSXXXBase`,
// which are derived from `NSXXX`.
//
// The @swift_native_objc_runtime_base attribute
// allows us to subclass an Objective-C class and still use the fast Swift
// memory allocator.
@objc @_swift_native_objc_runtime_base(_SwiftNativeNSArrayBase)
class _SwiftNativeNSArray {}
@objc @_swift_native_objc_runtime_base(_SwiftNativeNSDictionaryBase)
class _SwiftNativeNSDictionary {}
@objc @_swift_native_objc_runtime_base(_SwiftNativeNSSetBase)
class _SwiftNativeNSSet {}
@objc @_swift_native_objc_runtime_base(_SwiftNativeNSEnumeratorBase)
class _SwiftNativeNSEnumerator {}
//===----------------------------------------------------------------------===//
// Support for reliable testing of the return-autoreleased optimization
//===----------------------------------------------------------------------===//
@objc internal class _stdlib_ReturnAutoreleasedDummy {
// Use 'dynamic' to force Objective-C dispatch, which uses the
// return-autoreleased call sequence.
@objc dynamic func returnsAutoreleased(x: AnyObject) -> AnyObject {
return x
}
// Use 'dynamic' to prevent this call to be duplicated into other modules.
@objc dynamic func initializeReturnAutoreleased() {
// On x86_64 it is sufficient to perform one cycle of return-autorelesed
// call sequence in order to initialize all required PLT entries.
self.returnsAutoreleased(self)
}
}
/// This function ensures that the return-autoreleased optimization works.
///
/// On some platforms (for example, x86_64), the first call to
/// `objc_autoreleaseReturnValue` will always autorelease because it would fail
/// to verify the instruction sequence in the caller. On x86_64 certain PLT
/// entries would be still pointing to the resolver function, and sniffing
/// the call sequence would fail.
///
/// This code should live in the core stdlib dylib because PLT tables are
/// separate for each dylib.
///
/// Call this function in a fresh autorelease pool.
public func _stdlib_initializeReturnAutoreleased() {
// _stdlib_initializeReturnAutoreleasedImpl()
#if arch(x86_64)
_stdlib_ReturnAutoreleasedDummy().initializeReturnAutoreleased()
#endif
}
#else
class _SwiftNativeNSArray {}
class _SwiftNativeNSDictionary {}
class _SwiftNativeNSSet {}
#endif