Documentation4 min read
Reading the assembly
Three panes per program: your Python, the assembly the frontend emits, and the runtime helpers it calls, all generated from the repository at build time.
Pick a program. On the left is the Python, in the middle the assembly the real frontend and code generator emit for it, and on the right a helper from runtime.s. Select a line of Python and the assembly its statement produced is marked; select a line of assembly and the statement that produced it is marked. Every call into the runtime is a link: select it and the helper opens on the right.
What to try #
- Arithmetic and assignment (
pass_arith.py): Eachx = …evaluates into temporaries, parks the result, releases the old value, then stores. Click line 2 and followint_mul. - Aliasing and reference counts (
pass_alias.py):b = ais oneaython_incref: two slots, one list.y += 1builds a new int and leavesxalone. - if / else (
pass_if_basic.py): Every test goes throughaython_notand one compare againstFalse; the branch is one conditional jump. - while loop (
pass_while_sum.py): Awhileis a label at the top, the same test asif, and ajmpback.i <= 100uses the raw-bound helper: no box for100. - for over a range (
pass_for_range.py): Aforparks the iterable, keeps a raw counter, and re-reads the length every pass. - A recursive function (
pass_fn_fib.py): The call passesninrdiwith the count inr10d;fn_fibchecks the count, adopts the argument, and everyreturnjumps to one shared epilogue. - Lists (
pass_list_mut.py): Item stores and appends: the list keeps its own reference to each element. - Strings (
pass_str_basic.py): Literals are pooled bytes in.rodata, copied into a fresh box bystr_new. - Dicts (
pass_dict_basic.py): A display isdict_newplus oneobj_setitemper pair. - A class (
pass_class_basic.py): No class object exists:Counter()is a fresh dict plus a call tofn_Counter____init__, andc.bump(5)callsfn_Counter__bumpwithcas the first argument. - Comprehensions (
pass_comprehension.py): Each comprehension is an empty result plus one inline loop perfor, appending at the innermost level. - Imports (
pass_import_from.py): The helper module’s statements run where theimportstood, with names prefixedinc_import_from_helper__. Both files are on the left. - try / except (
pass_try_basic.py):exc_pushrecords where to land; a failure anywhere inside jumps to the handler label with the kind id inexc_kind, and the handlers compare it. - Exceptions across calls (
pass_try_fn.py): TheZeroDivisionErrorfromint_floordivinsidegunwinds straight pastfn_g’s frame to the handler inmain_main. - finally (
pass_try_finally_bare.py): Thefinallybody is emitted twice: once on the normal path and once on the unwind path, which then re-raises the saved kind.
How the mapping is made #
The code generator writes a # line N: comment at the start of every statement, which is enough to read by eye but not to tell where a statement’s code ends: an if contains its branches, a function contains its body, and a finally body is emitted more than once. So the site does not parse comments. At build time, docsite/figures/_spans.py wraps four functions of codegen.py for one run: _emit_stmt (every statement), _emit_function (a whole function), and _emit_def_defaults and _emit_class_defaults (what a def or class statement does where it stands). Each wrapper puts a marker line before and after the lines the real function returned. The markers are then removed, and the result must equal, byte for byte, the assembly aython run would assemble; if it does not, the build stops. Nothing in src/aython is changed, and no mapping is shown for any assembly other than the real one.
Each assembly line belongs to the innermost statement whose emitter produced it. Lines that belong to none are the file’s directives, main_main’s own prologue and epilogue, and the data sections. For an imported module, the frontend’s parser is wrapped the same way to remember which file each statement came from, so the helper module’s lines are marked in its own file.
How to read a helper #
Every helper borrows its arguments and returns a new owned reference, or nothing. A few rules cover most of what the listings do:
| You see | It means | Explained in |
|---|---|---|
mov rax, [rbp-8] then call aython_incref |
Reading a variable: the expression takes its own reference | Emitting assembly |
mov [rbp-40], rax after a call |
Parking a value in memory before the next call can overwrite rax |
Emitting assembly |
call aython_not, then cmp rax, rcx against aython_false |
Every branch test, whatever its type | Emitting assembly |
mov r10d, N before call fn_... |
The argument count, checked by the callee | Emitting assembly |
jne abort_type, jmp abort_runtime |
A failure: unwind to a handler, or print aython: <Kind> and exit 1 |
Emitting assembly |
call exc_push … call exc_pop |
A try body |
Emitting assembly |
fn_Counter__bump, inc_import_from_helper__double |
A method or an imported function, flattened by the frontend | The frontend |
The object layout behind every [rax + 8] and [rax + 16] is in Objects in memory, and the helpers one by one in The runtime.