|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Swift |
| 14 | + |
| 15 | +/// A pointer for accessing "volatile" memory, e.g. memory-mapped I/O registers. |
| 16 | +/// |
| 17 | +/// Do not use for inter-thread synchronization. This is only meaningful for |
| 18 | +/// low-level operations on special memory addresses performed from OS kernels, |
| 19 | +/// embedded firmware, and similar environments. |
| 20 | +/// |
| 21 | +/// The semantics of volatile load and volatile store operations match the LLVM |
| 22 | +/// volatile semantics. Notably, a volatile operation cannot be added, removed, |
| 23 | +/// or reordered with other volatile operations by the compiler. They may be |
| 24 | +/// reordered with non-volatile operations. For details, see |
| 25 | +/// <https://llvm.org/docs/LangRef.html#volatile-memory-accesses>. |
| 26 | +@frozen |
| 27 | +public struct VolatileMappedRegister<Pointee> { |
| 28 | + @usableFromInline |
| 29 | + let _rawPointer: Builtin.RawPointer |
| 30 | + |
| 31 | + @_transparent |
| 32 | + public init(unsafeBitPattern: UInt) { |
| 33 | + self._rawPointer = Builtin.inttoptr_Word(unsafeBitPattern._builtinWordValue) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +extension VolatileMappedRegister where Pointee == UInt8 { |
| 38 | + /// Perform an 8-bit volatile load operation from the target pointer. |
| 39 | + /// |
| 40 | + /// Do not use for inter-thread synchronization. |
| 41 | + @_transparent |
| 42 | + public func load() -> Pointee { |
| 43 | + UInt8(Builtin.atomicload_monotonic_volatile_Int8(_rawPointer)) |
| 44 | + } |
| 45 | + |
| 46 | + /// Perform an 8-bit volatile store operation on the target pointer. |
| 47 | + /// |
| 48 | + /// Do not use for inter-thread synchronization. |
| 49 | + @_transparent |
| 50 | + public func store(_ value: Pointee) { |
| 51 | + Builtin.atomicstore_monotonic_volatile_Int8(_rawPointer, value._value) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +extension VolatileMappedRegister where Pointee == UInt16 { |
| 56 | + /// Perform a 16-bit volatile load operation from the target pointer. |
| 57 | + /// |
| 58 | + /// Do not use for inter-thread synchronization. |
| 59 | + @_transparent |
| 60 | + public func load() -> Pointee { |
| 61 | + UInt16(Builtin.atomicload_monotonic_volatile_Int16(_rawPointer)) |
| 62 | + } |
| 63 | + |
| 64 | + /// Perform a 16-bit volatile store operation on the target pointer. |
| 65 | + /// |
| 66 | + /// Do not use for inter-thread synchronization. |
| 67 | + @_transparent |
| 68 | + public func store(_ value: Pointee) { |
| 69 | + Builtin.atomicstore_monotonic_volatile_Int16(_rawPointer, value._value) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +extension VolatileMappedRegister where Pointee == UInt32 { |
| 74 | + /// Perform a 32-bit volatile load operation from the target pointer. |
| 75 | + /// |
| 76 | + /// Do not use for inter-thread synchronization. |
| 77 | + @_transparent |
| 78 | + public func load() -> Pointee { |
| 79 | + UInt32(Builtin.atomicload_monotonic_volatile_Int32(_rawPointer)) |
| 80 | + } |
| 81 | + |
| 82 | + /// Perform a 32-bit volatile store operation on the target pointer. |
| 83 | + /// |
| 84 | + /// Do not use for inter-thread synchronization. |
| 85 | + @_transparent |
| 86 | + public func store(_ value: Pointee) { |
| 87 | + Builtin.atomicstore_monotonic_volatile_Int32(_rawPointer, value._value) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +extension VolatileMappedRegister where Pointee == UInt64 { |
| 92 | + /// Perform a 64-bit volatile load operation from the target pointer. |
| 93 | + /// |
| 94 | + /// Do not use for inter-thread synchronization. |
| 95 | + @_transparent |
| 96 | + public func load() -> Pointee { |
| 97 | + UInt64(Builtin.atomicload_monotonic_volatile_Int64(_rawPointer)) |
| 98 | + } |
| 99 | + |
| 100 | + /// Perform a 64-bit volatile store operation on the target pointer. |
| 101 | + /// |
| 102 | + /// Do not use for inter-thread synchronization. |
| 103 | + @_transparent |
| 104 | + public func store(_ value: Pointee) { |
| 105 | + Builtin.atomicstore_monotonic_volatile_Int64(_rawPointer, value._value) |
| 106 | + } |
| 107 | +} |
0 commit comments