Kotlin compiles directly to native code via LLVM

The Kotlin/Native compiler, currently in development, generates platform-native binaries from Kotlin code and is interoperable with C functions

Kotlin compiles directly to native code via LLVM
Deavmi (CC BY-SA 3.0)

Kotlin, the JVM-based language from IDE maker JetBrains, can now be compiled to standalone executables using the newly unveiled Kotlin/Native compiler.

The compiler uses the LLVM project to generate the executables—another example of LLVM as the premier tool to both enable new languages and enhance existing ones.

Go native, go faster

In its current pre-release form, Kotlin/Native produces binaries for MacOS 10.0 and greater, Ubuntu Linux 14.04 and greater, iOS, and Raspberry Pi. Microsoft Windows is not yet supported, due to “significant difference[s] in exception handling model on MS Windows and other LLVM targets,” but “this situation could be improved in upcoming releases,” JetBrains says. Many other features, like coroutines (recently introduced for Kotlin and LLVM), aren’t available yet.

One heavily touted Kotlin/Native feature, even at this early point in its development, is interoperability with C functions. C variable types are all supported with Kotlin counterparts, and you can automatically generate bindings from any C header at build time. With a language that compiles to a platform-native binary, working with C seems a must, given that C is one of the few languages that exists on almost every hardware platform.

Memory management has been left deliberately open-ended with Kotlin/Native. JetBrains claims it makes sense to have different memory management options depending on the target platform—for example, tracing garbage collection for desktop/server scenarios, simple reference counting on smaller platforms, or manual memory management if that’s needed. The only memory management option currently offered is a reference counting system, which is slow but reliable.

One compiler, many destinations

In the long term, JetBrains wants to make it possible to “use Kotlin to write every component [of a modern application], from the server back-end to the web or mobile clients.”

Kotlin has already taken steps toward becoming a cross-platform, full-stack language. Earlier this year JetBrains released version 1.1 of the language, which allow Kotlin to be transpiled to JavaScript—in theory making it possible to author both the back end and front end of a web app using the language.

Using LLVM opens up a far broader range of compilation targets for Kotlin. One possibility is to compile Kotlin code to the WebAssembly portable bytecode format. LLVM supports WebAssembly as a compilation target, and WebAssembly became incrementally more practical to use now that most major browsers have provisional support for it.

Copyright © 2017 IDG Communications, Inc.