It Works: Luma Now Has Its Own Compiler
Five days ago we announced that Luma was leaving Go behind, and the next day we explained why Luma would run in two ways, interpreted and compiled. Today we can say the words we’ve been waiting to write:
The new Luma compiler works.
luma build hello.luma
./hello
Hello WorldThat hello file was made entirely by Luma. Go never saw this program, and no C compiler, assembler or linker touched it. Luma read the code, worked out the machine instructions, and wrote the finished executable file itself, byte by byte.
This post is about how it works on the inside. The language itself deserves its own posts; here we only talk about the compiler.
The big picture
Every Luma program goes through the same short journey:
source ─▶ lexer ─▶ tokens ─▶ parser ─▶ syntax tree ─▶ IR ─┬─▶ interpreter (luma run)
└─▶ machine code ─▶ executable (luma build)Each step has one job and hands its result to the next.
- The lexer reads your text and cuts it into tokens: names, numbers, strings, operators.
- The parser arranges the tokens into a syntax tree, the structure of your program: this is a function, this is a loop, this is an addition.
- The checker and IR compiler walk the tree, check that everything fits together (types, names, what may go where) and translate it into IR, the instructions of a small, simple machine that we designed for Luma.
- From the IR, the road splits in two:
luma rungives the IR to the interpreter, which carries out the instructions one by one, straight away.luma buildgives the IR to the code generator, which turns each instruction into real x86-64 processor instructions. Then our own assembler turns those into bytes, and our own linker lays them out and writes the executable file.
One IR, two ways to run
The most important idea is in the middle: both modes run the same IR. The interpreter and the compiled program don’t each have their own version of Luma. They carry out the very same instructions, produced by the very same compiler.
That is how we keep our promise that luma run and luma build give identical results. Every test program is run both ways, and the outputs must match exactly, down to how a floating-point number is printed and the wording of an error message. If they ever differ, the test fails.
Writing the program file ourselves
Most compilers stop at an intermediate step and let other tools finish the job. Luma goes all the way:
- Machine code. The code generator chooses the x86-64 instructions for every IR instruction.
- Our own assembler. It encodes those instructions into bytes. It even picks short or long jumps depending on how far they go. To make sure it’s right, our tests check that the standard GNU assembler produces exactly the same bytes from the same code.
- Our own linker. It decides where everything goes in memory, fills in the addresses, and writes the file in the format the system expects: ELF for Linux and PE for Windows.
Because Luma writes the file format itself, building for another system is just a matter of choosing the format. From Linux you can make a Windows program, and from Windows a Linux one:
luma build hello.luma -target windows # hello.exeNo libc, no runtime to install
Programs made by Luma don’t use the C library (libc) or any other outside code. They talk to the operating system directly: system calls on Linux, and the system’s own kernel32 on Windows.
Luma brings its own small runtime: the routines for printing, memory, strings, lists and maps, written by the compiler into every program. The results:
- A “Hello World” is around 10 KB.
- It’s a single file: copy it to another computer with the same system and it runs. Luma doesn’t need to be installed there.
- It starts instantly.
Memory, handled for you
A compiled program has to manage its memory. Luma does this without you having to think about it:
- Its own allocator asks the operating system for large chunks of memory and hands out pieces of the right size, reusing pieces that are given back.
- Reference counting keeps track of how many places use each string, list or map. When the last one lets go, the memory is freed right away. No garbage collector pauses.
- Errors clean up too. When an error jumps out of several functions, everything those functions were holding is released on the way out.
Our tests build every program with a special memory check, which reports any value that was never freed. A leak is a failing test.
Fast where it counts
The first version of a compiler can be simple and slow. We’re making ours smarter step by step:
- The compiler keeps values in the processor’s registers while it can, instead of moving everything through memory.
- Comparisons go straight into jumps, so a loop condition becomes two machine instructions.
- Division by a constant becomes a multiplication, which is much faster.
- Strings that are only looked at, never changed, are borrowed instead of copied or counted.
- Heavy jobs such as cryptography get hand-written assembly using the processor’s special instructions, many times faster than plain code.
Look inside
We said Luma should be a language that shows you how languages work. The compiler can show you every step for any program:
luma tokens hello.luma # the tokens
luma ast hello.luma # the syntax tree
luma ir hello.luma # the IR instructions
luma asm hello.luma # the machine code, as readable assemblyFor “Hello World”, the IR is just three instructions:
0000 PUSH_STR 0 ; "Hello World"
0001 PRINT_STR
0002 HALTFollow one small program from text to tokens, to tree, to IR, to machine code, and you’ve seen how a compiler works.
Editor support from the same compiler
The same compiler also powers the language server: luma lsp. Editors such as VS Code use it to show errors as you type, types when you hover, completion, and go to definition. Because it is the real compiler, the editor and luma build never disagree about your code.
What’s next
The compiler is working, and the foundations are in place. We’ll keep growing it toward Luma 0.2.0, and in the next posts we’ll talk about the language itself: what stayed, what changed and why.