[フレーム]
BT

InfoQ Software Architects' Newsletter

A monthly overview of things you need to know as an architect or aspiring architect.

View an example

We protect your privacy.

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Unlock the full InfoQ experience

Unlock the full InfoQ experience by logging in! Stay updated with your favorite authors and topics, engage with content, and download exclusive resources.

Log In
or

Don't have an InfoQ account?

Register
  • Stay updated on topics and peers that matter to youReceive instant alerts on the latest insights and trends.
  • Quickly access free resources for continuous learningMinibooks, videos with transcripts, and training materials.
  • Save articles and read at anytimeBookmark articles to read whenever youre ready.

Topics

Choose your language

InfoQ Homepage News Swift 6 Introduces Embedded Swift for Low-level Programming

Swift 6 Introduces Embedded Swift for Low-level Programming

This item in japanese

Jul 20, 2024 2 min read

Write for InfoQ

Feed your curiosity. Help 550k+ global
senior developers
each month stay ahead.
Get in touch

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.

About the Author

Sergio De Simone

Show moreShow less

Rate this Article

Adoption
Style

Related Content

The InfoQ Newsletter

A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.

BT

AltStyle によって変換されたページ (->オリジナル) /