📚 Encyclopedia

Programming Languages · updated 2026-11-30

Garbage collection, exceptions, records and modules in a small native compiler

compilersgarbage-collectionmark-and-sweepexceptionstype-tablerecordsmodulesx86-64h-language

Once a toy compiler has functions, lists and closures, four features turn it into something that can run real programs: memory that is freed, errors that can be caught, user-defined data types, and more than one source file. This is how each was added to H, a statically typed language compiled straight to x86-64 assembly with a C runtime library (Writing a native compiler in C that emits x86-64 assembly), and which design choices kept each one small. The language-level decisions are in Designing a programming language's syntax and semantics (lessons from H).

Mark and sweep with safe points

The simplest collector that works with code that keeps raw pointers on the machine stack is mark and sweep, objects never move.

The rule this places on all later code generation: before a loop or a call, every live value is pushed or in a stack slot.

Two environment switches pay for themselves immediately: one prints a summary at exit, and a stress mode collects at every safe point. Running the whole test suite in stress mode finds missing roots that would otherwise show up once a month. A test with a memory cap (ulimit -v) proves the collector actually frees: one program needed 136 MB without it and ran in 47 MB with it. The cost was real (that test went from 0.13 s to 0.47 s, mostly registering and sweeping), which is the price of the simple design.

Exceptions without unwinding tables

try { ... } catch (e) { ... } needs no DWARF unwinder if the generated code is simple enough:

Making the caught value a built-in type with members (e.message, e.file, e.line) instead of a string costs one object kind now and lets it grow kinds later without breaking code. A re-thrown exception keeps the place it first happened.

From one bit per type to a type table

Representing a static type as a bit set (int, string, bool, nil, each a bit) makes "int or nil" and "never returns" trivial, and it tempts you to give each list or function type its own bit too. That stops the build at the 25th distinct list type in one program. The fix keeps a type as one int:

Interning means equal types have equal ints, so every == comparison in the checker keeps working. The one new rule: join two types with a type_union() function, never with |, because two table indexes or-ed together are nonsense. There were exactly three such sites (if, try, and &&/||). New kinds of type become a new entry kind, not a new bit.

Records and methods by desugaring

Modules

Program basics that unblock real use

Arguments, an exit status, writing a file and running another program are small individually and together decide whether the language can replace a shell script. Two implementation notes: feed a child's standard input from a temporary file rather than a pipe, so nothing can block on a full pipe; and an output file opened by a pipeline stage should be a collected object, so it is closed even when the pipeline is left by an exception.

Gotchas

See also

Linked from

Sources: h repo (DESIGN.md Layout, runtime/libh.c), h memory (type-table, error-handling, modules, records, program-basics), tldr post #1920, 2026-11-25