A Python subset on x86-64 Linux
A small Python, compiled down to the registers.
aython turns a small subset of Python into a native executable: no interpreter at run time, just the assembly a Python frontend emits and 16,217 lines of hand-written runtime underneath it. Every program in its corpus must print, byte for byte, what CPython prints. This site explains how all of it works, for a Python programmer who has never written assembly.
runtime.s, 177 exported symbolsOne line, all the way down #
Here is a whole program from the test corpus:
tests/mvp/pass_arith.pylines 1–9# mvp: pass
x = 6 * 7
y = x + 1
z = (y - 2) * 3
w = z - x
print(x)
print(y)
print(z)
print(w)
The frontend checks it, gives each of x, y, z and w an 8-byte slot on the stack, and writes assembly for every line. This is the part for y = x + 1, cut from what the real code generator emits for this file when the site is built:
tests/mvp/pass_arith.py · emitted · main_mainmov [rbp-8], rax # x now owns the new value
# line 3: y = <expr> (release old, compute new, store)
mov rax, [rbp-8] # rax = x's object (still owned by the slot)
mov rdi, rax # arg = same pointer (helper borrows it)
call aython_incref # take our own shared ownership; rax = same pointer
mov [rbp-48], rax # park owned left operand (calls below would clobber rax)
# int literal: box it (movabs covers the full i64 range; small values hit the cache)
movabs rdi, 1 # arg = raw integer value (not a pointer yet)
call int_new # rax = new owned int box (refcnt 1)
mov [rbp-56], rax # park owned right operand
mov rdi, [rbp-48] # arg1 = left (helper borrows it)
mov rsi, [rbp-56] # arg2 = right (helper borrows it)
call int_add # rax = new owned result (or aborts TypeError/OverflowError/...)
mov [rbp-64], rax # park the result across the cleanup calls
mov rdi, [rbp-48] # release left operand
call aython_decref # (single-use temporaries usually freed here)
mov rdi, [rbp-56] # release right operand
call aython_decref # (same)
mov rax, [rbp-64] # rax = owned result for our caller
mov [rbp-40], rax # park the new owned value where calls cannot clobber it
mov rdi, [rbp-16] # rdi = old y (borrowed for the call)
call aython_decref # release old y; may free it right here
mov rax, [rbp-40] # rax = new value again (survived the call in memory)
mov [rbp-16], rax # y now owns the new value
# line 4: z = <expr> (release old, compute new, store)
Each value is a pointer to a small object in memory. The generated code takes its own reference to x’s object (aython_incref), boxes the constant 1 (int_new), and calls the runtime’s int_add, which checks both types, adds, watches the overflow flag and returns a new object. Then it releases the two temporaries and puts the result in y’s slot, releasing whatever was there before. Nothing is implicit: every change of ownership is a call you can point at.
CPython prints this for the program, and the judge’s rule is that the executable prints exactly the same bytes:
42
43
123
81
What you can learn here #
The report builds the whole system up from nothing, teaching the assembly, the calling convention and the object model at the point each is needed.
The machine
Sixteen registers, a stack, and one calling convention: the dozen ideas every listing on this site rests on, taught from code aython emits.
Start here →Objects in memory
A 16-byte header, a reference count, a type pointer. Who owns a value, when it is freed, and the arena it comes from.
Objects →The frontend and the code generator
How CPython's own parser is reused, how the subset is enforced construct by construct, and how a checked tree becomes assembly.
Frontend →The runtime
Integers that abort instead of overflowing, floats printed exactly as CPython prints them, and sets that iterate in CPython's own order.
Runtime →Proving it right
Nobody reviews the assembly line by line. A differential judge, a leak probe and a fuzzer decide instead.
The judge →Python without Python
A tokenizer and a parser written in assembly, checked on every corpus file against Python references, the parser's built on CPython's own ast.
What exists #
A frontend and code generator in Python (src/aython/), the runtime in runtime.s, a corpus of 270 programs under tests/mvp/ (106 that must match CPython’s output, 71 that must fail with CPython’s error kind, 22 that pin a documented difference or a failure CPython cannot reproduce, 71 that must be refused), a fuzzer, a benchmark harness with results from one native x86-64 machine, a census of the standard library, and the first stages of a parser written in assembly under parser/. What the subset admits is summed up in Get started; where it behaves differently from CPython is in its own chapter.