Skip to content

How it works: the pipeline ​

minic is a Kotlin compiler with a custom backend. It reuses a Kotlin frontend to understand your source, lowers the result to its own mid-level IR (MIR), optimizes reference counting, emits plain C17, and lets clang compile that C together with a small C runtime, libminirt.

The stages ​

StageWhereWhat it does
Frontendtools/k2-oracle/jvm-bridge (K2; the shipped minic uses the own frontend)Parses and type-checks your files together with all of stdlib/. Runs the real Kotlin K2 compiler (pinned to 2.4.21) on the JVM, with -no-stdlib -no-jdk, so kotlin.* comes from Mini's own sources. A compiler plugin receives the Kotlin IR after fir2ir and before any JVM lowering.
Reachabilityjvm-bridgeDrops every stdlib declaration the program cannot reach, so println("x") becomes about 66 lines of C.
Ir2Mirjvm-bridge (Ir2Mir*.kt)Translates the Kotlin IR to MIR: classes, vtables, closures, inline expansion, exceptions, boxing. Anything it does not understand is a compile error, never wrong C.
Memory checkerbackend/.../memoryFor Strict files, a second view of the same IR (MemoryFacts to MemIr) goes through CFG construction and a dataflow analysis. Errors stop the build before code generation. See Memory checker.
MIRbackend/.../Mir.ktThe mid-level IR: structured, typed, C-oriented. See MIR.
RcOptbackend/.../RcOpt.ktComputes an RcPlan per function: which locals borrow instead of owning, which last uses move, which constructor parameters are owned. See Reference counting and RcOpt.
CEmitterbackend/.../CEmitter.ktWrites C17 using GNU statement expressions. Placement of retain and release follows the plan.
clangexternalCompiles the C with -std=c17 -O1 -Wall and links libminirt, whose sources are compiled together with the program (there is no separate library build).

Two frontends ​

Today every build goes through K2. The long-term goal is a compiler written in Kotlin that compiles itself without a JVM, so a second frontend (lexer, parser, type checker) is being written in compiler/frontend, with K2 as a test oracle. See Self-hosting and bootstrap for how far that is.

The same program at every stage ​

A small program, counter.kt, that CI builds and runs:

kotlin
// A tiny program used to show what the compiler produces at each stage (MIR and C).

class Counter(var n: Int) {
    fun inc(): Int {
        n += 1
        return n
    }
}

fun fib(n: Int): Int = if (n < 2) n else fib(n - 1) + fib(n - 2)

fun main() {
    val c = Counter(0)
    println(fib(10))
    println(c.inc())
}
text
55
1

Its MIR is on the next page, its C a little further down on the same page.

Module layout ​

ModuleRoleJVM APIs allowed?
compiler/backendMIR model, name mangling, RcOpt, ThrowAnalysis, CEmitter, memory checkerno (pure Kotlin, enforced by a test)
compiler/native-driverreads a serialized .mir file and writes C; the entry point of the self-hosted backendno
compiler/frontendown lexer, parser, typed AST and semantic analysisno
tools/k2-oracle/frontend-oracletests that compare the own frontend against K2yes (tests only)
tools/k2-oracle/jvm-bridgedrives K2, Ir2Mir, the CLI, starts clangyes, the only module that touches compiler internals

The "purity rule" (no java.*, no kotlin.jvm.*, no reflection in the backend) is what makes self-hosting possible: the backend must be compilable by Mini itself.

Released under the Apache License 2.0.