aython

Documentation11 min read

Glossary

The terms this site uses, from refcount and immortal to System V and ELF, each in a sentence or two with a link to where it is explained.

Every term a learner meets on this site, grouped by where it first comes up. Each one links to the chapter that explains it, and can itself be linked to as glossary/#term.

The machine #

Register
One of the sixteen 64-bit storage slots inside the processor: rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp and r8 to r15. Instructions work on registers; memory is reached through them. The machine.
Intel syntax
The way of writing x86-64 assembly used everywhere here: destination first (mov rax, rbx copies rbx into rax), no % or $ prefixes, memory in square brackets ([rbp-8]). Every file starts with .intel_syntax noprefix to tell GNU as. The machine.
Stack, stack pointer
A region of memory that grows downward, used for return addresses and local values. rsp always points at its top: push and call move it down by 8 bytes, pop and ret move it back. The machine.
Frame, frame pointer
The part of the stack one function call owns. Generated functions start with push rbp, mov rbp, rsp and one sub rsp, N that reserves every slot at once, then address their slots relative to rbp, the frame pointer. Emitting assembly.
Prologue, epilogue
The few instructions at the start of a function that set up its frame, and at the end that release its locals, restore rbp and return. Emitting assembly.
System V AMD64 calling convention
The contract every function call on x86-64 Linux follows, so separately written code can call each other: integer and pointer arguments in rdi, rsi, rdx, rcx, r8, r9, the result in rax, and rsp a multiple of 16 at every call. aython's generated calls add one register of their own, the argument count in r10d. The machine.
Callee-saved, caller-saved
Which registers survive a call. A function must put back rbx, rbp, r12 to r15 and rsp before it returns (callee-saved); every other register may hold anything afterwards (caller-saved). This is why generated code parks values in stack slots across calls rather than in registers. The machine.
Red zone
The 128 bytes below rsp that System V lets a function use without moving rsp, because nothing else may write there. aython does not use it: every frame is reserved explicitly with sub rsp, N. The machine.
rip-relative addressing
Addressing data by its distance from the current instruction, written [rip + label] or label[rip]. It works wherever the executable is loaded in memory, and is how code reaches the type tags, the immortal singletons and the runtime's globals, as in lea rax, aython_none[rip]. The machine.
Flags, overflow flag
Single bits the processor sets after arithmetic and comparisons, which conditional jumps read. int_add follows add with jo abort_overflow, a jump taken only when the signed result did not fit in 64 bits. The machine.
Label, local label
A name for an address in the assembly, written name:. A .globl label is visible to the linker, so other files can call it; a label starting with .L exists only inside its file and is used for branch targets such as .Lbody_fn_fib. The machine.
Section
A region of the executable with one purpose: .text for code, .data for initialised writable data such as the type tags and singletons, .bss for zeroed globals such as the arena pointers, .rodata for constants such as string literals. The machine.
System call, libc wrapper
The way a program asks the kernel for something it cannot do itself: write bytes, map memory, open a file, exit. aython calls libc's small wrappers (write, mmap, open, read, close, _exit) instead of issuing the syscall instruction directly, a decision recorded in the design. Decisions.

Objects and memory #

