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?
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.
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).
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!
© 2022 - 2025 — McMap. All rights reserved.