Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Kotlin code compiled into native code?

The official Kotlin/Native documentation states that Kotlin/Native

.. is an LLVM based backend for the Kotlin compiler.

As far as I understand:

  • The Kotlin compiler (kotlinc) produces .class files (with Java bytecode) from your Kotlin source files.
  • Generic LLVM backends (that are not related to Kotlin) take LLVM IR and turn it into binary code.

Therefore, is Kotlin/Native converting Java bytecode into LLVM IR? If so, is it correct to state that Kotlin/Native is a LLVM backend? Does Kotlin code get compiled into LLVM IR? If not, what is the input and output of each compilation step? (e.g.: Kotlin -(kotlinc)-> Java bytecode -(LLVM backend-> native binary)

This blog post states that the Kotlin frontend compiler (I think it is referring to kotlinc) produces Kotlin IR, which I have never read of.

Kotlin compiler has a single front-end but multiple back-end depending upon the the plugin you are using to build the code. Kotlin/Native plugin converts the Kotlin Intermediate Representation (IR) to native code (i.e code that is machine executable).

Is this quote correct?

It tells you that the compilation process is the same for Java bytecode, native code and JavaScript. You compile your Kotlin code and then you have 3 backend compilers that deliver the expected output format (Java bytecode, JavaScript, binary code).

Does the final platform specific binary include the native Kotlin standard library or is it linked dynamically?

like image 298
Andreas Avatar asked Sep 10 '19 13:09

Andreas


1 Answers

Kotlin compiler has a single front-end but multiple back-end depending upon the the plugin you are using to build the code. Kotlin/Native plugin converts the Kotlin Intermediate Representation (IR) to native code (i.e code that is machine executable).

Is this quote correct?

Yes, it's quite correct. The intermediate representation (IR) is a form that universally represents a Kotlin program, regardless of the platform. Then a platform-specific back-end translates the IR to the final binary form – the JVM class files for Kotlin/JVM, LLVM bitcode and other metadata packed into a *.klib for Kotlin/Native.

So, Kotlin/Native doesn't deal with the JVM bytecode in any way.

The intermediate representation is an implementation detail of the Kotlin compiler, and it's not used by any other consumer than the Kotlin compiler itself, so it's not a thing that a developer should care about. For the JVM, it's even kept in memory and never written to binaries. For Kotlin/Native, it's actually serialized and written into the *.klib to be reused.

It tells you that the compilation process is the same for Java bytecode, native code and JavaScript. You compile your Kotlin code and then you have 3 backend compilers that deliver the expected output format (Java bytecode, JavaScript, binary code).

Actually, a part of the compilation process is the same, namely, the front-end language analysis, which includes parsing, call resolution, type inference, diagnostics and other steps that don't require any code transformations that are platform-specific. Even so, the front-end part is tuned for each of the platforms, as those allow different sets of language features and provide different sets of diagnostics.

And the last question does the final platform specific binary include the native Kotlin standard library? Or is it linked dynamically?

Currently, the standard library is statically linked into the resulting Kotlin/Native binaries.

like image 73
hotkey Avatar answered Oct 04 '22 15:10

hotkey