包阅导读总结
1. 关键词:Swift 6、Embedded Swift、低级别编程、编译模式、受限特性
2. 总结:Swift 6 引入 Embedded Swift 用于低级别编程,它是 Swift 的一个子集,能与现有代码简单链接,禁用了部分需运行时支持的特性。虽有限制,但不影响表达力,可用于创建小型游戏及工业应用,目前处于实验阶段。
3. 主要内容:
– Swift 6 引入 Embedded Swift
– 针对嵌入式设备、内核和其他低级别代码的特定约束
– 是一个全功能子集,涵盖大部分语言特性
– Embedded Swift 编译模型
– 类似传统 C 编译器,产生可链接的对象文件
– 无需移植库或运行时
– 禁用的语言特性
– 如反射、某些类型、Swift 并发等
– 应用场景
– 创造小体积游戏
– 针对多种微控制器
– 苹果硬件的安全隔离处理器使用
– 编译方式
– 给出目标三元组、特定标志和源文件
– 目前状态
– 实验阶段,可能变化,支持特定芯片,未来会增加新指令集
思维导图:
文章来源:infoq.com
作者:Sergio De Simone
发布时间:2024/7/20 0:00
语言:英文
总字数:469字
预计阅读时间:2分钟
评分:87分
标签:Swift 6,嵌入式 Swift,低级编程,系统编程,嵌入式设备
以下为原文内容
本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com
Swift 6 brings a new compilation mode that aimsto address the specific constraints of embedded devices as well as kerneland 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.