https://kotlinlang.org logo
Title
b

bhaskarananthakrishna

02/22/2023, 2:25 PM
Hello Kotlin Devs, we recently started using KMM, and loving every minute of it. I have decided to document few highlights and add it to the list of community articles out there. here is one of the articles: https://medium.com/@InfinateDarkness/kmm-whats-under-the-hood-1a26cb50ca67 welcome any suggestions 🙂...
t

Troels Lund

02/22/2023, 2:34 PM
The LLVM bytecode is then passed through the LLVM-C interoperability layer, which converts it into C code.
No it doesn't.
b

bhaskarananthakrishna

02/22/2023, 2:48 PM
Hi @Troels Lund, Thanks for your reply. Perhaps my understanding is wrong here, can you please elaborate and help me understand the translation from Kotlin/Native to executable byte code for iOS/Mac? or point me in the right direction so i can modify the article accordingly 🥲
t

Troels Lund

02/22/2023, 3:24 PM
A good starting point is https://kotlinlang.org/docs/native-objc-interop.html I am interested in where you read that it got compiled to C code.
b

bhaskarananthakrishna

02/22/2023, 3:38 PM
"*Clang* is an "LLVM native" C/C++/Objective-C compiler, which aims to deliver amazingly fast compiles, extremely useful error and warning messages and to provide a platform for building great source level tools. The Clang Static Analyzer and clang-tidy are tools that automatically find bugs in your code, and are great examples of the sort of tools that can be built using the Clang frontend as a library to parse C/C++ code." perhaps it should be objective-C executables instead of C code to eliminate confusion?
"Kotlin/Native is a technology for compiling Kotlin code to native binaries which can run without a virtual machine. Kotlin/Native includes an LLVM-based backend for the Kotlin compiler and a native implementation of the Kotlin standard library." is from the official documentation.
w

Wout Werkman

02/22/2023, 4:01 PM
This means that both C and Kotlin can compile to LLVM IR, which is most often compiled to executable binaries. So in short:
Kotlin/Native -> LLVM IR -> target machine code
And also:
C/C++/Objective-C (using clang) -> LLVM IR -> target machine code
Great article nonetheless 🙂
b

bhaskarananthakrishna

02/22/2023, 4:01 PM
ah i see so there is no C middle layer in this conversion? I shall remove the claim "which converts it into C code" into "which converts it into platform executable code"
w

Wout Werkman

02/22/2023, 4:03 PM
Correct 👍
b

bhaskarananthakrishna

02/22/2023, 4:09 PM
Thanks a lot 🙌 @Wout Werkman and @Troels Lund I have since changed the statement, and re-wrote it as "*Kotlin-Native* In order to use Kotlin code in iOS, the Kotlin code is first compiled to LLVM bytecode using the Kotlin/Native compiler. LLVM (Low Level Virtual Machine) is a collection of modular and reusable compiler and toolchain technologies. The LLVM bytecode is then compiled using the iOS toolchain and linked with the iOS runtime libraries."
p

Pablichjenkov

02/22/2023, 4:46 PM
KN acts as a c/c++ competitor in this arena. Thanks for the article
b

bhaskarananthakrishna

02/22/2023, 5:21 PM
@Pablichjenkov Yes, i understand it now, i originally thought LLVM-native always compiled into a C-executable. after the feedback, additionally : https://kotlinlang.org/docs/native-command-line-compiler.html#compile-the-code-from-the-console helped me understand, KMM compiler as independent compiler where LLVM-native is just a part, not the entirety.