我试图理解Kotlin源代码在编译时所经历的过程.The documentation个州

当针对JVM时,Kotlin生成与Java兼容的字节码.当以JavaScript为目标时,Kotlin会传输到ES5.1并生成与AMD和CommonJS等模块系统兼容的代码.当以本机为目标时,Kotlin将生成特定于平台的代码(通过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?

最后,这是否意味着Kotlin既是一种编译语言,也是一种解释语言?

推荐答案

<...> 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.

JavaScript源代码也可以进行解释或JIT编译,这取决于用于运行程序的JavaScript引擎.

当以本机为目标时,代码是否直接编译为机器码?LLVM需要执行哪些步骤?

Kotlin/Native有两种可能的目标形式:

  • A *.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.
  • A platform-specific binary, in one of the numerous formats, including static and dynamic libraries and executable files. This is, indeed, the machine code for a specific target platform, which can be used for linking in case it is a library, or be run directly if it's an executable. In this case, the Kotlin compiler invokes the LLVM linker lld to link a binary from the LLVM bitcode.

Kotlin相关问答推荐

使用数据存储首选项Kotlin Jetpack Compose

在Webflux应用程序中通过kotlin协程启动fire and forget job

将文本与文本字段的内容对齐

编译后的JavaFX应用程序立即以静默方式崩溃

可选的.在kotlin中不使用泛型参数

Kotlin 复制列表中的项目以创建具有相同数据的不同对象的新列表

具有泛型类型的 Kotlin 密封接口不会为其子类型推断约束

is return inside function definition 也是 kotlin 中的表达式

禁用 Android 12 默认启动画面

Android数据绑定在自定义视图中注入ViewModel

无法从 XML 访问 NavHostFragment

致命错误 LifecycleOwners 必须在 registerForActivityResult 开始之前调用 register

Kotlin中的下划线名称是为什么保留的?

Tornadofx - 如何在每个实例上将参数传递给 Fragment

在kotlin中初始化类变量的正确位置是什么

Mocked suspend函数在Mockito中返回null

在Kotlin中将列表转换为对的惯用方法

Kotlin中的函数接口

如何在 firebase 数据库中使用 kotlin 协程

如何在 Kotlin 中生成 MD5 哈希?