Skip to content

MIR ​

MIR is Mini's mid-level IR (compiler/backend/.../Mir.kt). It sits between the Kotlin IR and C: it is typed, structured (if, loops, try, no basic blocks) and close enough to C that emitting code is mostly mechanical. The backend never sees Kotlin IR, which is what lets it run without the K2 compiler.

Shape ​

A module is a list of classes and functions. Functions are made of statements (declare, assign, if, return, setfield, eval, loops, try) over expressions (bin, call, rcall for a runtime call, new, get, casts, boxing). Types are a small set: Int, Long, Boolean, Double, Char, String, Unit, Nothing, nullable string, (obj ...) for classes and interfaces, and (boxed <prim>). Locals are numbered per function.

Serialized format ​

MIR can be written to a text file with minic emit-mir and read back, which is how the self-hosted backend gets its input (stage B1, see Self-hosting). The format is versioned ((mini-mir 1 ...)), S-expression based, ASCII only, one class, function or statement per line. A test checks, for every test program, that MIR to text to MIR to C is byte-identical to MIR to C.

Example ​

The MIR of counter.kt, generated by ./minic emit-mir (the runtime-provided exception classes that precede every module are left out):

lisp
(mini-mir 2 "counter.kt"
  (class "Counter" "kc_Counter" false - false - (fields (f "n" Int false)) (interfaces) (vtable - - -) (itables) false)
  (fun "fib" "kt_fib_AI" (params (l 0 "n" Int)) Int - false false false (owns)
    (declare (l 2 "tmp" Int) -)
    (if (bin Lt (l 0) (int 2))
      (then
        (assign (l 2) (l 0)))
      (else
        (declare (l 1 "tmp" Int) (call "kt_fib_AI" Int (bin Sub (l 0) (int 1))))
        (assign (l 2) (bin Add (l 1) (call "kt_fib_AI" Int (bin Sub (l 0) (int 2)))))))
    (return (l 2)))
  (fun "main" "mk_main" (params) Unit - false false false (owns)
    (declare (l 5 "n" Int) (int 0))
    (assign (l 5) (int 0))
    (eval (rcall "mk_println_int" Unit (call "kt_fib_AI" Int (int 10))))
    (declare (l 4 "tmp" Int) (l 5))
    (assign (l 5) (bin Add (l 4) (int 1)))
    (declare (l 2 "tmp" Int) (l 5))
    (eval (rcall "mk_println_int" Unit (l 2))))
  (fun "Counter.inc" "kc_Counter_Minc" (params (l 0 "this" (obj "Counter" "kc_Counter" false false))) Int - false false false (owns)
    (declare (l 2 "tmp" (obj "Counter" "kc_Counter" false false)) (l 0))
    (declare (l 1 "tmp" Int) (get (l 0) "kc_Counter" "n" Int))
    (setfield (l 2) "kc_Counter" "n" Int (bin Add (l 1) (int 1)))
    (eval unit)
    (return (get (l 0) "kc_Counter" "n" Int)))
  (fun "Counter.<init>" "kc_Counter_C_AI" (params (l 0 "this" (obj "Counter" "kc_Counter" false false)) (l 1 "n" Int)) Unit "kc_Counter" false false false (owns)
    (setfield (l 0) "kc_Counter" "n" Int (l 1))))

What to notice:

  • fib has one extra local, tmp 2, that holds the value of the if expression. Expression-bodied code is flattened into statements.
  • Names carry the C symbol (kt_fib_AI, kc_Counter_Minc); see below.
  • Properties are accessor functions (Counter.<get-n>); the emitter inlines calls to default accessors into plain field reads.
  • (vtable - - -) are the three fixed slots (toString, equals, hashCode), all defaulting to the runtime's Any implementations.
  • Intrinsics are rcall: println of an Int is mk_println_int in the runtime.

The C that comes out ​

Everything after the fixed preamble of helper macros, for counter.kt (RcOpt on):

c
/* prototypes */
MKH_STATIC MkInt kt_fib_AI(MkInt v_n_0);
void mkh_user_main(void);
MKH_STATIC MkInt kc_Counter_Minc(MkObject * v_this_0);
MKH_STATIC void kc_Counter_C_AI(MkObject * v_this_0, MkInt v_n_1);

