JetBrains / kotlin-native

Kotlin/Native infrastructure
Apache License 2.0
7.02k stars 564 forks source link

Provide Support For ARM Microcontrollers #458

Closed napperley closed 6 years ago

napperley commented 7 years ago

LLVM has support for targeting ARM Microcontrollers like the Cortex M3 and M4 for example. Kotlin Native should have the ARM microcontrollers as the next target. Many of the ARM microcontroller boards provide standard C libraries to cover low level HW access.

One ARM microcontroller board that would be a good candidate to get Kotlin Native working on is the STM 32 F4 Discovery. Some companies like Samsung have successfully used LLVM to target ARM microcontrollers.

olonho commented 7 years ago

The support for embedded targets is being considered, and community contributions are welcome.

napperley commented 7 years ago

What is the standard procedure for adding a new platform (eg cortex-m3) to Kotlin Native?

napperley commented 7 years ago

Does the Kotlin Native LLVM backend have ARM Thumb support compiled into it?

olonho commented 7 years ago

See https://github.com/JetBrains/kotlin-native/pull/365/files as an example of adding new backend. Also you need to create appropriate sysroot and stub/implement some platform functionality, such as locale support (will be fixed soon).

olonho commented 7 years ago

llc --version from toolchain lists all supported targets, and Thumb is amongst them.

napperley commented 7 years ago

@olonho - Thank you for the update on the Thumb support. How much of an issue is locale support? Huge concerns regarding the size of the Kotlin standard library since code size really matters on the ARM microcontroller side (every bit counts). Where is the Kotlin standard library currently located?

Is it normal for LLVM to generate ARM executables that are bigger than the Linux x86-64 ones?

olonho commented 7 years ago

If you're OK with not being able to parform case conversions and charater type detection - simple ASCII-only stubs could be used.

Standard library is linked into final binary only partially (due to LTO optimizations). stdlib is in https://github.com/JetBrains/kotlin-native/tree/master/runtime/src.

Generally, Thumb2 code compiled with optimizations (-opt konanc flag) is expected to be a bit smaller than optimized x86.

napperley commented 7 years ago

Character/string handling won't be a big issue at this stage. More important to have basic C interop that is good enough to do a basic Blinky project (requires basic hardware access) to see if it works. Have a STM32 Nucleo board in mind for the project.

Good to know that Thumb code will be smaller.

Generally, Thumb2 code compiled with optimizations (-opt konanc flag) is expected to be a bit smaller than optimized x86.

olonho commented 7 years ago

I removed system locale dependencies, so now it shall not be a problem anymore.

napperley commented 7 years ago

Have temporarily procured a STM32F411 (development board) for the Blinky project. Below is the basic hardware info for the board:

napperley commented 7 years ago

Using the following Target Triple:

Translates to the following string: thumbv7m-eabi. Note that there is no vendor and OS specified. Microcontrollers don't need an OS.

Using the following hardware flag: -mcpu=cortex-m4

Attempting to figure out how to create sysroot since it can be very tricky with the ARM microcontroller CPUs as there are many manufacturers that implement them in different ways.

olonho commented 7 years ago

For sysroot you likely need to start with some existing toolchain to produce binaries for your board. Once you wil create proper tarball I can make it available for download.

napperley commented 7 years ago

Is libffi required for the ARM Cortex M4 port? Does Kotlin Native use libffi for the C interop?

olonho commented 7 years ago

Yes, it is required for some corner cases, such as varargs processing. Basic port not needing C varargs could try to survive #ifdef'ing FFI code.

napperley commented 7 years ago

Where would the #ifdef'ing FFI code be implemented?

napperley commented 7 years ago

Currently libffi don't support the Cortex M CPUs, but do support the Cortex A ones. Downright confusing to determine which instruction set each ARM CPU uses.

olonho commented 7 years ago

https://github.com/JetBrains/kotlin-native/blob/master/runtime/src/main/cpp/Interop.cpp#L20

