Swift 6 Transforms Low-Level Programming with Embedded Swift
Swift 6 brings a new compilation mode aiming to address the specific constraints of embedded devices as well as kernel- and other low-level code. Embedded Swift is a full-featured subset of Swift covering most of the language, including value and reference types, closures, optionals, error handling, generics and more.
Embedded Swift is a compilation model that’s analogous to a traditional C compiler in the sense that the compiler produces an object file (.o) that can be simply linked with your existing code, and it’s not going to require you to port any libraries or runtimes.
Embedded Swift disables language features that need runtime support like reflection and any
types. This makes it possible to run Swift programs without distributing a runtime, which is instead required for macOS and iOS apps. Embedded Swift uses compiler techniques such as full generics specialization, and static linking to produce binaries suitable for running on embedded devices.
Specifically, the Mirror
API, values of protocol types, Any
and AnyObject
, metatypes (let t = SomeClass.Type or type(of: value)
), and stringification of arbitrary types (achieved using Reflection) are not supported. Support for Swift Concurrency is not supported either, but it is under active development.
According to Apple, these limitations to the language do not reduce its expressiveness and power:
Despite turning off some language features, the Embedded Swift subset feels very close to “full” Swift, and makes it easy to continue writing idiomatic, easy-to-read Swift code.
According to Apple, Embedded Swift makes it possible to create games with a binary size of only a few Kbytes that can run on a small console like the Playdate. Likewise, Embedded Swift can target a wide variety of ARM and RISC-V microcontrollers, popular for building industrial applications.
As a side note, Apple itself is using Embedded Swift for a critical component of its hardware:
The Apple Secure Enclave Processor uses Embedded Swift. The Secure Enclave is an isolated subsystem separate from the main processor, dedicated to keeping sensitive data secure.
To build Swift code in Embedded Swift mode, you give the compiler a target triple, the -enable-experimental-feature Embedded
flag, and the set of source files:
The example above ignores the fact that you will usually be linking a C or C++ SDK library for the specific hardware you are targeting and that is provided by the device vendor.
You can use Embedded Swift to build a macOS CLI program, as well:
Embedded Swift is currently experimental and subject to change and the best way to use it is with a preview toolchain. Currently, ARM and RISC-V chips of both 32-bit and 64-bit variants are supported, but new instruction sets will be added in the future, says Apple.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.