I took a glance how IR → JS translation is done. It’s actually not that hard to grasp. Here are my notes for someone that wants to tackle this (might be me one day ):
- the most interesting Gradle module:
compiler/ir/backend.js
(to see its logic, jump straight tocompiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js
) - the top-level file seems to be
compiler.kt
, with two functions:compile
returningCompilerResult
that containsval jsCode: String?
, andgenerateJsCode
. IR is the input, JS code is the output - going deeper, see
transformers/irToJs/IrModuleToJsTransformer.kt
and other...ToJsTransformer.kt
files. They seem to be the places which perform the actual mapping from IR entities to JavaScript abstract syntax tree (org.jetbrains.kotlin.js.backend.ast.*
entities) - the mapping of JS AST to the actual JS code is common for both the old and the new IR backend (see
js/js.ast
Gradle module). It happens inJsProgram
’stoString()
, inherited fromAbstractNode
. The actual implementation sits inJsToStringGenerationVisitor
Next steps: compile the compiler/ir/backend.js
module (after a small modification, to be sure that the modified version is used instead of the prod Kotlin compiler) and use it with some example Kotlin code. Once it works, we can gradually experiment with making adjustments towards Python. Maybe even the “IR → JS AST” layer can stay as is for now (I see some similarities between JS and Python and we could take advantage of them), and only “JS AST → JS code” layer can be adjusted to generate Python.