LockerGnomeTech news and nostalgia for today's technology professionals
Subscribe

Search

Softwareopen source

Simplex builds its own compiler with cat, then prints its executable as octal text

A small systems language exposes the awkward plumbing usually hidden inside an ARM Linux build.

Chris Pirillo feeds a strip of grouped escape marks through a decoder that turns them into a solid executable block, illustrating Simplex’s printable-text workaround.
Illustration: LockerGnome

The compiler can build itself. Getting the resulting program out of its mouth takes a little more persuasion.

Simplex has no raw-byte output, according to its maker’s README. Its string printer stops at a zero byte, while an ELF executable contains plenty of them. Sending that executable through the string printer would give the program a very early retirement.

The workaround prints every byte as a four-character octal escape. A shell script feeds that text to POSIX printf, which turns the escapes back into bytes. The compiler delivers a description of its executable, and printf unwraps it into bytes that the operating system can load. Some assembly required, apparently, even after compilation.

There is a similarly blunt answer to linking the compiler’s own source files: put them in dependency order and concatenate them. The self-hosting documentation says, “There is no linker.” cat gets the job instead, without suddenly acquiring an understanding of machine code.

Simplex targets AArch64 Linux and emits self-contained ELF64 executables directly. The language has one type, int, and five keywords: int, if, else, while and return. Pointer and byte operations supply enough machinery to implement the compiler inside those boundaries.

Its bootstrap starts with a compiler written in C. That builds the Simplex-written compiler, which builds another generation, which builds a third. The project’s check compares all three outputs byte for byte and prints their SHA-256 hashes. It also compares the self-hosted compiler with the C version on the example and test programs.

The documented hashes establish consistency across those bootstrap generations. They do not magically prove every program correct, despite the README’s enthusiasm about self-compilation. A compiler can faithfully reproduce its own mistakes, too.

Input needed its own workaround. The README says the input primitive keeps returning its last byte rather than signaling end-of-file, so the wrapper adds a zero-byte terminator. Output needs zeros preserved; input needs a zero added. This tiny language has an unusually involved relationship with nothing.

The earlier compiler also needed a larger heap and growable child arrays before it could swallow its own source. Those changes are useful reading for anyone tempted to treat self-hosting as a ceremonial victory lap. A compiler becomes a demanding customer of the language it implements.

A future extension could add proper binary output and end-of-file handling, removing two shell contortions. For now, those contortions make the process unusually easy to follow: source enters, textual byte escapes leave, and printf supplies the final conversion.

cat has a compiler credit.

More Software

Every Software story →