T-Diagrams
T-diagrams, also called tombstone diagrams, draw programs, interpreters, compilers and machines as shapes that fit together like puzzle pieces. Where two pieces touch, their languages must match. On this page the diagrams are not just drawings: every piece is real code that runs on the two machines in your browser, its JavaScript engine and its WebAssembly engine.
The four shapes
Each shape names the languages a piece cares about. A program is written in one language. An interpreter reads programs in one language (top) and is itself written in another (bottom). A compiler translates from its left arm to its right arm and is written in the language of its stem. A machine runs one language for real, and everything else must eventually stand on one.
The workbench
Follow the tour, or build your own diagram with the menus. The tools are written in Wee, a small language made for this page (described further down): a compiler from Wee to WebAssembly written in Wee, a Brainfuck interpreter and a Brainfuck-to-Wee translator. The only thing not written in Wee is weei.js, an interpreter for Wee in JavaScript, which is how the first Wee program gets to run at all.
The piece
Run log
How to read a diagram
Read a stack from the top down. A program stands on whatever runs it. If that is an interpreter, the program becomes the interpreter's input, and the interpreter in turn stands on something that runs its language. The tower ends at a machine. Here, Who ran what under each result lists the layers, and only the bottom one is timed by the machine itself.
A compiler turns the piece on its left arm into the same piece written in the language of its right arm. The output has the same name and the same shape. Compile an interpreter and you get an interpreter; compile a compiler and you get a compiler. That is what makes the next trick possible.
Bootstrapping and the fixed point
A compiler written in its own source language, like weec.wee, has a chicken-and-egg problem: to run it you need a Wee compiler. The way out is any other route to running Wee once. Here that route is the interpreter weei.js: weec runs on weei, compiles its own source, and out comes weec.wasm, a compiler that no longer needs the interpreter.
Then comes the check that compiler builders use. Compile the compiler's source with the compiled compiler. The two compilers behave the same, so they must produce the same output, and that output must be byte for byte equal to weec.wasm itself. GCC's three-stage bootstrap compares stage 2 with stage 3 in the same spirit. The tests behind this page check that all three stages agree.
Ken Thompson's lecture Reflections on Trusting Trust shows the uncomfortable side of this: a compiler binary can carry behaviour that appears nowhere in its source, and pass it on each time it compiles itself. A T-diagram shows which language each piece is written in, but not what the piece actually does.
Wee on one screen
Wee has one type, the 32-bit integer, and everything in it is meant to be easy to compile in a single page of Wee.
| Part | Form |
|---|---|
| Top level | fn name(a, b) { … }, var g;, const N = 10;. Functions can be used before they are defined. The program starts at main(). |
| Statements | var x = e;, x = e;, if (e) { … } else { … }, while (e) { … }, return e;, and any expression followed by ; |
| Expressions | numbers, 'c' characters, "text" (the address of the bytes, ending in 0), names, calls, - ! ~, then from loosest to tightest: ||, &&, |, ^, &, == !=, < <= > >=, << >>, + -, * / %. && and || stop early. |
| Built in | getc() reads a byte of input (−1 at the end), putc(c) writes one, load(a)/store(a, v) read and write 4-byte words, loadb(a)/storeb(a, v) read and write bytes |
| Memory | 2 MiB, all zero at the start. String literals are placed from 1 MiB up, so the first 1 MiB is free for the program. |
| Safety | Each loop iteration uses one unit of fuel, and the program stops when it runs out. Dividing by zero or reading outside memory stops it too. Interpreted and compiled Wee stop in exactly the same places. |
Compiled Wee is plain WebAssembly: one function per Wee function, all locals and globals 32-bit integers, two imported functions for input and output, and one exported counter for fuel. Use Save file in the viewer to take any module with you.
What is real here
Every arrow on this page runs. weec.wee really is compiled by itself: the WebAssembly comes from Wee code, not from JavaScript posing as a compiler. weei.js is the only interpreter not written in Wee, and the viewer shows its actual source, taken from the running function. When pieces do not fit and you press Try anyway, the error comes from the browser's own engines.
The page's tests check that six Wee programs give the same output interpreted and compiled. They also check that the compiler reaches a byte-identical fixed point, that 13 broken programs get the same error message from both routes, and that arithmetic, memory and traps agree to the bit.
Sources
- Harvey Bratman, “An alternate form of the ‘UNCOL diagram’”, Communications of the ACM 4(3), 1961. The original T-diagram.
- Jay Earley and Howard Sturgis, “A formalism for translator interactions”, Communications of the ACM 13(10), 1970. The four shapes and the rules for combining them.
- Ken Thompson, “Reflections on Trusting Trust”, Communications of the ACM 27(8), 1984.