-
Notifications
You must be signed in to change notification settings - Fork 66
Major expansion of documentation for Embedded Swift #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Documentation for Embedded Swift | ||
================================ | ||
|
||
Documentation for Embedded Swift can be found at these locations: | ||
- [in rendered form](https://swiftpackageindex.com/apple/swift-embedded-examples/documentation/embeddedswift) | ||
- [DocC source code](/Sources/EmbeddedSwift/Documentation.docc) |
8 changes: 2 additions & 6 deletions
8
...Documentation.docc/LanguageDetails/ABI.md → ...Documentation.docc/CompilerDetails/ABI.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 2 additions & 6 deletions
8
.../Documentation.docc/Development/Status.md → ...umentation.docc/CompilerDetails/Status.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 23 additions & 181 deletions
204
Sources/EmbeddedSwift/Documentation.docc/Documentation.md
Large diffs are not rendered by default.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
Sources/EmbeddedSwift/Documentation.docc/GettingStarted/InstallEmbeddedSwift.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Sources/EmbeddedSwift/Documentation.docc/GettingStarted/Introduction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Introduction to Embedded Swift | ||
|
||
Write Swift code for microcontrollers, embedded systems, and bare-metal applications | ||
|
||
## Overview | ||
|
||
Embedded Swift is an experimental and rapidly developing feature of the Swift language that enables development of baremetal, embedded and standalone software. It's a subset of the Swift language designed for producing small, efficient binaries with minimal dependencies, making it suitable for resource-constrained environments. | ||
|
||
> Warning: Embedded Swift is experimental. Use the latest downloadable 'Trunk Development' snapshot from swift.org to use Embedded Swift. Public releases of Swift do not yet support Embedded Swift. | ||
|
||
## How does Embedded Swift differ from regular Swift? | ||
|
||
Regular Swift is not a good fit for small constrained environments like microcontrollers, mainly due to codesize and memory footprint. Regular Swift typically requires at least a few megabytes of code and data to support dynamic language features like reflection, and separately compiled generics with ABI stability. Embedded Swift on the other hand can be deployed to environments with as little as kilobytes of available memory. This is achieved by: | ||
|
||
- Eliminating runtime type metadata where possible | ||
- Removing reflection capabilities | ||
- Using compile-time specialization (monomorphization) for generic code | ||
- Minimizing dependencies on external libraries | ||
|
||
This results in properties that are a great fit for embedded software development: | ||
|
||
- **Small binaries** that can be as tiny as a few hundred bytes for "Hello World"-like programs (fully self-contained). | ||
- **No hidden runtime costs** – Embedded Swift's runtime library does not manage any data structures behind your back, is itself less than a kilobyte in size, and it eligible to be removed if unused. | ||
- **Full C/C++ interoperability** to directly interact with existing C libraries and hardware-specific code, making it easy to integrate with vendor SDKs. | ||
- **Modern language features** like optionals, generics, and strong type safety are all available in Embedded Swift. | ||
- **Full safety of Swift** is retained in Embedded Swift. | ||
|
||
For a detailed introduction and motivation into Embedded Swift, please see "[A Vision for Embedded Swift](https://github.com/swiftlang/swift-evolution/blob/main/visions/embedded-swift.md)", a Swift Evolution document highlighting the main goals and approaches. Note that this is a historical document and does not capture latest development and further evolution. For an up-to-date in-depth breakdown of the language features of Embedded Swift, please see <doc:LanguageSubset>. | ||
|
||
## What Embedded Swift is and isn't | ||
|
||
- Embedded Swift **is** a way to produce small and freestanding binaries (with no, or trivial dependencies). | ||
- Embedded Swift **is not** a complete one-click solution to program all embedded boards and MCUs. | ||
- 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 **is not** a HAL, it's not an SDK for development, it's not a set of libraries to program peripherals using high-level APIs. It's instead a compilation mode that's suitable for creating these components. | ||
|
||
## Platform support | ||
|
||
The Swift toolchain has the ability to produce code for almost any standard ARM and RISC-V platform, and that makes Embedded Swift versatile and not limited to specific platforms or hardware devices. This way, Embedded Swift can potentially target many different microcontroller families and embedded devices. | ||
|
||
Boards with active community support include the Raspberry Pi Pico, various STM32 development boards, and several ESP32 variants, with more platforms being regularly added as the community grows. | ||
|
||
## Interoperability with existing SDKs | ||
|
||
Software projects using Embedded Swift are typically developed in one of the following ways: | ||
|
||
1. Integrating with an existing SDK (typically in C, or C++) that provides either an embedded OS, or OS-like facilities, hardware drivers, and overall functionality that's needed for embedded software. | ||
|
||
2. Writing fully "bare-metal" code, without any pre-existing setup or SDK. This is typically done for extremely constrained environments and/or when full control of every piece of code is needed. | ||
|
||
Both the approaches are readily available in Embedded Swift, and the choice of which approach to use depends on your specific project requirements, hardware constraints, and development preferences. For integrating with existing SDKs, Swift's C/C++ interoperability makes it straightforward to call native SDK functions, while the bare-metal approach gives you complete control over every aspect of your code's execution environment. | ||
|
||
## Getting Started | ||
|
||
To start using Embedded Swift, please see the <doc:InstallEmbeddedSwift> page for installation instructions. | ||
|
||
Once you've set up the toolchain, we recommend exploring the <doc:WaysToGetStarted> page which provides various paths for getting started, including the <doc:macOSGuide> to try Embedded Swift on your development machine, and more advanced guides such as <doc:PicoGuide> for programming an actual embedded device. | ||
|
||
For details about using Embedded Swift, consult the <doc:Basics> documentation, which explains how to build code with Embedded Swift and shows integration patterns with embedded SDKs and build systems. |
29 changes: 29 additions & 0 deletions
29
Sources/EmbeddedSwift/Documentation.docc/GettingStarted/LanguageSubset.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Language subset | ||
|
||
Details of the Embedded Swift language subset compared to full Swift | ||
|
||
Embedded Swift is a subset of the Swift language, and some features are not available in Embedded Swift. This is neccessary in order to achieve small binaries with effective dead-code elimination and minimized system dependencies. | ||
|
||
That said, *the vast majority* of the Swift language works exactly the same in Embedded Swift. This includes cenerics, protocols, enums with associated values, tuples, optionals, classes (instances are allocated on the heap and refcounted just like in regular Swift), inheritance, runtime polymorphism, arrays (heap-allocated copy-on-write just like in regular Swift) and much more. | ||
|
||
Note that there are no behavior changes in Embedded Swift compared to full Swift, and Embedded Swift is strictly a *subset* and not a *dialect*. Any code compatible with Embedded Swift will also compile and have the same semantics in full Swift. | ||
|
||
## Code-level features that are not available | ||
|
||
- **Not available**: Runtime reflection (`Mirror` APIs). | ||
- **Not available**: Values of protocol types ("existentials"), unless the protocol is restricted to be class-bound (derived from AnyObject). E.g. `let a: Hashable = ...` is not allowed. `Any` is also not allowed. See <doc:Existentials> for details and alternatives of existentials. | ||
- **Not available**: Throwing errors or `any Error` type (in contrast with "typed throws", which *is* supported in Embedded Swift). | ||
- **Not available**: Metatypes, e.g. `let t = SomeClass.Type` or `type(of: value)` are not allowed. | ||
- **Not available**: Printing and stringification of arbitrary types (which is achieved via reflection in desktop Swift). | ||
- **Not available**: Using non-fimal generic class methods. See <doc:NonFinalGenericMethods> for details on this. | ||
- **Not availble**: Weak and unowned references. | ||
|
||
## Compilation facilities that are not available | ||
|
||
- **Not available**: Library Evolution (stable ABI), and facilities that requires Library Evolution (e.g. internal module imports) | ||
- **Not available**: Objective-C interoperability | ||
- **Not available**: Builds without WMO (whole module optimization) | ||
|
||
## Further resources | ||
|
||
The above lists are describing features that are removed from Embedded Swift *by design*. Since Embedded Swift is currently an experimental preview, there might also be features that are not yet implemented. See the in-development status at [Embedded Swift -- Status](EmbeddedSwiftStatus.md). |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.