I'm trying to understand the journey Kotlin source code goes through when it is compiled. The documentation states
When targeting the JVM, Kotlin produces Java compatible bytecode. When targeting JavaScript, Kotlin transpiles to ES5.1 and generates code which is compatible with module systems including AMD and CommonJS. When targeting native, Kotlin will produce platform-specific code (via LLVM).
My understanding is when Kotlin is targeting the JVM, the code is compiled/translated down to bytecode and then the JVM interprets(?) it down to machine code. Would this be an example of JIT(Just in time) compilation?
When targeting javascript the word "transpiles" is used. What exactly is the code compiled down to and is it interpreted or compiled further down at any step?
When targeting native, is code compiled directly to machine code? What steps does LLVM take it through?
Finally, would this mean Kotlin is both a compiled language and an interpreted language?
<...> the code is compiled/translated down to bytecode and then the JVM interprets(?) it down to machine code. Would this be an example of JIT(Just in time) compilation?
Yes, when targeting the JVM, Kotlin is compiled to JVM *.class
files, which is a bytecode format that can later be either interpreted by a JVM, or compiled to the machine code by the JVM during the program run (JIT), or even compiled ahead-of-time (AOT) down to the machine code. Here, the Kotlin compiler doesn't need to know how exactly the bytecode will then be used.
When targeting javascript the word "transpiles" is used. What exactly is the code compiled down to and is it interpreted or compiled further down at any step?
The target format for Kotlin/JS is JavaScript source code. You can try and build any Kotlin/JS example and examine the *.js
output files containing the JS source code that the Kotlin code is translated to. I believe the word transpile (translate + compile) is used here to emphasize that the target format is source code rather than binary, while the compiler still performs a lot of transformations and optimizations.
The JavaScript source code is, just as well, either interpreted or JIT-compiled, which depends on the JavaScript engine used to run the program.
When targeting native, is code compiled directly to machine code? What steps does LLVM take it through?
There are two possible target forms for Kotlin/Native:
*.klib
library that can be reused in another Kotlin/Native project. This is a ZIP archive containing LLVM bitcode along with some Kotlin-specific metadata.lld
to link a binary from the LLVM bitcode.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With