Object, box
A Python value in memory: a block in the arena (or in the executable's data, for immortals) that starts with a header and continues with a payload. Every value the program handles is a pointer to one. Objects in memory.
The first 16 bytes of every object: the reference count at offset 0 and the type pointer at offset 8. An int adds its value at offset 16, making 24 bytes; a list adds its length, capacity and a pointer to its items, making 40. Objects in memory.
Type pointer, type tag
The address at offset 8 of an object, pointing at one of the runtime's type labels (type_int, type_list, ...). Checking a type is one comparison of addresses. Class instances carry the dict tag, which is why isinstance(x, dict) is refused. Objects in memory.
Reference count
The number of owners an object has, at offset 0. Taking a reference adds one (aython_incref); releasing it subtracts one (aython_decref), and the object is taken apart and its header recycled when the count reaches zero. Objects in memory.
Owned reference
A pointer whose holder is responsible for releasing it exactly once. Every runtime helper returns a new owned reference, and a variable's slot owns the value stored in it. Objects in memory.
Borrowed reference
A pointer used for a while without taking ownership: nothing is added to the count and nothing must be released. Every runtime helper borrows its arguments. Objects in memory.
Adopt
What a generated function does with its arguments: the caller's owned references become the callee's without an increment, and the callee releases them with its other locals when it returns. Emitting assembly.
Immortal
An object that is never freed: None, True, False, the empty tuple and the integers from -5 to 256. Their reference count is the sentinel 262, which aython_incref and aython_decref recognise and leave alone. Objects in memory.
Small-int cache
small_ints in runtime.s: 262 preallocated immortal ints for -5 to 256, which int_new returns instead of allocating, like CPython's own cache. Objects in memory.
Arena
The one 64 MiB block of memory the runtime asks for with mmap at start-up, in arena_init. Every object and buffer comes from it; running out aborts MemoryError. Objects in memory.
Bump allocation
Allocating by moving a pointer forward: arena_ptr advances by the size asked for, and there is no search and no bookkeeping. Objects in memory.
Free list, size class
A list of freed object headers of one size, reused before the arena is bumped again. Headers are pooled by size: ints, floats (each 24 bytes, in separate pools), strings (32), lists and tuples (40, shared), dicts and ranges (48, separate), sets (56). Objects in memory.
Item buffer
The separate block that holds a list's element pointers, a string's bytes or a dict's entries. Buffers are sized exactly and are not reused when freed: they leak by design, which the 64 MiB arena makes harmless for the programs this runs. Objects in memory.
Reference cycle
Objects that refer to each other in a ring, such as a list that contains itself. Their counts never reach zero, so they are never freed; aython has no cycle collector, and this leak is accepted as long as it never crashes. Where it differs.
Slot
An 8-byte place in a function's frame, such as [rbp-8], holding one pointer. Every variable has one, and so does every intermediate value, so nothing has to survive a call in a register. Emitting assembly.

The program and its translation #

Subset
The part of Python aython admits: what the frontend accepts and the code generator and runtime implement completely. Its exact definition is the program contract in docs/core-design.md; Get started has a summary.
Frontend
src/aython/frontend.py: parses with CPython's ast module, checks every construct against the subset, and flattens imported modules into one program. It runs on your machine, never in the executable. The frontend.
Refusal
The frontend's answer to anything outside the subset: aython: unsupported: <feature> and a non-zero exit, before any assembly is written. A # mvp: reject: corpus file pins each one. The frontend.
Flattening (imports)
How imports work without module objects: the imported sibling file is compiled into the same program at compile time, its names prefixed by the module's (def fib in utils.py becomes utils__fib). The frontend.
C3 linearization (MRO)
The rule CPython uses to order a class's bases into its method resolution order. aython computes it at compile time to decide which __init__ and which class-level values a subclass inherits. The frontend.
Code generator
src/aython/codegen.py: turns the checked tree into assembly text, with a comment on every line. Emitting assembly.
main_main, fn_<name>
The generated functions: main_main runs the module's top-level statements, and each def f becomes fn_f. The runtime's main, called by crt, sets up the arena and calls main_main. Emitting assembly.
Temporary
An intermediate value, such as the boxed 1 in x + 1. The code generator parks each in its own slot and releases it as soon as it has been used. Emitting assembly.
Runtime
runtime.s, the hand-written assembly linked into every executable: the object model, allocator, reference counting, every operation's helper, printing, the exception machinery and main. The runtime.
Helper
One exported runtime function the generated code calls, such as int_add or list_append. Each borrows its arguments and returns a new owned reference, or nothing. The runtime.
Abort
What a run-time failure does when no try catches it: write one line such as aython: ZeroDivisionError to standard error and exit with status 1. There is no traceback and no exception object. The runtime.
Exception kind, kind id
A failure is identified only by a small integer, 1 for TypeError through 11 for NotImplementedError; except clauses match by comparing it. The names and numbers are EXC_KINDS in frontend.py. The frontend.
Handler stack, unwinding
Each try pushes its handler's address with the current rbp and rsp onto a 64-entry stack in the runtime. A failure restores the innermost entry's registers and jumps to its handler: that jump is the unwind. The runtime.
Floor division
Python's // rounds toward negative infinity, so -7 // 2 is -4; the processor's idiv truncates toward zero and gives -3. int_floordiv corrects the quotient after idiv. The runtime.
Ryu
An algorithm that prints the shortest decimal string that reads back as the same double. The runtime's port of it is why print(0.1) spells 0.1, exactly as CPython does. The runtime.
FNV-1a
The simple, fixed hash function aython uses for strings. CPython seeds its string hash randomly per process, so the order in which a set of strings prints can differ between the two, and between two CPython runs. The runtime.
Divergence
A documented place where aython behaves differently from CPython, such as aborting OverflowError where CPython grows a big integer. Each is pinned by a corpus program, usually an abort: one. Where it differs.

Building and linking #

Assembler, object file
GNU as turns assembly text (prog.s.gen, runtime.s) into object files (.o): machine code with the names of the labels it defines and the ones it still needs. Proving it right.
Linker
Joins object files into one executable and resolves each call to the label it names. aython uses cc as the linker driver, which also brings in libc, libm and crt. Proving it right.
ELF
The Executable and Linkable Format, Linux's file format for executables and object files. aython run writes prog.elf, a dynamically linked ELF executable. Proving it right.
crt
The C runtime start-up files the linker adds. They provide _start, where the kernel begins a process, which sets up libc and calls main(argc, argv): in aython, the main in runtime.s. Proving it right.
libc, libm
The C library and its maths library. aython uses libc only for process start-up and the kernel calls, and libm for pow; the allocator, objects and printing are its own. Decisions.

Testing #

Oracle
The program whose answer counts as right. For aython it is CPython, run on the same file as python3. Proving it right.
Differential testing
Testing by running two implementations on the same input and comparing what they do, instead of checking each against hand-written expected output. The judge and the fuzzer both work this way. Proving it right.
Judge
src/aython/judge.py, run by aython check: builds a corpus program, runs it and CPython, and gives the verdict its marker asks for. Proving it right.
Corpus, marker
The test programs in tests/mvp/, 290 files. Each says what must happen in a # mvp: comment: pass, error:Kind, abort:Kind, reject:text or skip. Get started lists the five; Proving it right explains them.
Leak probe, arena flatness
aython check --leak: runs a program's body twice in one process and fails if the second run takes any fresh memory from the arena, which happens only when some object of the first run was never released. Proving it right.
Fuzzer, seed
aython fuzz: generates random programs inside the subset, guaranteed to terminate, and judges each against CPython. The seed fixes the random stream, so a failing program can be generated again; CI checks 250 per push. Proving it right.
Stdlib census
aython stdlib-census: runs the frontend alone over every file of CPython's standard library and counts how many it accepts unchanged, 31 of 637 at the latest count, with the construct that stops each of the rest. The stdlib census.
Census floor
The smallest accepted count CI tolerates, --min-accepted 31 in the workflow. Raised when a change admits more files, so the subset cannot shrink unnoticed. The stdlib census.
Ratio (benchmarks)
The executable's best time divided by CPython's best time for one program; below 1 the executable is faster. Small programs mostly measure CPython's start-up. How fast.

The parser track #

Token, tokenizer
A token is one meaningful piece of source text: a name, a number, a string, an operator, a newline, an indent. The tokenizer, parser/tok.s, cuts a file into them; aython tokdump is the Python reference it must match. Python without Python.
Recursive descent
A way of parsing with one function per grammar rule, each calling the others for the parts it contains. parser/parse.s is written this way, in assembly. Python without Python.
S-expression
A tree written as nested parentheses, such as (assign (name x) (num 1)). The parser track prints one per statement so that its tree can be compared, byte for byte, with what aython sexpdump prints from CPython's ast. Python without Python.
emitdiff
parser/emitdiff.sh: compiles straight-line corpus programs twice, once through the host code generator and once through parse.s and emit.s, and requires both executables to behave identically. Python without Python.