Kotlin 代码是如何编译成原生代码的?

How is Kotlin code compiled into native code?

官方Kotlin/Nativedocumentation表示Kotlin/Native

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

据我了解:

因此,Kotlin/Native 是否将 Java 字节码转换为 LLVM IR? 如果是这样,声明 Kotlin/Native 是 LLVM 后端是否正确? Kotlin 代码会被编译到 LLVM IR 中吗? 如果不是,每个编译步骤的输入和输出是什么? (例如:Kotlin -(kotlinc)-> Java 字节码 -(LLVM 后端 -> 本机二进制文件)

This 博客 post 指出 Kotlin 前端编译器(我认为它指的是 kotlinc)产生 Kotlin IR,我从未读过的。

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

这句话正确吗?

它告诉你Java字节码、本机代码和Java脚本的编译过程是一样的。你编译你的 Kotlin 代码,然后你有 3 个后端编译器提供预期的输出格式(Java 字节码,Java 脚本,二进制代码)。

最终的特定于平台的二进制文件是否包含原生 Kotlin 标准库,或者它是动态链接的吗?

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

这句话正确吗?

是的,非常正确。中间表示 (IR) 是一种普遍表示 Kotlin 程序的形式,与平台无关。然后特定于平台的后端将 IR 转换为最终的二进制形式——Kotlin/JVM 的 JVM class 文件、Kotlin/Native 的 LLVM 位码和其他元数据打包到 *.klib 中].

因此,Kotlin/Native 不以任何方式处理 JVM 字节码。

中间表示是 Kotlin 编译器的一个实现细节,除 Kotlin 编译器本身外,其他任何消费者都不会使用它,因此开发人员不应该关心它。对于 JVM,它甚至保存在内存中,从不写入二进制文件。对于Kotlin/Native,其实是序列化后写入*.klib中,以供重复使用。

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

其实一部分的编译过程是一样的,即前端语言分析,包括解析、调用解析、类型推断、诊断等步骤不需要任何特定于平台的代码转换。尽管如此,前端部分还是针对每个平台进行了调整,因为它们允许不同的语言功能集并提供不同的诊断集。

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

目前,标准库静态链接到生成的 Kotlin/Native 二进制文件中。