/* classes */
/* class Counter */
struct kc_Counter {
    MkObject header;
    MkInt f_n;
};
#if UINTPTR_MAX == 0xffffffffffffffffu
_Static_assert(sizeof(struct kc_Counter) == 24, "object layout");
_Static_assert(offsetof(struct kc_Counter, f_n) == 16, "object layout");
#endif
static void *const kc_Counter_vt[] = {(void *)mk_any_to_string, (void *)mk_any_equals, (void *)mk_any_hash_code};
static const MkTypeInfo kc_Counter_ti = {"Counter", sizeof(struct kc_Counter), &mk_type_Any, NULL, kc_Counter_vt, 3, NULL, 0, NULL};

MKH_FN MkObject *kc_Counter_C_AI_new(MkInt a0) {
    MkObject *o = mkh_alloc_inline(&kc_Counter_ti, sizeof(struct kc_Counter), 0);
    kc_Counter_C_AI(o, a0);
    return o;
}

/* fib */
MKH_STATIC MkInt kt_fib_AI(MkInt v_n_0) {
    MkInt v_tmp_2 = 0;
    if (v_n_0 < 2) {
        v_tmp_2 = v_n_0;
    } else {
        MkInt v_tmp_1 = kt_fib_AI((MkInt)((uint32_t)v_n_0 - (uint32_t)1));
        v_tmp_2 = (MkInt)((uint32_t)v_tmp_1 + (uint32_t)kt_fib_AI((MkInt)((uint32_t)v_n_0 - (uint32_t)2)));
    }
    return v_tmp_2;
}

/* main */
void mkh_user_main(void) {
    MkInt v_n_5 = 0;
    v_n_5 = 0;
    mk_println_int(kt_fib_AI(10));
    MkInt v_tmp_4 = v_n_5;
    v_n_5 = (MkInt)((uint32_t)v_tmp_4 + (uint32_t)1);
    MkInt v_tmp_2 = v_n_5;
    mk_println_int(v_tmp_2);
}

/* Counter.inc */
MKH_STATIC MkInt kc_Counter_Minc(MkObject * v_this_0) {
    MkObject * v_tmp_2 = NULL;
    v_tmp_2 = v_this_0;
    MkInt v_tmp_1 = (((struct kc_Counter *)v_this_0)->f_n);
    ((struct kc_Counter *)(v_tmp_2))->f_n = (MkInt)((uint32_t)v_tmp_1 + (uint32_t)1);
    return (((struct kc_Counter *)v_this_0)->f_n);
}

/* Counter.<init> */
MKH_STATIC void kc_Counter_C_AI(MkObject * v_this_0, MkInt v_n_1) {
    ((struct kc_Counter *)(v_this_0))->f_n = v_n_1;
}

/* program entry: an exception that escaped main is reported by the runtime */
void mk_main(void) {
    mkh_user_main();
    if (mk_pending_exception != NULL) mk_report_uncaught();
}

Points worth reading in this output:

  • struct kc_Counter starts with the MkObject header, then the fields. kc_Counter_ti is the type info. new is an allocating wrapper around the constructor-initializer.
  • Int arithmetic is done on uint32_t and cast back, so overflow wraps without C undefined behavior.
  • main is mkh_user_main; the generated mk_main calls it and reports an exception that escaped.
  • The local v_c_1 owns one reference and releases it at the end of the scope with MKH_REL.

C name mangling ​

Mangling.kt makes C names injective and collision-free with the runtime:

ThingName
fun main()always mk_main; the user entry is mkh_user_main
top-level functionkt_ + package parts and name joined with _D, then _A + one letter per parameter: fib(Int) is kt_fib_AI
parameter lettersI Int, J Long, Z Boolean, D Double, C Char, S String, L<class>_E object, Q prefix for nullable
classkc_ + package and nested path joined with _D: kc_Counter; type info kc_Counter_ti
method / constructor<class>_M<name>_A<letters> / <class>_C_A<letters> (plus an allocating _new)
local, string literal, helperv_<name>_<id>, kstr_<n>, mkh_*

Runtime symbols (mk_*, Mk*) are never produced for user code.

Released under the Apache License 2.0.