Files
swift-mirror/test/embedded/volatile-exec.swift
T
Doug Gregor ae1e3d0865 Build a static library that implements the Embedded Swift platform layer on POSIX
The new library, swiftEmbeddedPlatformPOSIX, implements all of the
_swift_XYZ functions needed to support Embedded Swift as shims on top
of a POSIX system that provides posix_memalign, free, putchar, and so
on. This offers an easier way to bridge between the prior ad hoc
requirements of Embedded Swift and the newer platform abstraction
layer.

Part of rdar://164057124
2026-03-03 09:05:30 -08:00

42 lines
1.4 KiB
Swift

// RUN: %target-run-simple-swift(-parse-as-library -enable-experimental-feature Extern -enable-experimental-feature Embedded -wmo -runtime-compatibility-version none %target-embedded-posix-shim) | %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
}
}