Cross Compiling Swift for Bare Metal?

Hi,

I wanted to know if it is possible to compile swift code for bare metal. I know there is a runtime but does swift depend on it to execute even if I don't use those features?, can I disable them somehow. I was hoping that since you can produce LLVM Bytecode that you can cross compile using the arm-none-eabi toolchain for an embedded use-case (http://www.ti.com/tool/ek-tm4c123gxl\). Please tell me there's hope, I don't want to use C++ :(

Thanks.

It is a goal of Swift to be a systems programming language, but nobody has
yet compiled a program using Swift for a bare-metal target, so a
significant amount of work would be required.

The Swift runtime is required to support basic features such as object
allocation, casts and generics, so you can't just omit it.

Regarding the microcontroller that you referenced, I think it would be an
overly ambitious goal to try to make everything work with 256 Kb of flash
and 32 Kb of RAM. I would recommend to start with a more powerful
bare-metal target, and get things working without an OS and memory
protection, measure RAM and flash consumption, and try to fit everything
into a smaller MCU.

Dmitri

···

On Sat, Dec 19, 2015 at 6:53 PM, Chaitanya Mannem via swift-users < swift-users@swift.org> wrote:

Hi,

I wanted to know if it is possible to compile swift code for bare metal. I
know there is a runtime but does swift depend on it to execute even if I
don't use those features?, can I disable them somehow. I was hoping that
since you can produce LLVM Bytecode that you can cross compile using the
arm-none-eabi toolchain for an embedded use-case (
http://www.ti.com/tool/ek-tm4c123gxl\). Please tell me there's hope, I
don't want to use C++ :(

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/

Though it might sound a little silly (because why not run linux), but starting with a beagle bone black would not be a terrible way to begin with bare metal work. It has plenty of RAM, you know that LLVM writes good machine code for it, it has JTAG accessible, it’s inexpensive, etc...

This would be an amazing achievement. I admit that was one of the first things I thought of when open source swift was announced. I loath programming in anything else. I’m spoiled.

- Will

···

On Dec 20, 2015, at 12:22 AM, Dmitri Gribenko via swift-dev <swift-dev@swift.org> wrote:

On Sat, Dec 19, 2015 at 6:53 PM, Chaitanya Mannem via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Hi,

I wanted to know if it is possible to compile swift code for bare metal. I know there is a runtime but does swift depend on it to execute even if I don't use those features?, can I disable them somehow. I was hoping that since you can produce LLVM Bytecode that you can cross compile using the arm-none-eabi toolchain for an embedded use-case (http://www.ti.com/tool/ek-tm4c123gxl\) <http://www.ti.com/tool/ek-tm4c123gxl\)>. Please tell me there's hope, I don't want to use C++ :(

It is a goal of Swift to be a systems programming language, but nobody has yet compiled a program using Swift for a bare-metal target, so a significant amount of work would be required.

The Swift runtime is required to support basic features such as object allocation, casts and generics, so you can't just omit it.

Regarding the microcontroller that you referenced, I think it would be an overly ambitious goal to try to make everything work with 256 Kb of flash and 32 Kb of RAM. I would recommend to start with a more powerful bare-metal target, and get things working without an OS and memory protection, measure RAM and flash consumption, and try to fit everything into a smaller MCU.

Dmitri

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com <mailto:gribozavr@gmail.com>>*/
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

Though it might sound a little silly (because why not run linux), but starting with a beagle bone black would not be a terrible way to begin with bare metal work. It has plenty of RAM, you know that LLVM writes good machine code for it, it has JTAG accessible, it’s inexpensive, etc...

This would be an amazing achievement. I admit that was one of the first things I thought of when open source swift was announced. I loath programming in anything else. I’m spoiled.

- Will

···

On Dec 20, 2015, at 12:22 AM, Dmitri Gribenko via swift-dev <swift-dev@swift.org> wrote:

On Sat, Dec 19, 2015 at 6:53 PM, Chaitanya Mannem via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Hi,

I wanted to know if it is possible to compile swift code for bare metal. I know there is a runtime but does swift depend on it to execute even if I don't use those features?, can I disable them somehow. I was hoping that since you can produce LLVM Bytecode that you can cross compile using the arm-none-eabi toolchain for an embedded use-case (http://www.ti.com/tool/ek-tm4c123gxl\) <http://www.ti.com/tool/ek-tm4c123gxl\)>. Please tell me there's hope, I don't want to use C++ :(

It is a goal of Swift to be a systems programming language, but nobody has yet compiled a program using Swift for a bare-metal target, so a significant amount of work would be required.

The Swift runtime is required to support basic features such as object allocation, casts and generics, so you can't just omit it.

Regarding the microcontroller that you referenced, I think it would be an overly ambitious goal to try to make everything work with 256 Kb of flash and 32 Kb of RAM. I would recommend to start with a more powerful bare-metal target, and get things working without an OS and memory protection, measure RAM and flash consumption, and try to fit everything into a smaller MCU.

Dmitri

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com <mailto:gribozavr@gmail.com>>*/
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

It would be great to get Swift up and running on a beefier ARM system like a BeagleBoard or Raspberry Pi 2, or the upcoming C.H.I.P. (The other RPi models could be problematic because their CPUs are only ARM6.)

Writing for the bare metal probably isn’t the most effective way to go unless your primary goal is to learn — before you can get Swift running you’d need to implement a memory allocator, enough concurrency/threading to be able to handle I/O interrupts, and drivers for whatever I/O devices you need like USB or WiFi. If I were doing a project like this I’d start with a microkernel like FreeRTOS or the guts of MINIX3, which provide the above and a bit more.

—Jens

···

On Dec 20, 2015, at 12:22 AM, Dmitri Gribenko via swift-users <swift-users@swift.org> wrote:

Regarding the microcontroller that you referenced, I think it would be an overly ambitious goal to try to make everything work with 256 Kb of flash and 32 Kb of RAM. I would recommend to start with a more powerful bare-metal target

The code that emits runtime calls in IRGen is fairly well-factored. One thing you might experiment with is adding an `-fstandalone` flag that causes the compiler to diagnose any time it has to generate code with a runtime call. If you're willing to forgo the standard library and build from LLVM primitives up and always use -Ounchecked, you might be able to generate standalone binaries without a runtime dependency. You're on your own if you go that road though.

-Joe

···

On Dec 19, 2015, at 6:53 PM, Chaitanya Mannem via swift-users <swift-users@swift.org> wrote:

Hi,

I wanted to know if it is possible to compile swift code for bare metal. I know there is a runtime but does swift depend on it to execute even if I don't use those features?, can I disable them somehow. I was hoping that since you can produce LLVM Bytecode that you can cross compile using the arm-none-eabi toolchain for an embedded use-case (http://www.ti.com/tool/ek-tm4c123gxl\) <http://www.ti.com/tool/ek-tm4c123gxl\)>. Please tell me there's hope, I don't want to use C++ :(

I never thought I’d say this, but $50 seems kind of expensive for a computer :) The specs look like the Raspberry Pi 2 which is closer to $30. Me, I’m waiting for my $9 C.H.I.P. (similar specs) that I Kickstarted to arrive :)

Writing a kernel would be awesome. I’ve been reading Operating Systems Design And Implementation, “the MINIX book”, which is a pretty clear introduction to Unix-like OSs, but of course the kernel source code it walks through is a mix of C and x86 asm, and worse, it’s ten years behind the current MINIX3 kernel sourcebase. But it could be rewritten in Swift… :) And the great thing is that if you got the kernel running, you could then drop the whole rest of MINIX on top of it and instantly have a working BSD-like UNIX with a shell, filesystem, networking…

—Jens

···

On Dec 20, 2015, at 3:03 PM, William Dillon via swift-users <swift-users@swift.org> wrote:

Though it might sound a little silly (because why not run linux), but starting with a beagle bone black would not be a terrible way to begin with bare metal work. It has plenty of RAM, you know that LLVM writes good machine code for it, it has JTAG accessible, it’s inexpensive, etc...