-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathvolatile-exec.swift
41 lines (36 loc) · 1.33 KB
/
volatile-exec.swift
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
// RUN: %target-run-simple-swift(-parse-as-library -enable-experimental-feature Extern -enable-experimental-feature Embedded -wmo -runtime-compatibility-version none) | %FileCheck %s
// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: optimized_stdlib
// REQUIRES: volatile
// REQUIRES: swift_feature_Embedded
// REQUIRES: swift_feature_Extern
import _Volatile
@_extern(c, "putchar")
@discardableResult
func putchar(_: CInt) -> CInt
@main
struct Main {
static func main() {
var byte: UInt8 = 42
withUnsafePointer(to: &byte) { p in
let volatilePointer = VolatileMappedRegister<UInt8>(unsafeBitPattern: UInt(bitPattern: p))
print("byte = ", terminator: "")
print(volatilePointer.load())
// CHECK: byte = 42
volatilePointer.store(77)
}
print("byte = ", terminator: "")
print(byte)
// CHECK: byte = 77
var heapPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16) // uninitialized content
for i in 0 ..< 16 {
let pointerWithOffset = heapPointer.advanced(by: i)
let volatilePointer = VolatileMappedRegister<UInt8>(unsafeBitPattern: UInt(bitPattern: pointerWithOffset))
volatilePointer.store(UInt8(0x61 + i))
}
for i in 0 ..< 16 { putchar(CInt(heapPointer[i])) }
putchar(CInt(("\n" as Unicode.Scalar).value))
// CHECK: abcdefghijklmnop
}
}