Skip to content

Commit 3f5c716

Browse files
committed
de-gyb runtime
1 parent 73250ad commit 3f5c716

File tree

2 files changed

+209
-34
lines changed

2 files changed

+209
-34
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ set(SWIFTLIB_ESSENTIAL
117117
REPL.swift
118118
Result.swift
119119
Reverse.swift
120+
Runtime.swift
120121
RuntimeFunctionCounters.swift
121122
SipHash.swift
122123
Sequence.swift
@@ -192,7 +193,6 @@ set(SWIFTLIB_ESSENTIAL_GYB_SOURCES
192193
FloatingPointParsing.swift.gyb
193194
FloatingPointTypes.swift.gyb
194195
IntegerTypes.swift.gyb
195-
Runtime.swift.gyb
196196
UnavailableStringAPIs.swift.gyb
197197
UnsafeBufferPointer.swift.gyb
198198
UnsafeRawBufferPointer.swift.gyb

stdlib/public/core/Runtime.swift.gyb renamed to stdlib/public/core/Runtime.swift

Lines changed: 208 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -25,8 +25,8 @@ public // @testable
2525
func _stdlib_atomicCompareExchangeStrongPtr(
2626
object target: UnsafeMutablePointer<UnsafeRawPointer?>,
2727
expected: UnsafeMutablePointer<UnsafeRawPointer?>,
28-
desired: UnsafeRawPointer?) -> Bool {
29-
28+
desired: UnsafeRawPointer?
29+
) -> Bool {
3030
// We use Builtin.Word here because Builtin.RawPointer can't be nil.
3131
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Word(
3232
target._rawValue,
@@ -36,7 +36,6 @@ func _stdlib_atomicCompareExchangeStrongPtr(
3636
return Bool(won)
3737
}
3838

39-
% for optional in ['', '?']:
4039
/// Atomic compare and exchange of `UnsafeMutablePointer<T>` with sequentially
4140
/// consistent memory ordering. Precise semantics are defined in C++11 or C11.
4241
///
@@ -67,9 +66,53 @@ func _stdlib_atomicCompareExchangeStrongPtr(
6766
@_transparent
6867
public // @testable
6968
func _stdlib_atomicCompareExchangeStrongPtr<T>(
70-
object target: UnsafeMutablePointer<UnsafeMutablePointer<T>${optional}>,
71-
expected: UnsafeMutablePointer<UnsafeMutablePointer<T>${optional}>,
72-
desired: UnsafeMutablePointer<T>${optional}
69+
object target: UnsafeMutablePointer<UnsafeMutablePointer<T>>,
70+
expected: UnsafeMutablePointer<UnsafeMutablePointer<T>>,
71+
desired: UnsafeMutablePointer<T>
72+
) -> Bool {
73+
let rawTarget = UnsafeMutableRawPointer(target).assumingMemoryBound(
74+
to: Optional<UnsafeRawPointer>.self)
75+
let rawExpected = UnsafeMutableRawPointer(expected).assumingMemoryBound(
76+
to: Optional<UnsafeRawPointer>.self)
77+
return _stdlib_atomicCompareExchangeStrongPtr(
78+
object: rawTarget,
79+
expected: rawExpected,
80+
desired: UnsafeRawPointer(desired))
81+
}
82+
83+
/// Atomic compare and exchange of `UnsafeMutablePointer<T>` with sequentially
84+
/// consistent memory ordering. Precise semantics are defined in C++11 or C11.
85+
///
86+
/// - Warning: This operation is extremely tricky to use correctly because of
87+
/// writeback semantics.
88+
///
89+
/// It is best to use it directly on an
90+
/// `UnsafeMutablePointer<UnsafeMutablePointer<T>>` that is known to point
91+
/// directly to the memory where the value is stored.
92+
///
93+
/// In a call like this:
94+
///
95+
/// _stdlib_atomicCompareExchangeStrongPtr(&foo.property1.property2, ...)
96+
///
97+
/// you need to manually make sure that:
98+
///
99+
/// - all properties in the chain are physical (to make sure that no writeback
100+
/// happens; the compare-and-exchange instruction should operate on the
101+
/// shared memory); and
102+
///
103+
/// - the shared memory that you are accessing is located inside a heap
104+
/// allocation (a class instance property, a `_BridgingBuffer`, a pointer to
105+
/// an `Array` element etc.)
106+
///
107+
/// If the conditions above are not met, the code will still compile, but the
108+
/// compare-and-exchange instruction will operate on the writeback buffer, and
109+
/// you will get a *race* while doing writeback into shared memory.
110+
@_transparent
111+
public // @testable
112+
func _stdlib_atomicCompareExchangeStrongPtr<T>(
113+
object target: UnsafeMutablePointer<UnsafeMutablePointer<T>?>,
114+
expected: UnsafeMutablePointer<UnsafeMutablePointer<T>?>,
115+
desired: UnsafeMutablePointer<T>?
73116
) -> Bool {
74117
let rawTarget = UnsafeMutableRawPointer(target).assumingMemoryBound(
75118
to: Optional<UnsafeRawPointer>.self)
@@ -80,14 +123,14 @@ func _stdlib_atomicCompareExchangeStrongPtr<T>(
80123
expected: rawExpected,
81124
desired: UnsafeRawPointer(desired))
82125
}
83-
% end # optional
84126

85127
@_transparent
86128
@discardableResult
87129
public // @testable
88130
func _stdlib_atomicInitializeARCRef(
89131
object target: UnsafeMutablePointer<AnyObject?>,
90-
desired: AnyObject) -> Bool {
132+
desired: AnyObject
133+
) -> Bool {
91134
var expected: UnsafeRawPointer?
92135
let desiredPtr = Unmanaged.passRetained(desired).toOpaque()
93136
let rawTarget = UnsafeMutableRawPointer(target).assumingMemoryBound(
@@ -121,9 +164,39 @@ func _stdlib_atomicLoadARCRef(
121164
/// A 32 byte buffer.
122165
internal struct _Buffer32 {
123166
internal init() {}
124-
% for i in range(32):
125-
internal var _x${i}: UInt8 = 0
126-
% end
167+
168+
internal var _x0: UInt8 = 0
169+
internal var _x1: UInt8 = 0
170+
internal var _x2: UInt8 = 0
171+
internal var _x3: UInt8 = 0
172+
internal var _x4: UInt8 = 0
173+
internal var _x5: UInt8 = 0
174+
internal var _x6: UInt8 = 0
175+
internal var _x7: UInt8 = 0
176+
internal var _x8: UInt8 = 0
177+
internal var _x9: UInt8 = 0
178+
internal var _x10: UInt8 = 0
179+
internal var _x11: UInt8 = 0
180+
internal var _x12: UInt8 = 0
181+
internal var _x13: UInt8 = 0
182+
internal var _x14: UInt8 = 0
183+
internal var _x15: UInt8 = 0
184+
internal var _x16: UInt8 = 0
185+
internal var _x17: UInt8 = 0
186+
internal var _x18: UInt8 = 0
187+
internal var _x19: UInt8 = 0
188+
internal var _x20: UInt8 = 0
189+
internal var _x21: UInt8 = 0
190+
internal var _x22: UInt8 = 0
191+
internal var _x23: UInt8 = 0
192+
internal var _x24: UInt8 = 0
193+
internal var _x25: UInt8 = 0
194+
internal var _x26: UInt8 = 0
195+
internal var _x27: UInt8 = 0
196+
internal var _x28: UInt8 = 0
197+
internal var _x29: UInt8 = 0
198+
internal var _x30: UInt8 = 0
199+
internal var _x31: UInt8 = 0
127200

128201
internal mutating func withBytes<Result>(
129202
_ body: (UnsafeMutablePointer<UInt8>) throws -> Result
@@ -137,9 +210,79 @@ internal struct _Buffer32 {
137210
/// A 72 byte buffer.
138211
internal struct _Buffer72 {
139212
internal init() {}
140-
% for i in range(72):
141-
internal var _x${i}: UInt8 = 0
142-
% end
213+
214+
internal var _x0: UInt8 = 0
215+
internal var _x1: UInt8 = 0
216+
internal var _x2: UInt8 = 0
217+
internal var _x3: UInt8 = 0
218+
internal var _x4: UInt8 = 0
219+
internal var _x5: UInt8 = 0
220+
internal var _x6: UInt8 = 0
221+
internal var _x7: UInt8 = 0
222+
internal var _x8: UInt8 = 0
223+
internal var _x9: UInt8 = 0
224+
internal var _x10: UInt8 = 0
225+
internal var _x11: UInt8 = 0
226+
internal var _x12: UInt8 = 0
227+
internal var _x13: UInt8 = 0
228+
internal var _x14: UInt8 = 0
229+
internal var _x15: UInt8 = 0
230+
internal var _x16: UInt8 = 0
231+
internal var _x17: UInt8 = 0
232+
internal var _x18: UInt8 = 0
233+
internal var _x19: UInt8 = 0
234+
internal var _x20: UInt8 = 0
235+
internal var _x21: UInt8 = 0
236+
internal var _x22: UInt8 = 0
237+
internal var _x23: UInt8 = 0
238+
internal var _x24: UInt8 = 0
239+
internal var _x25: UInt8 = 0
240+
internal var _x26: UInt8 = 0
241+
internal var _x27: UInt8 = 0
242+
internal var _x28: UInt8 = 0
243+
internal var _x29: UInt8 = 0
244+
internal var _x30: UInt8 = 0
245+
internal var _x31: UInt8 = 0
246+
internal var _x32: UInt8 = 0
247+
internal var _x33: UInt8 = 0
248+
internal var _x34: UInt8 = 0
249+
internal var _x35: UInt8 = 0
250+
internal var _x36: UInt8 = 0
251+
internal var _x37: UInt8 = 0
252+
internal var _x38: UInt8 = 0
253+
internal var _x39: UInt8 = 0
254+
internal var _x40: UInt8 = 0
255+
internal var _x41: UInt8 = 0
256+
internal var _x42: UInt8 = 0
257+
internal var _x43: UInt8 = 0
258+
internal var _x44: UInt8 = 0
259+
internal var _x45: UInt8 = 0
260+
internal var _x46: UInt8 = 0
261+
internal var _x47: UInt8 = 0
262+
internal var _x48: UInt8 = 0
263+
internal var _x49: UInt8 = 0
264+
internal var _x50: UInt8 = 0
265+
internal var _x51: UInt8 = 0
266+
internal var _x52: UInt8 = 0
267+
internal var _x53: UInt8 = 0
268+
internal var _x54: UInt8 = 0
269+
internal var _x55: UInt8 = 0
270+
internal var _x56: UInt8 = 0
271+
internal var _x57: UInt8 = 0
272+
internal var _x58: UInt8 = 0
273+
internal var _x59: UInt8 = 0
274+
internal var _x60: UInt8 = 0
275+
internal var _x61: UInt8 = 0
276+
internal var _x62: UInt8 = 0
277+
internal var _x63: UInt8 = 0
278+
internal var _x64: UInt8 = 0
279+
internal var _x65: UInt8 = 0
280+
internal var _x66: UInt8 = 0
281+
internal var _x67: UInt8 = 0
282+
internal var _x68: UInt8 = 0
283+
internal var _x69: UInt8 = 0
284+
internal var _x70: UInt8 = 0
285+
internal var _x71: UInt8 = 0
143286

144287
internal mutating func withBytes<Result>(
145288
_ body: (UnsafeMutablePointer<UInt8>) throws -> Result
@@ -150,39 +293,75 @@ internal struct _Buffer72 {
150293
}
151294
}
152295

153-
% for bits in [ 32, 64, 80 ]:
296+
// Returns a UInt64, but that value is the length of the string, so it's
297+
// guaranteed to fit into an Int. This is part of the ABI, so we can't
298+
// trivially change it to Int. Callers can safely convert the result
299+
// to any integer type without checks, however.
300+
@_silgen_name("swift_float32ToString")
301+
internal func _float32ToStringImpl(
302+
_ buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
303+
_ bufferLength: UInt, _ value: Float32,
304+
_ debug: Bool
305+
) -> UInt64
154306

155-
% if bits == 80:
156-
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
157-
% end
307+
internal func _float32ToString(
308+
_ value: Float32, debug: Bool
309+
) -> (buffer: _Buffer32, length: Int) {
310+
_internalInvariant(MemoryLayout<_Buffer32>.size == 32)
311+
var buffer = _Buffer32()
312+
let length = buffer.withBytes { (bufferPtr) in Int(
313+
truncatingIfNeeded: _float32ToStringImpl(bufferPtr, 32, value, debug)
314+
)}
315+
return (buffer, length)
316+
}
158317

159318
// Returns a UInt64, but that value is the length of the string, so it's
160319
// guaranteed to fit into an Int. This is part of the ABI, so we can't
161320
// trivially change it to Int. Callers can safely convert the result
162321
// to any integer type without checks, however.
163-
@_silgen_name("swift_float${bits}ToString")
164-
internal func _float${bits}ToStringImpl(
322+
@_silgen_name("swift_float64ToString")
323+
internal func _float64ToStringImpl(
165324
_ buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
166-
_ bufferLength: UInt, _ value: Float${bits},
325+
_ bufferLength: UInt, _ value: Float64,
167326
_ debug: Bool
168327
) -> UInt64
169328

170-
internal func _float${bits}ToString(
171-
_ value: Float${bits}, debug: Bool
329+
internal func _float64ToString(
330+
_ value: Float64, debug: Bool
172331
) -> (buffer: _Buffer32, length: Int) {
173332
_internalInvariant(MemoryLayout<_Buffer32>.size == 32)
174333
var buffer = _Buffer32()
175334
let length = buffer.withBytes { (bufferPtr) in Int(
176-
truncatingIfNeeded: _float${bits}ToStringImpl(bufferPtr, 32, value, debug)
335+
truncatingIfNeeded: _float64ToStringImpl(bufferPtr, 32, value, debug)
177336
)}
178337
return (buffer, length)
179338
}
180339

181-
% if bits == 80:
182-
#endif
183-
% end
184340

185-
% end
341+
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
342+
343+
// Returns a UInt64, but that value is the length of the string, so it's
344+
// guaranteed to fit into an Int. This is part of the ABI, so we can't
345+
// trivially change it to Int. Callers can safely convert the result
346+
// to any integer type without checks, however.
347+
@_silgen_name("swift_float80ToString")
348+
internal func _float80ToStringImpl(
349+
_ buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
350+
_ bufferLength: UInt, _ value: Float80,
351+
_ debug: Bool
352+
) -> UInt64
353+
354+
internal func _float80ToString(
355+
_ value: Float80, debug: Bool
356+
) -> (buffer: _Buffer32, length: Int) {
357+
_internalInvariant(MemoryLayout<_Buffer32>.size == 32)
358+
var buffer = _Buffer32()
359+
let length = buffer.withBytes { (bufferPtr) in Int(
360+
truncatingIfNeeded: _float80ToStringImpl(bufferPtr, 32, value, debug)
361+
)}
362+
return (buffer, length)
363+
}
364+
#endif
186365

187366
// Returns a UInt64, but that value is the length of the string, so it's
188367
// guaranteed to fit into an Int. This is part of the ABI, so we can't
@@ -394,7 +573,3 @@ internal class __SwiftNativeNSSet {
394573
}
395574

396575
#endif
397-
398-
// ${'Local Variables'}:
399-
// eval: (read-only-mode 1)
400-
// End:

0 commit comments

Comments
 (0)