Compiler architecture
- The compiler emits C.
brass runcompiles the C with a fast compiler (TCC when available) and caches the binary;brass build --releaseuses 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,continueand whentryforwards 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 _ = valuedrops right away. - String slices,
split,trimandlinesreturn copies, so no string ever points into another. - A value moved into a call only changes owner once the call runs, so a
trythat leaves an expression early frees what it had built so far; operands before atryare evaluated left to right to make this exact. - Leaving a
forover a movedVecearly frees the elements not reached.
- Leaks are checked, not hoped for:
- The runtime counts allocations.
BRASS_MEMCHECK=1makes any program report allocations still alive at exit, andbrass testfails a test that leaves allocations behind. - The test suite runs every program with memcheck, and
make asanruns the suite and the compiler compiling itself under AddressSanitizer and LeakSanitizer.
- The runtime counts allocations.
Performance
Section titled “Performance”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).
Debugging
Section titled “Debugging”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.
Bootstrap
Section titled “Bootstrap”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:
- 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. - stage1: the compiler written in Brass, compiled by stage0.
- 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.