Got my hands very dirty today with C++/Arduino cod...
# kotlin-native
l
Got my hands very dirty today with C++/Arduino code, and I am seriously considering finding a way to generate that C++ from Kotlin code (because Kotlin is easier to write and read as a human). Why not directly to machine code? First, I have no idea how to generate correct machine code that could run on AVR microcontrollers. Second, I don't see myself checking the generated machine code, while for C++, it's doable to a certain extent. Would someone want to brainstorm that idea with me?
👀 1
a
how complicated is it to add a C++ target?
Transpile from kotlin to cpp
g
Yeah, I was also thinking of transpiling Kotlin to C++. Something similar has been done with Swift, I think.
Please, @louiscad, is this probably what you had in mind? Link: https://github.com/studo-app/Kotlift
This is Kotlin to Swift.
l
I have multiple things in mind @Glen
g
OK. I was asking whether the repo above accomplished one of the ideas you had in mind😅
l
Transpiling is one of the things on the table. Another one is to convert Kotlin IR to C++. @svyatoslav.scherbina I believe you are the most informed on the topic. How doable is making a Kotlin program that generates C++ from Kotlin IR?
👍 1
👍🏼 1
s
How doable is making a Kotlin program that generates C++ from Kotlin IR?
Well, it might be possible, but I won’t expect this to be easy, fast or pleasant.
😅 1
l
fast
You mean from a development process point of view or regarding the generated code?
s
I mean that implementing such a translator would probably take a lot of time.
l
What do you think would make it so long to implement?
s
Generally this depends on how much you would like the resulting compiler to behave like Kotlin. Making trivial unambitious “Hello world” PoC would probably be fun and easy, but things will get much more rough once you try to add GC, inheritance, make existing Kotlin code and libraries (including stdlib) work etc.
l
I'd be fine with not 100% of Kotlin working so it long it allows to write okay code for typical Arduino or light embedded projects. I might also consider outputting C code instead of C++ if doable (I think so, might be more compatible for very barebones or niche embedded "platforms", also maybe easier than writing C++). Where should I look to get started in reading Kotlin IR (from Kotlin/JVM code)? Is it just bytes or is there also a kind of AST (Abstract Syntax Tree) or higher-level API I can use to read and output whatever I want from it?
About "GC", my planned approach is to circumvent it by counting allocations that are not deallocated in a given function scope, and allow sending this info over serial port, so I can see/report if there's any dynamic allocation beyond the objects that should live as long the program runs. That should be fine for many use cases.
s
Where should I look to get started in reading Kotlin IR (from Kotlin/JVM code)?
In Kotlin JVM IR backend source code in this case. For example, this function translates frontend output to IR: https://github.com/JetBrains/kotlin/blob/fb1b253d1efb830c58efd51f203c4a17788f9e75/[…]int/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt
Is it just bytes or is there also a kind of AST (Abstract Syntax Tree) or higher-level API I can use to read and output whatever I want from it?
There are AST and higher-level APIs as a part of the compiler.
🙏 1
p
@louiscad have you gained any now knowledge on your search for kotlin on arduino?
l
@Paul Woitaschek A bit. I know where to search, but I didn't get started on a project yet. I'd start by getting some experience with the Kotlin compiler by writing some plugins or looking at existing ones like the Jetpack Compose compiler.
🙏 2