Cross Compiling Swift for Bare Metal?


(Chaitanya Mannem) #1

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++ :frowning:

Thanks.


(Dmitri Gribenko) #2

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++ :frowning:

--
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>*/


(William Dillon) #3

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鈥檚 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鈥檓 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++ :frowning:

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


(William Dillon) #4

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鈥檚 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鈥檓 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++ :frowning:

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


(Jens Alfke) #5

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鈥檛 the most effective way to go unless your primary goal is to learn 鈥 before you can get Swift running you鈥檇 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鈥檇 start with a microkernel like FreeRTOS or the guts of MINIX3, which provide the above and a bit more.

鈥擩ens

路路路

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


(Joe Groff) #6

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++ :frowning:


(Jens Alfke) #7

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

Writing a kernel would be awesome. I鈥檝e been reading Operating Systems Design And Implementation, 鈥渢he 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鈥檚 ten years behind the current MINIX3 kernel sourcebase. But it could be rewritten in Swift鈥 :slight_smile: 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鈥

鈥擩ens

路路路

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鈥檚 inexpensive, etc...