Swift 6 Introduces Embedded Swift for Low-level Programming

0
Swift 6 Introduces Embedded Swift for Low-level Programming

Swift 6 brings a new compilation mode that aims 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, which are 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 keep 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:


$ swiftc -target armv6m-none-none-eabi -enable-experimental-feature Embedded \ 
  -wmo  input1.swift input2.swift ... -c -o output.o

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:


xcrun swiftc hello.swift -enable-experimental-feature Embedded -wmo

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.


link

Leave a Reply

Your email address will not be published. Required fields are marked *