Skip to content

Compiler architecture

  • The compiler emits C. brass run compiles the C with a fast compiler (TCC when available) and caches the binary; brass build --release uses an optimizing C compiler.
  • It is a single pass from the AST to C that type-checks, checks ownership, borrows and error handling, and emits code at the same time. Generic code is checked once in the abstract and emitted per instantiation.
  • Planned: a mid-level IR between the checker and C, so the backend can be replaced by a native one.
  • Memory is freed deterministically, without a garbage collector:
    • Moving a value out of a variable or field zeroes the source at the end of the statement; dropping a zeroed value frees nothing.
    • Variables are dropped in reverse order when their scope ends, and on return, break, continue and when try forwards an error.
    • Values that are created but not stored (a + b + c, print(make())) are freed at the end of their statement; a reference to one cannot be kept beyond it.
    • Assignment drops the old value; let _ = value drops right away.
    • String slices, split, trim and lines return copies, so no string ever points into another.
    • A value moved into a call only changes owner once the call runs, so a try that leaves an expression early frees what it had built so far; operands before a try are evaluated left to right to make this exact.
    • Leaving a for over a moved Vec early frees the elements not reached.
  • Leaks are checked, not hoped for:
    • The runtime counts allocations. BRASS_MEMCHECK=1 makes any program report allocations still alive at exit, and brass test fails a test that leaves allocations behind.
    • The test suite runs every program with memcheck, and make asan runs the suite and the compiler compiling itself under AddressSanitizer and LeakSanitizer.

make bench builds each program in bench/ with --release, reports the best of three runs, and times the compiler emitting C for itself (tools/bench.sh). Strings and vectors grow in place with realloc; in a + b + c every + after the first adds to the buffer the first one made; f-strings write integers straight into the result; Str.truncate(n) lets a builder drop text without copying. --time shows where compile time goes: for the compiler itself, about a quarter of a second in Brass and several seconds in the C compiler at -O0 (tcc is much faster when it is installed).

The generated C carries #line directives, so debug info, sanitizer reports and profilers point at .brass files and lines. Build with BRASS_CFLAGS=-g and use gdb as usual: break shapes.brass:12, bt. Code the compiler writes itself is attributed to brass-generated.c.

The compiler is bootstrapped from bootstrap/brass.c, C generated by an earlier version of the compiler itself (see the Makefile). The original bootstrap path was:

  1. stage0 (Rust, in stage0/): a minimal compiler for the subset of Brass needed to write the compiler. It only needs to compile correct programs, so it skips the borrow checker and most diagnostics.
  2. stage1: the compiler written in Brass, compiled by stage0.
  3. stage1 compiles itself; stage0 is frozen. All further features live in the self-hosted compiler, and the C seed replaces stage0 as the starting point.