Using swift compiler for bare metal?
Asked Answered
L

3

8

I would really like to use swift for embedded programming as I feel like its a much better replacement for c++, The processor I'm using is an ARM Cortex-M4F(http://www.ti.com/tool/ek-tm4c123gxl). Looking at the swift compiler page, it says you can generate LLVM IR from swift source and then I was hoping to cross compile with LLVM. Would this be possible?

Luisaluise answered 19/12, 2015 at 18:12 Comment(0)
M
3

It definitely is possible to generate machine code with Swift. In fact, by default when you compile a Swift program in Xcode or with the swiftc command-line compiler, the executable file produced is composed of machine-code.

The LLVM bytecode is generated at some point during the build process, but the final executable that's produced is machine-code. There are compiler options that allow you to produce only LLVM bytcode if you want, but LLVM bytecode isn't usually executed directly, like Java bytecode with is run by the Java runtime.

As far as cross-compiling for ARM, I'm not sure how it works with the swiftc tool, but if you build an Xcode iOS project, it produces an ARM executable. I'm sure the swiftc complier has all the options you need to produce ARM executables.

However, the one catch I can think of, is that lots of Swift's functionality depends on Apple's frameworks. However, now that Swift has been open-sourced, there gradually be more pure swift libraries for all kinds of things.

Mcgruder answered 19/12, 2015 at 19:57 Comment(8)
most of the dependency with apple frameworks are gone, Foundation has been reimplemented in swift and is open sourced as well.Luisaluise
Swift requires the Objective-C runtime, which is fairly large in the embedded context (at least 600KB according to this person who got Objective-C working on a SiLabs Cortex-M4). If you've got external memory to burn then that's less of an issue.Poet
@TonyK www.swift.org , swift no longer depends on the objective c. it has been open sourcedLuisaluise
@jacksexton I did not know that, very interesting. It still has a runtime though according to the Swift.org site, but I wonder how tightly coupled the language is to the runtime.Poet
@TonyK I can't believe I didn't catch that, I'm no expert in this area but do you know if its possible to compile and run swift and just avoid using those features. The runtime seems pretty light.Luisaluise
@jacksexton I'm not familiar with Swift, but for example in the case of C++ it is possible to disable all the "dynamic" features like runtime polymorphism and use only the core language. Depends on the architecture of the Swift language and compiler, the same may be possible.Poet
For a language based on automatic memory management, you're clearly going to need some sort of runtime to take care of that at the very least. Code to do anything useful on a microcontroller (i.e. touch hardware) is going to need writing in a language with real pointers, but that could probably be wrapped up into a reusable framework with Swift bindings - not sure how you'd go about your interrupt handlers without compiler support, though. For "application-level" code written to some abstracted API (in the vein of Arduino or mbed), rather than pure bare-metal, it seems more reasonable.Chemistry
Swift uses ARC for memory management, which as I understand it, is not like runtime garbage collection, but rather the code for releasing objects is added to the executable at compile-time. See apple.co/2iTQfVI. (Article discusses Obj-C, but I think ARC works the same in Swift.) But, it's true that Swift has a runtime that handles important features, and I wasn't really considering that when I wrote this answer. I also wasn't considering the issues that arise when writing programs without an OS, because it's not a topic I know about. In retrospect, it's not a great answer.Mcgruder
M
1

I was exploring this possibility (using Swift for embedded applications). Since Swift needs a runtime, after static compilation of "Hello, World!" (on Ubuntu, x86-64 using latest Swift 3.0.2) the result binary size was over 5 Megabytes, which might be an issue on "small" ARM controllers (like Cortex-M0).

Mixture answered 2/1, 2017 at 15:58 Comment(0)
V
0

I am also interested in this topic - running a pure swift program on bare metal since swift language has so many modern language features. Recently Madmachine.io released a board called SwiftIO based ARM M7 core. It can run a pure swift code that complied by the GCC for ARM. Also they developed an IDE to integrated all tool chains.

They used a smart way to take advantage of full language features, not like such Tiny Go to rewrite a special version of language compiler, they use gcc-arm to compile pure swift code into a binary file. As above mentioned, it is not easy to deal with the low level issues if there is not stronger support by swift compiler teams. SwiftIO just simple port Zephyr RTOS with external 32M ram and save the binary file into an external MicroSD card, not to burn them into the flash on chip like usually we did on other embedded boards. All the low level functions - drivers such as GPIO,I2C,SPI,UART,display and Network are implemented by the RTOS with C code. The SwiftIO framework just port them in swift. The Bootloder inside the chip to fetch the binary file from SD card and throw it into memory and then to run it. That’s all. Perfect!

Verify answered 24/4, 2021 at 0:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.