Skip to content

A small and fast Kotlin compiler in C99. Still experimental!

Notifications You must be signed in to change notification settings

gaultier/micro-kotlin

Repository files navigation

A fast, small Kotlin compiler written in C

A statically linked, dependencyless, 200 Kib (stripped) executable using < 10 MiB of memory, with pprof memory profiling included.

Caution: Not production grade! Not ready!

My third (and last?) take on a Kotlin compiler, in C. It goes much further than the two previous attempts both in terms of language support and in terms of implementation:

  • Expressions, statements, control flow, and functions (including recursion) are implemented
  • It implements Stack Map Frames contrary the kotlin-rs so the latest JVM versions work seamlessly with it and bytecode generated by the compiler is verified at load time by the JVM
  • It implements type constraints in the type checker to infer which function the user intended to call (and the rules in Kotlin for that are so very complex). That's one of the thorniest topics in Kotlin due to the language trying to have every programming language feature under the sun, namely: type inference, function overloading, default parameters, generics, implicit callers (it and such), variadic functions, object inheritance with method overriding, etc.
  • It explores the class path so that external libraries can be used including the Java and Kotlin standard libraries
  • Some Java/Kotlin interop is supported (mostly, calling Java functions from Kotlin)
  • It parses and understands .jmod, .class, .jar files even with compression
  • Out-of-order definitions of functions and variables are supported
  • Some support for function inlining is supported (inlining the body of a called function)
  • All allocations are done with an arena allocator and there is support for a memory dump with stacktraces
  • It's only 10k lines of code and the final stripped executable is 145 Kib!

See TODO.md for details.

Such code works:

fun fibonacci_rec(n: Int) : Int {
  if (n == 1) {
    return 1
  } 
  return n + fibonacci_rec(n-1)
}

fun main() {
  println("Hello, world!")
  println(fibonacci_rec(10))
}

Quickstart

Requirements: A C99 compiler, the Kotlin standard library, the Java standard library, optionally: zlib (to read jar files that have compression).

# Pick whatever variant and version of the JDK you like here. We only need to get the Kotlin & Java standard library files.
$ sudo apt install kotlin openjdk-21-jdk 
$ make
$ ./micro-kotlin --java-home /usr/lib/jvm/java-21-openjdk-amd64/ -c /usr/share/java/kotlin-stdlib.jar demo.kt
$ java DemoKt
Hello, world!
12
72
144
55
1
2
255

See demo.kt to see what language constructs are supported at the moment.

$ micro-kotlin --help
A small compiler for the Kotlin programming language.

  micro-kotlin [OPTIONS] <path>

EXAMPLES:
  micro-kotlin -j /usr/lib/jvm/java-21-openjdk-amd64/ -c /usr/share/java/kotlin-stdlib.jar main.kt

OPTIONS:
  -v, --verbose                  Verbose.
  -m, --memory-usage             Debug memory usage by printing a heap dump in the pprof format.
  -h, --help                     Print this help message and exit.
  -c, --classpath <classpath>    Load additional classpath entries, which are colon separated.
  -j, --java-home <java_home>    Java home (the root of the Java installation).

LICENSE

BSD-3.

About

A small and fast Kotlin compiler in C99. Still experimental!

Topics

Resources

Stars

Watchers

Forks

Languages