Skip to content
This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
/ microkt Public archive

A compiler for a subset of the Kotlin language. It produces a native executable on MacOS and Linux.

Notifications You must be signed in to change notification settings

gaultier/microkt

Repository files navigation

microkt

A work-in-progress compiler for a subset of the Kotlin language, with zero dependencies. It compiles a (very small) subset of the Kotlin language to a native executable.

For now it only supports x86_64 (macOS & Linux) although it would not be hard to add more platforms.

Have a look at the tests directory to get a feeling of what's supported. Here's a sample (see the Quickstart section to see how to run it):

// Compute the 35th fibonacci number recursively
fun fibonacci(n: Long) : Long {
  if (n == 0L) return 0L
  if (n == 1L) return 1L

  return fibonacci(n-1L) + fibonacci(n-2L)
}

fun main() {
  println(fibonacci(35L)) // expect: 9227465
}

Quickstart

# Only requires a C99 compiler toolchain and GNU make
# On Linux, you probably need to use `WITH_DTRACE=0`
make

# Run the tests
make check

# Compile a source file (requires `as` and `ld` in the PATH)
./mktc tests/hello_world.kt

# And then run the executable
./tests/hello_world.exe
Hello, world!

# Also works in Docker
docker build -t microkt .
docker run --rm -it microkt sh -c 'mktc /usr/local/share/mktc/hello_world.kt \
    && /usr/local/share/mktc/hello_world.exe'

Hello, world!

Features

  • Small, fast, and portable: the code is written in C99 with zero dependencies. It compiles in under a second to an executable no bigger than 100 Kib
  • Produces small native executables under 10 Kib in a few milliseconds
  • Friendly error messages
  • Tiny memory usage
  • Simple mark and sweep garbage collector
  • The native executables produced by mktc only use libc

On the roadmap

Future features that are not yet implemented but are planned. In no particular order:

  • Cross-compilation
  • Classes
  • Floats and doubles
  • Type inference
  • Out-of-order declarations (e.g. use before declare)

Non features

Things that we do not have today. Maybe we will have them in the future, and maybe not. PRs welcome, I guess. In no particular order:

  • Support for the full Kotlin language
  • .kts files i.e. 'script' files
  • Production grade quality. There will be bugs; use at your own risk
  • The Kotlin standard library (we only support println at the moment)
  • Language Server Protocol (LSP)
  • Source code formatter
  • Support for other architectures e.g. ARM or RISC
  • 32 bits support
  • Windows/FreeBSD/NetBSD/OpenBSD/Illumos support (although Linux executables should work just fine on FreeBSD, NetBSD and Illumos; Windows has WSL)
  • Linter
  • Compile to JVM bytecode
  • Compile to JS
  • Compile to WebAssembly
  • Highly optimized generated assembler instructions a la LLVM/GCC

Develop

Available Makefile flags:

  • WITH_LOGS=0|1 : disable/enable logs. Defaults to 0.
  • WITH_ASAN=0|1 : disable/enable AddressSanitizer (clang only). Defaults to 0.
  • WITH_OPTIMIZE=0|1 : optimization level. Corresponds to respectively -O0 and -O2
  • WITH_DTRACE=0|1 : disable/enable dtrace in generated executables. Defaults to 1; on Linux, you will most likely not have dtrace so you need to pass WITH_DTRACE=0
  • CC, AS, LD: standard make variables
# Debug build with logs and asan, using clang
make WITH_OPTIMIZE=0 WITH_LOGS=1 WITH_ASAN=1 CC=clang

# Release build without logs with asan using gcc and tell `mktc` to use `lld` as linker
make WITH_ASAN=1 CC=gcc LD=ld64.lld

License

BSD-3

About

A compiler for a subset of the Kotlin language. It produces a native executable on MacOS and Linux.

Topics

Resources

Stars

Watchers

Forks