olonho commented 7 years ago

Also for purposes of what we need from libffi supporting Thumb2 ISA shalln't be too hard, looking at what kind of ASM it needs https://github.com/libffi/libffi/blob/master/src/arm/sysv.S#L153. Problematic instructions are multiple loads with disjoint register sets, maybe those could be split into multiple instructions, and 3 argument arithmetic, which likely could be achieved by two argument arithmetic and move.

napperley commented 7 years ago

All Cortex M CPUs support Thumb1, NOT Thumb2.

napperley commented 7 years ago

Cortex A CPUs support Thumb2, which is what the Raspberry Pi (RPi) and Beaglebone Black (BBB - RPi's biggest competitor) use for example. Very common for the Single Board Computers (SBCs) to use a Cortex A CPU.

napperley commented 7 years ago

Turns out that not all Cortex M's are Thumb1 only.

Might be best to try with an ARM Cortex M7 (has Thumb2 support) development board. Don't know anyone who has one. Would be good if a major ARM manufacturer could send one to JetBrains for the Kotlin Native port.

jabrena commented 7 years ago

I think that the compilation for ARM 7, could be necessary to be adopted for educational robots like Lego Mindstorms. https://en.wikipedia.org/wiki/Lego_Mindstorms_EV3 http://ev3dev-lang-java.github.io/#/ http://www.oracle.com/technetwork/java/embedded/downloads/javase/javaseemeddedev3-1982511.html

2010 Chip included on EV3 Brick:

SoC: Sitara Processor AM1808
CPU: ARM9 300MHz
RAM: 16KB of Instruction Cache, 16KB of Data Cache ,8KB of RAM (Vector Table), 64KB of ROM

2016 Chip included on Raspberry Pi 3:

SoC: Broadcom BCM2837
CPU: 4× ARM Cortex-A53, 1.2GHz
RAM: 1GB LPDDR2 (900 MHz)
napperley commented 7 years ago

First version of the Raspberry Pi's SoC is ARM 7 based and Kotlin Native was recently back ported to it. Juan - Are you talking about the old application based ARM CPUs?

On Sun, 18 Jun 2017 at 10:32 Juan Antonio Breña Moral < notifications@github.com> wrote:

I think that the compilation for ARM 7, could be necessary to be adopted for educational robots like Lego Mindstorms. https://en.wikipedia.org/wiki/Lego_Mindstorms_EV3 http://ev3dev-lang-java.github.io/#/

http://www.oracle.com/technetwork/java/embedded/downloads/javase/javaseemeddedev3-1982511.html

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JetBrains/kotlin-native/issues/458#issuecomment-309244198, or mute the thread https://github.com/notifications/unsubscribe-auth/ADX9Z_eGuyIXpGZX3ve8JSSZ3Z4ckFwaks5sFFQFgaJpZM4M2vV9 .

jabrena commented 7 years ago

Hi @napperley,

This kind of boards: https://en.wikipedia.org/wiki/ARM9#ARM9E

Next week, I will test the native features with Raspi 3 + BrickPi: https://www.dexterindustries.com/brickpi/

But I think that EV3 community is greater. If Kotlin could have support for that boards, it could be pretty interesting for the whole ecosystem.

Cheers

Juan Antonio

olonho commented 7 years ago

With recent changes for WASM, unifying memory management and making explicit porting layer here https://github.com/JetBrains/kotlin-native/blob/master/runtime/src/main/cpp/Porting.cpp porting Kotlin/Native to embedded targets shall be relatively easy.

khatamnejad commented 7 years ago

Hi everyone,

I am curious if anyone has started adding Thumb (either Thumb-1 or Thumb-2) support? Is there any repository?

Regards, Aydin

olonho commented 6 years ago

STM32 support is merged, it includes Thumb support in https://github.com/JetBrains/kotlin-native/pull/1305

mgiaco commented 6 years ago

@olonho that sounds really great. Is there any information available how to start with a sample project?